Issue 348 in google-gson: Serializing and Deserializing Generic Types - toJson() works!

15 views
Skip to first unread message

googl...@googlecode.com

unread,
Jul 22, 2011, 11:54:40 AM7/22/11
to google-gson...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 348 by ray.may...@gmail.com: Serializing and Deserializing
Generic Types - toJson() works!
http://code.google.com/p/google-gson/issues/detail?id=348

What steps will reproduce the problem?
1. Write a vanilla java class and declare a field of type List<String>
2. Populate the list with some arbitrary string values and call toJson()
3. It serializes the object and generates a correctly formatted JSON string

What is the expected output? What do you see instead?
It should throw a runtime exception as per the user guide

What version of the product are you using? On what operating system?
1.7.1

Please provide any additional information below.
Either something is wrong with my code / the user guide needs to be updated

googl...@googlecode.com

unread,
Jul 22, 2011, 11:58:52 AM7/22/11
to google-gson...@googlegroups.com

Comment #1 on issue 348 by ray.may...@gmail.com: Serializing and
Deserializing Generic Types - toJson() works!
http://code.google.com/p/google-gson/issues/detail?id=348

Updated


What is the expected output? What do you see instead?

Expected output - runtime exception
Actual output - correct JSON string

OS - Windows XP


googl...@googlecode.com

unread,
Jul 23, 2011, 12:49:45 AM7/23/11
to google-gson...@googlegroups.com

Comment #2 on issue 348 by limpbiz...@gmail.com: Serializing and
Deserializing Generic Types - toJson() works!
http://code.google.com/p/google-gson/issues/detail?id=348

You're reporting that GSON works too well? Why don't I fix the docs that
claim we don't support generics. Please point me at these docs.

googl...@googlecode.com

unread,
Jul 23, 2011, 12:55:45 AM7/23/11
to google-gson...@googlegroups.com

Comment #3 on issue 348 by ray.may...@gmail.com: Serializing and
Deserializing Generic Types - toJson() works!
http://code.google.com/p/google-gson/issues/detail?id=348

Here is the link
https://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Gener


And the following works for me (contrary to what the doc says)

List<String> myStrings = new List<String>();
gson.toJson(myStrings); // Will cause a runtime exception


googl...@googlecode.com

unread,
Nov 22, 2011, 11:51:28 AM11/22/11
to google-gson...@googlegroups.com

Comment #4 on issue 348 by maks....@gmail.com: Serializing and
Deserializing Generic Types - toJson() works!
http://code.google.com/p/google-gson/issues/detail?id=348

The behaviour of lib is as per documentation. I feel u are confused between
serialization and deseralization.
I quto from docs ....."You can serialize the collection with Gson without
doing anything specific: toJson(collection) would write out the desired
output.
However, deserialization with fromJson(json, Collection.class) will not
work since Gson has no way of knowing how to map the input to the
types. " ..

What u are doing is serialization . Docs talk of deserialization

googl...@googlecode.com

unread,
Dec 17, 2013, 4:09:44 PM12/17/13
to google-gson...@googlegroups.com

Comment #5 on issue 348 by gvh...@gmail.com: Serializing and Deserializing
I too felt bit worried when i am using gson.toJson(obj); for both
non-generic and generic object.The document saying "May not serialize
foo.value correctly" in generic type serialization section buts its working
fine for most of the examples i have tried. So can some one please let me
know " Do serializing both generic and non-generic type using
gson.toJson(obj) cause any trouble in any situation".

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

googl...@googlecode.com

unread,
May 12, 2015, 1:40:28 PM5/12/15
to google-gson...@googlegroups.com

Comment #6 on issue 348 by rhd...@gmail.com: Serializing and Deserializing
Generic Types - toJson() works!
https://code.google.com/p/google-gson/issues/detail?id=348

RE "However, deserialization with fromJson(json, Collection.class) will not
work since Gson has no way of knowing how to map the input to the types. "

If you control the json objects coming in (i.e. its your source), you can
add a field like java_class_name that contains the name of the Java class
it should be mapped to. Then a deserialization adapter can be easily
written for the Abstract class coming in, then looks at the java_class_name
field and deserializes it accordingly. We are doing this and it turned out
to be surprisingly easy and effective to deserialize hetrogeneous array.
Reply all
Reply to author
Forward
0 new messages