actolap / json-io

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

In Java 7, the Throwable type contains java.util.Collections$UnmodifiableRandomAccessList, which does not allow a null constructor arg #14

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

String json = JsonWriter.objectToJson(new Exception("whatever"));
JsonReader.jsonToJava(json);

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

Expected output is an instance of the exception. The actual output is 
java.io.IOException: Could not instantiate 
java.util.Collections$UnmodifiableRandomAccessList using any constructor

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

version 2.2.30 on windows 7, 64 bit.

Please provide any additional information below.

This only seems to be an issue when moving to Java 7 (i've only tried update 40 
so far, which is the latest).

The issue is that the Throwable type has a SUPPRESSED_SENTINEL field of type 
java.util.Collections$UnmodifiableRandomAccessList, which only accepts a non 
null argument in its constructor. However, when JsonReader tries to create an 
instance and there's no default constructor, it attempts to pass in null 
arguments for non primitive constructor arguments. One solution would be to 
attempt both null and non null constructor arguments. These non null arguments 
would be replaced by the field setting reflection logic after the instance is 
created, so they shouldn't gunk up the object.

Original issue reported on code.google.com by EricKu...@gmail.com on 23 Sep 2013 at 5:49

GoogleCodeExporter commented 9 years ago
Here's a modified JsonReader that dumbly handles this particular case by 
creating a new ArrayList when it detects a constructor arg of type List, along 
with accompanying test TestJsonReaderWriter.testException. Several other common 
java types could be handled like this, although it's sort of plugging holes in 
the dike.

Original comment by EricKu...@gmail.com on 23 Sep 2013 at 9:10

Attachments:

GoogleCodeExporter commented 9 years ago
I will review and likely add / update with your changes.  I understand what you 
mean by "like plugging holes in the dike."  Java needs a C-style malloc() for 
objects, at least when you are performing serialization.   I will likely add 
some other "popular" classes (Collections, Map, String, Date, etc.) the usual 
suspects that end up as 'fields' on data transfer (model) classes.  Good find 
and thanks for providing a solution / test.

Original comment by jdereg@gmail.com on 24 Sep 2013 at 3:07

GoogleCodeExporter commented 9 years ago
Hi

I also have this issue on a simple pojo with JDK7

I have a simple bean with string name, int amount, int total and boolean 
accepted with a default empty constructor and getter and setter for each 
property.

I can serialise this type to a JSON String fine but when I try to deserialise I 
get the exact same exception as Erick.

I am trying to use the library in a REST service so when deserialising there 
can be an array of types needed by the java REST service and this library 
seemed just what I was looking for.

Any resolution to this would be greatly appreciated.

Regards

Steve

Original comment by steven.m...@gmail.com on 26 Sep 2013 at 10:52

GoogleCodeExporter commented 9 years ago
This issue has been addressed.  The code now tries the default constructor 
(public, private, protected, or package friendly).  If that does not work (or 
there is no constructor that takes no arguments, or the no-arg constructor 
throws an exception), then it tries construction with defaults for primitivies 
and null for non-primities, for all constructors.  If that does not work, it 
tries defaults for primitives, and non-null values for a wide range of common 
classes (Collection, String, Date, Timezone, etc).  A new test case has been 
added for this.

Original comment by jdereg@gmail.com on 27 Sep 2013 at 11:00