I have a json structure which I've pasted below. I'd like to deserialize the json into a java POJO using Gson
which is pretty straight-forward, except that I want to keep one of the fields, data
, as a String type instead of a nested object.
JSON structure
{
"created_on": "2015-06-04T16:12:04-0700",
"modified_on": "2015-06-04T16:12:09-0700",
"identifier": "sample",
"name": "some name",
"summary": "some summary",
"data": {
"$type": "a_type",
"some_other_stuff": {
"more_stuff": "lorem ipsum"
},
"type2": {
"$type": "another_type",
"other_stuff": {
"event_more_stuff": "lorem ipsum"
}
}
}
}
My POJO would then look like this:
public class Sample {
private String identifier; // "sample"
private String created_on; // "2015-06-04T16:12:04-0700"
private String modified_on; // "2015-06-04T16:12:09-0700"
private String name; // "some name"
private String summary; // "some summary"
private String data; // "{ \"$type\": ... }"
// getters and setters
}
The data
field should remain as a JSON-formatted String.
I've tried implementing a custom TypeAdapter
and reading the field as a String, but it fails with Expected a string but was BEGIN_OBJECT
.
Also please note, I would like the structure to be maintained on serialization as well - so I can serialize the POJO back to the original JSON structure.
Edit Custom TypeAdapter
:
public class SampleTypeAdapter extends TypeAdapter<Sample> {
@Override
public void write(JsonWriter out, Sample sample) throws IOException {
out.beginObject();
out.name("identifier").value(sample.getIdentifier());
out.name("name").value(sample.getName());
out.name("data").value(sample.getData());
out.name("summary").value(sample.getSummary());
out.name("modified_on").value(sample.getModifiedOn());
out.name("created_on").value(sample.getCreatedOn());
out.endObject();
}
@Override
public Sample read(JsonReader in) throws IOException {
final Sample sample = new Sample();
in.beginObject();
while (in.hasNext()) {
String nextName = in.nextName();
switch (nextName) {
case "identifier":
sample.setIdentifier(in.nextString());
break;
case "name":
sample.setName(in.nextString());
break;
case "data":
sample.setData(in.nextString()); // <-- fails here
break;
case "summary":
sample.setSummary(in.nextString());
break;
case "modified_on":
sample.setModifiedOn(in.nextString());
break;
case "created_on":
sample.setCreatedOn(in.nextString());
break;
default:
in.skipValue();
break;
}
}
in.endObject();
return sample;
}
}
You could create a custom JsonDeserializer like this one:
And you use it like this:
The bad part is that is not as fast as the one you wrote but at least you can do custom parsing like this one.