JsonWriter.value( JsonElement ) ?

571 views
Skip to first unread message

Reuben

unread,
Sep 27, 2012, 6:52:58 PM9/27/12
to googl...@googlegroups.com
Hello!

Any interest by the maintainers to add something like:
       public JsonWriter value( JsonElement value ) ...
to the JsonWriter ?
I'm working with some code where I have TypeAdapters for various classes
that I want to plug into a JsonStream ...
I'd be happy to throw together a patch if it would be considered.

What do you think ?

Cheers,
Reuben

Brandon Mintern

unread,
Sep 27, 2012, 7:02:07 PM9/27/12
to googl...@googlegroups.com
Although I agree that your suggestion would be convenient, in the
meantime you can use:

com.google.gson.internal.Streams.write(JsonElement value, JsonWriter out)
> --
> You received this message because you are subscribed to the Google Groups
> "google-gson" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-gson/-/4CSc-aJgt9gJ.
> 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.

Reuben

unread,
Sep 27, 2012, 7:14:16 PM9/27/12
to googl...@googlegroups.com
Thanks for the tip! :)

Reuben

unread,
Sep 27, 2012, 7:26:55 PM9/27/12
to googl...@googlegroups.com
Doh!
Actually - it looks like I would need something like this anyway to access my custom type adapters:
      JsonWriter value( JSonElement v, Type typeOfSrc,  Gson gsonTool )
bla.
Anyway - it looks like I can work around the problem by just calling JsonWriter.flush(),
then dropping down to the underlying java.io.Writer with
       gsonTool.toJson( v, MyClass.class, writer )

scala> val w = new java.io.StringWriter
w: java.io.StringWriter =

scala> val jw = new com.google.gson.stream.JsonWriter( w )
jw: com.google.gson.stream.JsonWriter = com.google.gson.stream.JsonWriter@1e32728a

scala> jw.beginObject.name( "foo" ).value( "fred" ).flush

scala> w.append( """ , "custom":"custom value" """ )
res18: java.io.StringWriter = {"foo":"fred" , "custom":"custom value"

scala> jw.name( "bla" ).value( "boogy" ).endObject.flush

scala> w
res20: java.io.StringWriter = {"foo":"fred" , "custom":"custom value" ,"bla":"boogy"}

Reuben

unread,
Sep 27, 2012, 8:01:44 PM9/27/12
to googl...@googlegroups.com
Ugh!
Ignore the first part of that last post - I'm thinking of something like this:

     jwriter.name( "foo" ).value( gsonTool.toJsonTree( myObj, MyClass.class ) )

Anyway - I'm on my way - sorry for spamming the group!  ;-)

Cheers,
Reuben

Jesse Wilson

unread,
Sep 27, 2012, 9:31:03 PM9/27/12
to googl...@googlegroups.com
You might prefer this:
  TypeAdapter<JsonElement> elementAdapter = gson.getTypeAdapter(JsonElement.class);
Then you can use that to do your serialization.

APIs in the 'internal' package are private APIs and can change from release to release.
Reply all
Reply to author
Forward
0 new messages