Deserializing an object that contains JSON using GSON

浪尽此生 提交于 2019-12-22 07:55:11

问题


I'm using gson to deserialize POJO objects from JSON representations.

I'd like one of the fields in one of my POJOs to contain arbitrary JSON data. For example:

class B {
    public String stringField;
    public JsonObject jsonField;
}

I'd like to be able to call Gson.fromJson(json, B.class) on the following JSON:

{
    "stringField": "booger",
    "jsonField" :
    {
        "arbitraryField1": "foo"
    }
}

and have the resulting B.jsonField contain a JsonObject with an arbitraryField of value foo.

However, when I attempt to do this, jsonField is always an empty object ({}). In fact, more generally, it appears that the following always returns an empty object:

new Gson().fromJson("{ foo: 1 }", JsonObject.class)

I would expect the above to return an object containing a field named foo of value 1.

How can I have gson preserve arbitrary json data when deserializing json to POJOS?


回答1:


I was able to work around the problem by introducing a wrapper object that contains a JsonObject, and then writing a custom deserializer for that object that simply returns the original json. However, it seems like there must be a better way.

For posterity, the deserializer and the trivial wrapper object look like the following:

class MyJsonObjectWrapperDeserializer implements JsonDeserializer<MyJsonObjectWrapper> {
    @Override
    public MyJsonObjectWrapper deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        return new MyJsonObjectWrapper(json.getAsJsonObject());
    }
}

class MyJsonObjectWrapper {
    public JsonObject json;

    public MyJsonObjectWrapper(JsonObject json) {
        this.json = json;
    }
}



回答2:


you can use JsonParser:

JsonParser parser = new JsonParser();
JsonObject o = parser.parse("{ \"foo\": \"1\" }").getAsJsonObject();



回答3:


Consider this deserializer, which is my interpretation of the doc.

import com.google.gson.*;
import java.lang.reflect.Type;

class B {
    public String stringField;
    public JsonObject jsonField;
}

class BDeserializer implements JsonDeserializer<B> {
    public B deserialize(JsonElement json, Type typeOfT, 
                         JsonDeserializationContext context)
                 throws JsonParseException {
        JsonObject jsonObject = json.getAsJsonObject();

        B b = new B();
        b.stringField = jsonObject.get("stringField").getAsString();
        b.jsonField = jsonObject.getAsJsonObject("jsonField"); 

        return b;
    }
}

public class Test {
    static public void main(String[] args) {
        GsonBuilder gson = new GsonBuilder();
        gson.registerTypeAdapter(B.class, new BDeserializer());

        String json = " { \"stringField\" : \"booger\", \"jsonField\" : { \"arbitraryField1\" : \"foo\" } } ";

        B b = gson.create().fromJson(json, B.class);
        System.out.println(b.stringField);
        System.out.println(b.jsonField.toString());
    }
}


来源:https://stackoverflow.com/questions/12380927/deserializing-an-object-that-contains-json-using-gson

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!