Using GSON do not parse a field, only keep it with the json raw string

ⅰ亾dé卋堺 提交于 2019-12-01 05:22:32

问题


Using GSON in Java is there any annotation where I can indicate a field that it should keep it as a raw string even though it is an object. ?

Or What would be the easiest way to achieve this?

//This is the original
    @SerializedName("perro")
    public Perro perro

//This is what I want to achieve 
    @SerializedName("perro")
    public String perro

So the result should be 
perro = "{"Users":[{"Name":"firulais","Raza":"beagle"},{"Name":"Spike","Value":"Terrier"}]}"

回答1:


The only way I found this to work was using

public JsonElement perro;



回答2:


Basically speaking, You need to create a custom gson TypeAdapter class and write the conversion login from Object to String yourself.

Then annotate the field indicating what TypeAdapter to use in order to read/write it using gson.

More details in this blog post: Gson TypeAdapter Example

Example: Prasing class object as a raw JSON string

public class StringTypeAdapter extends TypeAdapter<String> {

    @Override
    public void write(JsonWriter out, String value) throws IOException {
        try {
            JSONObject jsonObject = new JSONObject(value);
            out.beginObject();
            Iterator<String> iterator = jsonObject.keys();
            while (iterator.hasNext()) {
                String key = iterator.next();
                String keyValue = jsonObject.getString(key);
                out.name(key).value(keyValue);
            }
            out.endObject();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String read(JsonReader in) throws IOException {
        in.beginObject();
        JSONObject jsonObject = new JSONObject();
        while (in.hasNext()) {
            final String name = in.nextName();
            final String value = in.nextString();
            try {
                jsonObject.put(name, value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        in.endObject();
        return jsonObject.toString();
    }
}

Using the TypeAdapter:

@JsonAdapter(StringTypeAdapter.class)
private String someClass; // Lazy parsing this json



回答3:


Based on @mrsegev's answer, here's a simpler version (in Kotlin) that works with arbitrary objects:

class RawJsonAdapter: TypeAdapter<String>() {
    override fun write(out: JsonWriter?, value: String?) {
        out?.jsonValue(value)
    }
    override fun read(reader: JsonReader?): String {
        return JsonParser().parse(reader).toString()
    }
}

This takes advantage of JsonWriter#jsonValue() which was added in https://github.com/google/gson/pull/667

Usage:

@JsonAdapter(RawJsonAdapter::class)
val fieldName: String? = null



回答4:


You should be able to use public JsonObject perro;

You can then call gson.toJson(perro) to get the String value.



来源:https://stackoverflow.com/questions/44831266/using-gson-do-not-parse-a-field-only-keep-it-with-the-json-raw-string

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