Raw JSON objects from strings?

1,199 views
Skip to first unread message

William Pietri

unread,
Mar 30, 2009, 5:03:51 PM3/30/09
to google-gson
Hi! I'm interested in eventually using Gson for its intended purpose,
mapping between Java classes and JSON. Currently, however, I'm
focusing on the client-side development, and just want my server-side
code to manage bags of JSON until the proper object model becomes more
obvious.

I see that there are objects like JsonArray and JsonObject, but I
don't see an obvious way to turn JSON strings into those. Is there
anything in the public API that lets me do that? And if not, would you
accept a patch to change that?

Thanks,

William

inder

unread,
Mar 30, 2009, 6:09:46 PM3/30/09
to google-gson
We will have this feature soon, in Gson 1.4 beta to be release soon
after 1.3 goes final.
See http://code.google.com/p/google-gson/issues/detail?id=110

Inder

William Pietri

unread,
Mar 30, 2009, 7:12:09 PM3/30/09
to googl...@googlegroups.com
Thanks! That's good news.

Could you give me your best guess as to the API? I'll hack my copy to let me get started now, and then switch over once things get released.

For example, would a round trip look like this?

    Gson parser = new Gson();
    JsonElement parseResult = parser.toJsonTree(someJsonString);
    String backToJson = parseResult.toString();
    assertThat(someJsonString, isEquivalentJsonTo(backToJson));

Thanks,

William
Message has been deleted

inder

unread,
Mar 30, 2009, 9:12:41 PM3/30/09
to google-gson
It will be more like:

Gson parser = new Gson();
JsonElement parseResult = parser.toJsonTree(someJsonString);
MyClass obj = parser.fromJson(parseResult, MyClass.class);

The following will actually not work as expected:
String backToJson = parseResult.toString();

This is because JsonElement.toString() method currently does NOT
escape strings properly. So, if there is a string with escape
characters, those will get inlined as is. This is filed as a bug
http://code.google.com/p/google-gson/issues/detail?id=107
When this issue gets resolved, you will be able to do what you are
intending to.

Inder


On Mar 30, 4:12 pm, William Pietri <will...@scissor.com> wrote:
> Thanks! That's good news.
>
> Could you give me your best guess as to the API? I'll hack my copy to
> let me get started now, and then switch over once things get released.
>
> For example, would a round trip look like this?
>
>     Gson parser = new Gson();
>     JsonElement parseResult = parser.toJsonTree(someJsonString);
>     String backToJson = parseResult.toString();
>     assertThat(someJsonString, isEquivalentJsonTo(backToJson));
>
> Thanks,
>
> William
>
>
>
> inder wrote:
> > We will have this feature soon, in Gson 1.4 beta to be release soon
> > after 1.3 goes final.
> > Seehttp://code.google.com/p/google-gson/issues/detail?id=110

joel....@gmail.com

unread,
Apr 1, 2009, 9:31:02 PM4/1/09
to google-gson
This functionality is available in the Gson 1.3 Release which is now
available. To get the parse tree you can do the following:

JsonParser parser = new JsonParser();
JsonElement parseTreeRootElement = parser.parse(jsonStringValue);

The nice thing about the above approach is you are not required to set
up the Gson overhead of constructing the "default" type adapter, etc.

As well, as Inder stated above, the conversion back to the string
value is not as simple as calling a "toString" on the JsonElement
object.

Hope this feature helps you,
Joel

On Mar 30, 6:12 pm, inder <inder...@gmail.com> wrote:
> It will be more like:
>
> Gson parser = new Gson();
> JsonElement parseResult = parser.toJsonTree(someJsonString);
> MyClass obj = parser.fromJson(parseResult, MyClass.class);
>
> The following will actually not work as expected:
> String backToJson = parseResult.toString();
>
> This is because JsonElement.toString() method currently does NOT
> escape strings properly. So, if there is a string with escape
> characters, those will get inlined as is. This is filed as a bughttp://code.google.com/p/google-gson/issues/detail?id=107
Reply all
Reply to author
Forward
0 new messages