FranzGames / google-gson

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

Provide object-model location in exceptions #395

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Try to serialize an object that has circular references

What is the expected output? What do you see instead?
A message that tells me the exact point that caused my exception to be 
triggered(the method createDetailedException of CircularReferenceException 
receives a FieldAttributes as argument that makes possible to create a much 
more rich and intuitive message than just the name of the field like the class 
that own the field that caused it).

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

Original issue reported on code.google.com by Jayrmo...@gmail.com on 28 Dec 2011 at 9:11

GoogleCodeExporter commented 9 years ago
We should go even further. Every error message should include a full path in 
the object model. Like "customers[3].homeAddress.street[0]" or similar. It 
wouldn't be too much work to include this state in JsonReader.

Original comment by limpbizkit on 29 Dec 2011 at 5:28

GoogleCodeExporter commented 9 years ago
Issue 160 has been merged into this issue.

Original comment by limpbizkit on 29 Dec 2011 at 6:03

GoogleCodeExporter commented 9 years ago
Issue 335 has been merged into this issue.

Original comment by limpbizkit on 29 Dec 2011 at 7:29

GoogleCodeExporter commented 9 years ago
Can I contribute with this enhancements to the project?

Original comment by Jayrmo...@gmail.com on 29 Dec 2011 at 11:16

GoogleCodeExporter commented 9 years ago
Jaymotta: Would be great if you can post some source-code that outlines 
suggestions on how to do this.

Original comment by inder123 on 31 Dec 2011 at 9:53

GoogleCodeExporter commented 9 years ago
http://paste.ideaslabs.com/show/0FJICsG4xc

Original comment by Jayrmo...@gmail.com on 6 Jan 2012 at 12:01

GoogleCodeExporter commented 9 years ago
We want this at Square.

Original comment by limpbizkit on 21 Sep 2012 at 8:39

GoogleCodeExporter commented 9 years ago
Would have been very helpful for this crash:

ConcurrentModificationException
    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:95)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    at com.google.gson.Gson.toJson(Gson.java:586)
    at com.google.gson.Gson.toJson(Gson.java:565)
    at com.google.gson.Gson.toJson(Gson.java:520)
    at com.google.gson.Gson.toJson(Gson.java:500)

Original comment by limpbizkit on 12 Nov 2012 at 12:13

GoogleCodeExporter commented 9 years ago
Issue 476 has been merged into this issue.

Original comment by limpbizkit on 4 Feb 2013 at 3:31

GoogleCodeExporter commented 9 years ago
As a work around, is it possible to get GSON to log its traversal of the object 
tree as it generates JSON? I just had to debug a circular reference buried 
within in a large object model and the only way I could find the problem was by 
attaching a debugger, which was very inconvenient in my case.

Original comment by ja...@planticle.com.au on 15 Jul 2013 at 5:34

GoogleCodeExporter commented 9 years ago
I think we should do this, and we should use JsonPath to pinpoint the location 
in the document of the failure:

"Expected a boolean but was NUMBER at line 1 column 37: 
$.payments[2].customers[1].has_email_preference"

Original comment by limpbizkit on 26 Mar 2014 at 6:05

GoogleCodeExporter commented 9 years ago
http://code.google.com/p/google-gson/source/detail?r=1286

Original comment by jwil...@squareup.com on 31 Jul 2014 at 5:20

GoogleCodeExporter commented 9 years ago
Hi, 
I've tried the SVN HEAD version (r1288) and the issue is still present.
In my case the exception is thrown from JsonTreeReader.java without any 
information about object-model location.

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected 
BEGIN_ARRAY but was STRING
at 
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(Reflecti
veTypeAdapterFactory.java:183)
at 
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRunt
imeTypeWrapper.java:40)
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72)
at 
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRunt
imeTypeWrapper.java:40)
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72)
at 
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveType
AdapterFactory.java:93)
at 
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(Reflecti
veTypeAdapterFactory.java:179)
at 
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveType
AdapterFactory.java:93)
at 
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(Reflecti
veTypeAdapterFactory.java:179)
at com.google.gson.Gson.fromJson(Gson.java:805)
at com.google.gson.Gson.fromJson(Gson.java:870)
at com.google.gson.Gson.fromJson(Gson.java:843)

Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
at com.google.gson.internal.bind.JsonTreeReader.expect(JsonTreeReader.java:139)
at 
com.google.gson.internal.bind.JsonTreeReader.beginArray(JsonTreeReader.java:58)
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70)
at 
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveType
AdapterFactory.java:93)
at 
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(Reflecti
veTypeAdapterFactory.java:179)
... 26 more

Best regards,
    Alex

Original comment by zilberk...@gmail.com on 3 Aug 2014 at 2:26

GoogleCodeExporter commented 9 years ago
Yup, Alex you're right. This is only implemented for stream-backed JsonReader. 
I still need to make it work for the JsonElement-backed version.

Original comment by limpbizkit on 3 Aug 2014 at 3:05