简体   繁体   中英

Gson Deserialization error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING

I've seen the many questions on this topic, but can't seem to find the solution to my problem.

I'm getting a deserialization error:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 3384

when deserializing my RoutingTable clas specifically, decerialization works perfectly well for everything else. I've uploaded the serialized json data here: http://jsonfiddle.net/bx0ga

Class to serialize:

public class RoutingTable
{
    private transient final Node localNode;  // The 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 and so on
}

Serialization code: i have a serialization class that 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 the content type */
            gson.toJson(data.getClass().getName(), String.class, writer);

            /* Now Store the 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 the class name */
            String className = gson.fromJson(reader, String.class);

            /* Read and return the Content*/
            return gson.fromJson(reader, Class.forName(className));
        }
    }
}

So for serialization, I do:

// Writing to dout which is an output stream
new JsonSerializer<RoutingTable>().write(this.localNode.getRoutingTable(), dout);

// Reading from din which is an input stream
RoutingTable irtbl = new JsonSerializer<RoutingTable>().read(din);

Using this works perfectly for every other class in the system, except for routing tables.

RoutingTable包含临时的类变量,用于指示不应序列化字段。

To me it's seems like your input is not JSON object. Happened to me also once also but i don't remember exactly what was the problem but i guess it was because of space in the beginning. Look at the raw input which you are providing for deserialization and convert it into proper json object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM