api - Gson Deserialization error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING -
i've seen many questions on topic, can't seem find solution problem.
i'm getting deserialization error:
com.google.gson.jsonsyntaxexception: java.lang.illegalstateexception: expected begin_object string @ line 1 column 3384
when deserializing routingtable clas specifically, decerialization works else. i've uploaded serialized json data here: http://jsonfiddle.net/bx0ga
class serialize:
public class routingtable { private transient final node localnode; // current node private final kadbucket[] buckets; { buckets = new kadbucket[nodeid.id_length]; } // methods.... }
kadbucket class:
public class kadbucket implements bucket { private final int depth; private final map<nodeid, node> nodes; { nodes = new hashmap<>(); } // methods , on }
serialization code: have serialization class handles serialization using generics:
public class jsonserializer<t> implements kadserializer<t> { private final gson gson; { gson = new gson(); } @override public void write(t data, dataoutputstream out) throws ioexception { try (jsonwriter writer = new jsonwriter(new outputstreamwriter(out))) { writer.beginarray(); /* store content type */ gson.tojson(data.getclass().getname(), string.class, writer); /* store content */ gson.tojson(data, data.getclass(), writer); writer.endarray(); } } @override public t read(datainputstream in) throws ioexception, classnotfoundexception { try (datainputstream din = new datainputstream(in); jsonreader reader = new jsonreader(new inputstreamreader(in))) { reader.beginarray(); /* read class name */ string classname = gson.fromjson(reader, string.class); /* read , return content*/ return gson.fromjson(reader, class.forname(classname)); } } }
so serialization, do:
// writing dout output stream new jsonserializer<routingtable>().write(this.localnode.getroutingtable(), dout); // reading din input stream routingtable irtbl = new jsonserializer<routingtable>().read(din);
using works every other class in system, except routing tables.
routingtable contains class variable transient, used indicate field should not serialized.
Comments
Post a Comment