...
protected String prop1;
protected Long prop2;
protected Integer myInt;
public void setMyInt(Integer myInt) {
this.myInt = myInt;
}
public void setProp2(Long prop2) {
this.prop2 = prop2;
}
public void setProp1(String prop1) {
this.prop1 = prop1;
}
...
When I try to deserialize:
MyClass mc = new JSONDeserializer<MyClass>().use( null, MyClass.class ).deserialize(jsonString);
I got various exceptions, like:
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at flexjson.ObjectBinder.bindObject(ObjectBinder.java:300)
at flexjson.ObjectBinder.bindObject(ObjectBinder.java:152)
at flexjson.ObjectBinder.bind(ObjectBinder.java:125)
at flexjson.JSONDeserializer.deserialize(JSONDeserializer.java:121)
flexjson.JSONException: [ prop1 ]: Can not convert java.lang.String into java.lang.Class
at flexjson.ObjectBinder.cannotConvertValueToTargetType(ObjectBinder.java:174)
at flexjson.factories.IntegerObjectFactory.instantiate(IntegerObjectFactory.java:13)
at flexjson.ObjectBinder.bind(ObjectBinder.java:90)
at flexjson.ObjectBinder.bindIntoObject(ObjectBinder.java:143)
at flexjson.factories.ClassLocatorObjectFactory.instantiate(ClassLocatorObjectFactory.java:38)
at flexjson.ObjectBinder.bind(ObjectBinder.java:90)
at flexjson.ObjectBinder.bind(ObjectBinder.java:69)
at flexjson.JSONDeserializer.deserialize(JSONDeserializer.java:150)
but with this patch, it got converted successfully.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
empty string,and number handling
Can you explain what problem this patch fixes in more detail? Can you give the input, the expected output, and the actual output?
I have a json like this:
String jsonString = "{\"prop1\":\"\",\"prop2\":\"null\",\"myInt\":\"0\"}";
A class like this:
...
protected String prop1;
protected Long prop2;
protected Integer myInt;
public void setMyInt(Integer myInt) {
this.myInt = myInt;
}
public void setProp2(Long prop2) {
this.prop2 = prop2;
}
public void setProp1(String prop1) {
this.prop1 = prop1;
}
...
When I try to deserialize:
MyClass mc = new JSONDeserializer<MyClass>().use( null, MyClass.class ).deserialize(jsonString);
I got various exceptions, like:
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at flexjson.ObjectBinder.bindObject(ObjectBinder.java:300)
at flexjson.ObjectBinder.bindObject(ObjectBinder.java:152)
at flexjson.ObjectBinder.bind(ObjectBinder.java:125)
at flexjson.JSONDeserializer.deserialize(JSONDeserializer.java:121)
flexjson.JSONException: [ prop1 ]: Can not convert java.lang.String into java.lang.Class
at flexjson.ObjectBinder.cannotConvertValueToTargetType(ObjectBinder.java:174)
at flexjson.factories.IntegerObjectFactory.instantiate(IntegerObjectFactory.java:13)
at flexjson.ObjectBinder.bind(ObjectBinder.java:90)
at flexjson.ObjectBinder.bindIntoObject(ObjectBinder.java:143)
at flexjson.factories.ClassLocatorObjectFactory.instantiate(ClassLocatorObjectFactory.java:38)
at flexjson.ObjectBinder.bind(ObjectBinder.java:90)
at flexjson.ObjectBinder.bind(ObjectBinder.java:69)
at flexjson.JSONDeserializer.deserialize(JSONDeserializer.java:150)
but with this patch, it got converted successfully.
Thank you for the clarification and the patch! I will look it over.
View and moderate all "patches Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Patches"
the NumberFactory patch is nice
i believe it make sense to convert String to number, this is quite useful for deserialization
Ok this patch has been applied. The fixes will be included in the next release which is 2.1.