All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.jfunc.json.impl.JSONObject Maven / Gradle / Ivy

package top.jfunc.json.impl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import top.jfunc.json.Json;
import top.jfunc.json.JsonArray;
import top.jfunc.json.JsonException;
import top.jfunc.json.JsonObject;
import top.jfunc.json.strategy.FieldExclusionStrategy;
import top.jfunc.json.strategy.FieldNameChangeNamingStrategy;

import java.util.List;
import java.util.Map;

/**
 * @author xiongshiyan at 2018/6/10
 */
public class JSONObject extends BaseMapJSONObject {

    public JSONObject(Map map){
        super(map);
    }
    public JSONObject(){
    }
    public JSONObject(String jsonString){
        super(jsonString);
    }

    @Override
    protected Map str2Map(String jsonString) {
        return new Gson().fromJson(jsonString, Map.class);
    }

    @Override
    protected String map2Str(Map map) {
        return new Gson().toJson(map , Map.class);
    }

    @Override
    public JsonObject parse(String jsonString) {
        this.map = new Gson().fromJson(jsonString, Map.class);
        return this;
    }

    @Override
    public  String serialize(T javaBean, boolean nullHold, String... ignoreFields) {
        //new GsonBuilder().serializeNulls().setFieldNamingPolicy().addSerializationExclusionStrategy()
        GsonBuilder gsonBuilder = new GsonBuilder()
                .setFieldNamingStrategy(new FieldNameChangeNamingStrategy())
                .addSerializationExclusionStrategy(new FieldExclusionStrategy(ignoreFields));

        if(nullHold){
            return gsonBuilder.serializeNulls().create().toJson(javaBean);
        }
        return gsonBuilder.create().toJson(javaBean);
    }

    @Override
    public  T deserialize(String jsonString, Class clazz) {
        return new Gson().fromJson(jsonString , clazz);
    }

    @Override
    public JsonObject getJsonObject(String key) {
        assertKey(key);
        //这里不能使用getJSONObject,因为每一种Json实现不一样,给出的JsonObject类型是不一致的。
        //这里就是各种JsonObject不能混用的原因
        Object temp = this.map.get(key);
        Object t = checkNullValue(key, temp);

        if(t instanceof Map){
            return new JSONObject((Map) t);
        }

        return (JsonObject) t;
    }

    @Override
    public JsonArray getJsonArray(String key) {
        assertKey(key);
        //这里不能使用getJSONObject,因为每一种Json实现不一样,给出的JsonObject类型是不一致的。
        //这里就是各种JsonObject不能混用的原因
        Object temp = this.map.get(key);
        Object t = checkNullValue(key, temp);

        if(t instanceof List){
            return new JSONArray((List)t);
        }
        return (JsonArray) t;
    }
    @Override
    public JsonObject fromMap(Map map) {
        return new JSONObject(map);
    }


    @Override
    public Json toJson(Object o) {
        if(o instanceof List){
            return new JSONArray((List) o);
        }
        if(o instanceof Map){
            return new JSONObject((Map) o);
        }
        try {
            return (Json) o;
        } catch (Exception e) {
            throw new JsonException("不能将非Json的对象转换为Json");
        }
    }
}