java - Gson deserialize JSON array as Object -
i've got object, need deserialized / unmarshalled json (using gson). problem 1 of properties, in javabean, complex object, needs serialized fron json array of numbers:
the json (somewhat similar to):
{ "name": "john doe", "location": [ 9.560006, 55.719474 ], }
the targeted classes (somwaht similar to):
class userlocation { private string name; private location location; // ... "zero-args constructor", get'ers , set'ers } class location { private double longitude; private double latitude; // ... "zero-args constructor", get'ers , set'ers }
my gsonbuilder configured as:
new gsonbuilder() .registertypeadapter(location.class, new locationtypeconverter()) .create();
where locationtypeconverter looks this:
public class locationtypeconverter extends typeadapter<location> implements jsonserializer<location>, jsondeserializer<location> { private static final int longitude_index = 0; private static final int latitude_index = 1; @override public jsonelement serialize(location src, type srctype, jsonserializationcontext context) { jsonarray array = new jsonarray(); array.set(latitude_index, new jsonprimitive(src.getlatitude())); array.set(longitude_index, new jsonprimitive(src.getlongitude())); return array; } @override public location deserialize(jsonelement json, type type, jsondeserializationcontext context) throws jsonparseexception { jsonarray array = json.getasjsonarray(); return new location(array.get(latitude_index).getasdouble(), array.get(longitude_index).getasdouble()); } @override public void write(jsonwriter out, location value) throws ioexception { out.beginarray().value(value.getlongitude()).value(value.getlatitude()).endarray(); } @override public location read(jsonreader in) throws ioexception { in.beginarray(); double longitude = in.nextdouble(); double latitude = in.nextdouble(); in.endarray(); return new location(longitude, latitude); } }
but no avail, since keep recieving following stacktrace:
retrofit.retrofiterror: com.google.gson.jsonsyntaxexception: java.lang.illegalstateexception: expected begin_object begin_array @ line 1 column 85 path $[0].location @ retrofit.restadapter$resthandler.invokerequest(restadapter.java:377) @ retrofit.restadapter$resthandler.access$100(restadapter.java:220) @ retrofit.restadapter$resthandler$2.obtainresponse(restadapter.java:278) @ retrofit.callbackrunnable.run(callbackrunnable.java:42) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) @ retrofit.platform$android$2$1.run(platform.java:142) @ java.lang.thread.run(thread.java:818) caused by: retrofit.converter.conversionexception: com.google.gson.jsonsyntaxexception: java.lang.illegalstateexception: expected begin_object begin_array @ line 1 column 85 path $[0].location @ retrofit.converter.gsonconverter.frombody(gsonconverter.java:67) @ retrofit.restadapter$resthandler.invokerequest(restadapter.java:362) at retrofit.restadapter$resthandler.access$100(restadapter.java:220) at retrofit.restadapter$resthandler$2.obtainresponse(restadapter.java:278) at retrofit.callbackrunnable.run(callbackrunnable.java:42) at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) at retrofit.platform$android$2$1.run(platform.java:142) at java.lang.thread.run(thread.java:818) caused by: com.google.gson.jsonsyntaxexception: java.lang.illegalstateexception: expected begin_object begin_array @ line 1 column 85 path $[0].location @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(reflectivetypeadapterfactory.java:200) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$1.read(reflectivetypeadapterfactory.java:103) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(reflectivetypeadapterfactory.java:196) @ com.google.gson.internal.bind.typeadapterruntimetypewrapper.read(typeadapterruntimetypewrapper.java:40) @ com.google.gson.internal.bind.collectiontypeadapterfactory$adapter.read(collectiontypeadapterfactory.java:81) @ com.google.gson.internal.bind.collectiontypeadapterfactory$adapter.read(collectiontypeadapterfactory.java:60) @ com.google.gson.gson.fromjson(gson.java:810) @ com.google.gson.gson.fromjson(gson.java:775) @ retrofit.converter.gsonconverter.frombody(gsonconverter.java:63) at retrofit.restadapter$resthandler.invokerequest(restadapter.java:362) at retrofit.restadapter$resthandler.access$100(restadapter.java:220) at retrofit.restadapter$resthandler$2.obtainresponse(restadapter.java:278) at retrofit.callbackrunnable.run(callbackrunnable.java:42) at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) at retrofit.platform$android$2$1.run(platform.java:142) at java.lang.thread.run(thread.java:818) caused by: java.lang.illegalstateexception: expected begin_object begin_array @ line 1 column 85 path $[0].location @ com.google.gson.stream.jsonreader.beginobject(jsonreader.java:387) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(reflectivetypeadapterfactory.java:189)
the 2 classes needs deserialized (the 1 containing location instance , "wrapping" object both simple javabean (with appropriate set'er , get'er methods.
none of methods serialize, deserialize nor read being called, doing wrong here?
thanks in advance, help!
edit #1: added target classses / beans clarify object graph i'm trying deserialize to.
this class need ser/des json wrote:
myobject { public string name; public float[] location; public float getlatitude() { if (location != null) return location[0]; return 0; } public float getlongitude() { if (location != null) return location[1]; return 0; } }
while classes wrote have json this:
{ "name": "john doe", "location": { "longitude": 9.560006, "latitude": 55.719474 } }
and finally, test did code, , works fine! need sure you're using same gson object (the 1 gsonbuilder.create)?
gson gson = new gsonbuilder() .registertypeadapter(location.class, new locationtypeconverter()) .setprettyprinting() .create(); userlocation userlocation = new userlocation(); userlocation.name = "ciao"; userlocation.location = new location(1.0, 3.0); string json = gson.tojson(userlocation); system.out.println(json); userlocation newlocation = gson.fromjson(json, userlocation.class); system.out.print(newlocation.location.getlatitude());
Comments
Post a Comment