Object to JsonElement

3,390 views
Skip to first unread message

petr kobalicek

unread,
Sep 19, 2009, 6:57:54 PM9/19/09
to google-gson
Hi,

is there a way how to convert java Object to JsonElement?

I'm talking about code in Gson.toJson() method:

JsonSerializationContext context = new
JsonSerializationContextDefault(
createDefaultObjectNavigatorFactory(), serializeNulls,
serializers);
JsonElement jsonElement = context.serialize(src, typeOfSrc);

I'd like to convert Object to JsonElement and to add it to another
JsonObject. Should I use another lib to be able to manipulate with
JSON in lower level?

Thanks
- Petr

inder

unread,
Sep 22, 2009, 4:58:04 PM9/22/09
to google-gson
Not in the current version of Gson, but Gson 1.4 has such a method:
Gson.toJsonTree() gives you a JsonElement tree corresponding to an
object.

You can wait for Gson 1.4 to be released (hopefully before the month
end) or build it yourself from the repository using Maven2.

Inder

Ian Clarke

unread,
Sep 22, 2009, 5:10:04 PM9/22/09
to googl...@googlegroups.com
On Tue, Sep 22, 2009 at 3:58 PM, inder <inde...@gmail.com> wrote:
Not in the current version of Gson, but Gson 1.4 has such a method:
Gson.toJsonTree() gives you a JsonElement tree corresponding to an
object.

You can wait for Gson 1.4 to be released (hopefully before the month
end) or build it yourself from the repository using Maven2.

Exciting!  Will you update the maven repository immediately upon its release?

Is there a list of the significant changes/improvements (I assume you'll be addressing the pretty-printer stuff we discussed a while back)?

Ian.

--
Ian Clarke
CEO, Uprizer Labs
Email: i...@uprizer.com
Ph: +1 512 422 3588
Fax: +1 512 276 6674

inder

unread,
Sep 22, 2009, 8:12:57 PM9/22/09
to google-gson


On Sep 22, 2:10 pm, Ian Clarke <ian.cla...@gmail.com> wrote:
> On Tue, Sep 22, 2009 at 3:58 PM, inder <inder...@gmail.com> wrote:
> > Not in the current version of Gson, but Gson 1.4 has such a method:
> > Gson.toJsonTree() gives you a JsonElement tree corresponding to an
> > object.
>
> > You can wait for Gson 1.4 to be released (hopefully before the month
> > end) or build it yourself from the repository using Maven2.
>
> Exciting!  Will you update the maven repository immediately upon its
> release?

We will update the maven repository that we host. But we will not
update the public maven repository (it is a somewhat tedious process
that I dont have cycles for. If someone could help here, we will
appreciate it).

> Is there a list of the significant changes/improvements (I assume you'll be
> addressing the pretty-printer stuff we discussed a while back)?

There are quite a few changes in this version: Gson has new methods to
output Json tree, deserialize from Json tree, an async parser API,
byte and collection array deserialization improvements (from 80KB to
11MB), etc.

We will not be directly addressing the pretty printing stuff, but you
can use call Gson.toJsonTree() and the navigate the tree to print it
nicely yourself. Sorry, we couldn't devote enough cycles to get it
right, so we didn't want to commit to a half-baked solution.

Inder

petr kobalicek

unread,
Sep 23, 2009, 8:02:24 PM9/23/09
to google-gson
Hi Inder,

thanks for reply. Temporary I'm choosing json-lib because I need
working solution, maybe I will look back at gson next time:) I like
the generics support and powerful filtering support in gson.

Cheers
- Petr
Reply all
Reply to author
Forward
0 new messages