Deserialize JSON with unknown structure?

5,067 views
Skip to first unread message

paul houle

unread,
May 5, 2011, 2:30:52 PM5/5/11
to google-gson
Suppose I'm getting JSON data from an arbitrary source, where I don't
know what the structure of the data is going to be ahead of time. Is
there some way I can use GSON to load the JSON into a flexible data
model, such a JSONObject?

Phil Bogle

unread,
May 6, 2011, 1:39:54 PM5/6/11
to google-gson
This will let you parse a string into a JsonObject, adapted this from
post by jjnguy on StackOverflow. It would be really nice if Gson had
something like this built in, seems like a basic operation.

http://stackoverflow.com/questions/4110664/gson-directly-convert-string-to-jsonobject-no-pojo

public static JsonObject parseJson(String json) {
GsonBuilder gsonBuilder = new GsonBuilder();
// Register a trivial custom deserializer to obtain the
JsonObject.
gsonBuilder.registerTypeAdapter(JsonElement.class,
new JsonDeserializer<JsonElement>() {
@Override
public JsonElement deserialize(JsonElement json, Type
type,
JsonDeserializationContext context) throws
JsonParseException {
return json;
}
});
return gsonBuilder.create().fromJson(json,
JsonElement.class).getAsJsonObject();

Lin Dapeng

unread,
May 13, 2011, 11:36:14 PM5/13/11
to googl...@googlegroups.com
I would rather there is a constructor for JsonObject JsonObject(String string) throws NotJsonStructureException. internally, gson must have done something similar to have a JsonElement tree (after parsing the string) before deserialization.


--
You received this message because you are subscribed to the Google Groups "google-gson" group.
To post to this group, send email to googl...@googlegroups.com.
To unsubscribe from this group, send email to google-gson...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-gson?hl=en.




--

Regards,
Dapeng

Adrian Cole

unread,
May 14, 2011, 8:55:43 PM5/14/11
to google-gson
another option to leaving it as a json literal is to leave the unknown
parts generic parameterized to Object, and deal with primitive
wrappers, lists, and maps. Here's another issue I just raised
regarding this approach: http://code.google.com/p/google-gson/issues/detail?id=325

Cheers,
-Adrian
jclouds

On May 13, 8:36 pm, Lin Dapeng <lindap...@gmail.com> wrote:
> I would rather there is a constructor for JsonObject JsonObject(String
> string) throws NotJsonStructureException. internally, gson must have done
> something similar to have a JsonElement tree (after parsing the string)
> before deserialization.
>
>
>
>
>
> On Sat, May 7, 2011 at 1:39 AM, Phil Bogle <pbo...@google.com> wrote:
> > This will let you parse a string into a JsonObject, adapted this from
> > post by jjnguy on StackOverflow.  It would be really nice if Gson had
> > something like this built in, seems like a basic operation.
>
> >http://stackoverflow.com/questions/4110664/gson-directly-convert-stri...

inde...@gmail.com

unread,
May 15, 2011, 2:46:19 PM5/15/11
to googl...@googlegroups.com
------ Sorry, I accidently deleted this message in an attempt to avoid duplicates, so here it is cut-and-pasted ------

From: Adrian Cole <adrian...@gmail.com>
To: google-gson <googl...@googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

I'm just migrating to gson 1.7.1 and absolutely love it.  In jclouds,
we were using 1.6; now with the new no-no-args constructor and type
hierarchy registrations in 1.7.1, I'm getting a cruft-cleansing high.

One of the things crufty things we have left is this very issue,
reflective of the semi-structured nature of a lot of json apis.

We sorted this out last year by making a marker class called JsonBall
and hooking into gson guts to cut based on it.  I'm not terribly happy
with the design, but it works fine.  Here's the issue I raised just
now, summarizing the changes.  Feel free to ping me.


Cheers,
Adrian
jclouds

inde...@gmail.com

unread,
May 15, 2011, 2:51:47 PM5/15/11
to googl...@googlegroups.com
If you just want to parse incoming JSON into a DOM, you should use Gson's Json parser. 

You can navigate the parse tree and call gson.fromJson() selectively on each of the nodes. 
Inder
 
Reply all
Reply to author
Forward
0 new messages