JamesDeCarlo / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Serializing and Deserializing Generic Types - toJson() works! #348

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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

Original issue reported on code.google.com by ray.may...@gmail.com on 22 Jul 2011 at 3:50

GoogleCodeExporter commented 9 years ago
Updated
What is the expected output? What do you see instead?
Expected output - runtime exception
Actual output - correct JSON string

OS - Windows XP

Original comment by ray.may...@gmail.com on 22 Jul 2011 at 3:53

GoogleCodeExporter commented 9 years ago
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.

Original comment by limpbizkit on 23 Jul 2011 at 4:49

GoogleCodeExporter commented 9 years ago
Here is the link 
https://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deseriali
zing-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

Original comment by ray.may...@gmail.com on 23 Jul 2011 at 4:55

GoogleCodeExporter commented 9 years ago
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

Original comment by maks....@gmail.com on 22 Nov 2011 at 4:51

GoogleCodeExporter commented 9 years ago
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".

Original comment by gvh...@gmail.com on 17 Dec 2013 at 9:09