Deserialize to JsonArray causes JsonParseException (GSON-1.6)

2,239 views
Skip to first unread message

Tommy1199

unread,
Dec 20, 2010, 8:09:59 AM12/20/10
to google-gson
Hi everyone,

I try to use Gson (1.6) in combination with Jersey to work with the
Neo4J REST api.

One of the responses of Neo4J looks like this:

[ {
"start" : "http://localhost:7474/db/data/node/0",
"nodes" : [ "http://localhost:7474/db/data/node/0", "http://
localhost:7474/db/data/node/1" ],
"length" : 1,
"relationships" : [ "http://localhost:7474/db/data/relationship/
0" ],
"end" : "http://localhost:7474/db/data/node/1"
} ]

and I try to deserialize it to a JsonArray with this code:

String jsonText = response.getEntity(String.class);
JsonArray jsonObjects = gson.fromJson(jsonText, JsonArray.class);

but I got this exception

Exception in thread "main" com.google.gson.JsonParseException:
Expecting array but found object: []
at
com.google.gson.JsonArrayDeserializationVisitor.startVisitingObject(JsonArrayDeserializationVisitor.java:
90)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:125)
at
com.google.gson.JsonDeserializationContextDefault.fromJsonArray(JsonDeserializationContextDefault.java:
64)
at
com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:
49)
at com.google.gson.Gson.fromJson(Gson.java:568)
at com.google.gson.Gson.fromJson(Gson.java:515)
at com.google.gson.Gson.fromJson(Gson.java:484)
at com.google.gson.Gson.fromJson(Gson.java:434)
at com.google.gson.Gson.fromJson(Gson.java:406)
at JerseyTest.main(JerseyTest.java:40)


Have I missed something?

Pawel Veselov

unread,
Dec 20, 2010, 7:16:32 PM12/20/10
to googl...@googlegroups.com
Hi,

On Mon, Dec 20, 2010 at 5:09 AM, Tommy1199 <sascha...@gmail.com> wrote:
> Hi everyone,
>
> I try to use Gson (1.6) in combination with Jersey to work with the
> Neo4J REST api.
>
> One of the responses of Neo4J looks like this:
>
> [ {
>  "start" : "http://localhost:7474/db/data/node/0",
>  "nodes" : [ "http://localhost:7474/db/data/node/0", "http://
> localhost:7474/db/data/node/1" ],
>  "length" : 1,
>  "relationships" : [ "http://localhost:7474/db/data/relationship/
> 0" ],
>  "end" : "http://localhost:7474/db/data/node/1"
> } ]
>
> and I try to deserialize it to a JsonArray with this code:

JsonArray is an internal json element representation of an array.
I don't think you can deserialize anything into JsonArray.
Gson generally is used for serilizing your structures, or deserializing into
your structures (your structures here means structures that you defined)
In this case, there is such definition offered.

Also, I believe that the above is not really a good json, but I may be mistaken.
Json.org is actually not particularly clear on what a valid Json stream is
(they say what json elements are, but the root element is not really defined.
They should've added "json := xxxx", for example "json := object |
array", but it may
be just "json := object"), so it may be so that valid json has to
start from object.

Typically, you'd need to define the object structure where you want
to load your data into. So you'd define an object which will contain whatever
things you expect in the response. I guess that if you don't know all
the things,
you'd need to pass over a map.

So it should be either:

MyObject[] jsonObjects = gson.fromJson(jsonText, MyObject[].class);

or

Map<String, Object>[] result = gson.fromJson(jsonText, HashMap[].class);

limpb...@gmail.com

unread,
Dec 20, 2010, 11:11:52 PM12/20/10
to google-gson
If you aren't using GSON for databinding, just do this:
JsonArray array = new JsonParser().parse(jsonText).getAsJsonArray();

Tommy1199

unread,
Dec 21, 2010, 5:49:20 AM12/21/10
to google-gson
Thanks for the quick answer. Works for me :)

Richard Schilling

unread,
Jan 5, 2011, 11:29:41 PM1/5/11
to google-gson
I am using JSON for databinding, and I've run into this problem too.
I may actually be able to submit a patch to fix it. Here's what is
going on...

With the input of this:

[ {
"start" : "http://localhost:7474/db/data/node/0",
"nodes" : [ "http://localhost:7474/db/data/node/0", "http://
localhost:7474/db/data/node/1" ],
"length" : 1,
"relationships" : [ "http://localhost:7474/db/data/relationship/
0" ],
"end" : "http://localhost:7474/db/data/node/1"
} ]


The parser resolves the input stream to these types:

JSONObject:
start: String
nodes: JSONArray
length: String (or perhaps integer)
relationships: JSONArray
end: String

public class MyClass{
public String start;
public String[] nodes;
/// etc .... notice nodes here is an array.
}

Then, when the deserialization occurs, the deserializer tries to
associate nodes with a simple string and not a whole array.

It's not clear how to get the deserializer to resolve to the correct
type. But, I'm looking into it.

inde...@gmail.com

unread,
Jan 10, 2011, 6:20:32 PM1/10/11
to google-gson
If nodes was an array type as defined in MyClass, Gson WILL map it to
an array. If you are not seeing that behavior, please file a bug.

On Jan 5, 8:29 pm, Richard Schilling <richard.rootwirel...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages