@JSON(objectFactory=) annotation ignored
Brought to you by:
charliehubbard
Hello,
When I use the annoation
@JSON(objectFactory=MyTransformer.class, transformer=MyTransformer.class)
the transformer is used at time of serialization but the objectFactory is ignored at time of deserialization
Here is a sample application:
import java.lang.reflect.Type;
import flexjson.JSON;
import flexjson.JSONContext;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import flexjson.ObjectBinder;
import flexjson.ObjectFactory;
import flexjson.transformer.Transformer;
public class FlexjsonTest {
@JSON(objectFactory=MyTransformer.class, transformer=MyTransformer.class)
private String test;
public FlexjsonTest() {
}
@JSON(objectFactory=MyTransformer.class, transformer=MyTransformer.class)
public void setTest(String test) {
this.test = test;
}
@JSON(objectFactory=MyTransformer.class, transformer=MyTransformer.class)
public String getTest() {
return this.test;
}
public static void main(String[] args) {
FlexjsonTest a = new FlexjsonTest();
a.setTest("ABC");
System.out.println("a.getTest() - "+a.getTest());
String json = new JSONSerializer().deepSerialize(a);
System.out.println(json);
FlexjsonTest b = new JSONDeserializer<FlexjsonTest>().deserialize(json);
System.out.println("b.getTest() - "+b.getTest());
}
public static class MyTransformer implements ObjectFactory, Transformer {
@Override
public Object instantiate(ObjectBinder context, Object value, Type targetType, @SuppressWarnings("rawtypes") Class targetClass) {
return "ABC";
}
@Override
public void transform(Object object) {
JSONContext.get().writeQuoted("123");
}
}
}
the transformer did work as expected and the value written to the Json is "123" (instead of "ABC" which is stored on the object) - but when deserializing the object the ObjectFactory referenced in the annotation is not used - instead the built in object factory is used. The output of this application is the following:
a.getTest() - ABC
{"class":"com.alpvue.hms.test.FlexjsonTest","test":"123"}
b.getTest() - 123
Is this project supported?
Proposed fix for 3.3:
--- ../../Sources/flexjson-3.3/flexjson/ObjectBinder.java (revision )
+++ ../../Sources/flexjson-3.3/flexjson/ObjectBinder.java (revision )
@@ -146,6 +146,7 @@
Type[] types = setMethod.getGenericParameterTypes();
if( types.length == 1 ) {
Type paramType = types[0];
setMethod.invoke( objectStack.getLast(), bind( value, resolveParameterizedTypes( paramType, targetType ) ) );
} else {
throw new JSONException(currentPath + ": Expected a single parameter for method " + target.getClass().getName() + "." + setMethod.getName() + " but got " + types.length );
@@ -154,6 +155,7 @@
Field field = descriptor.getProperty();
if( field != null ) {
field.setAccessible( true );
value = convertValue(value, descriptor);
field.set( target, bind( value, field.getGenericType() ) );
}
}
@@ -169,6 +171,18 @@
}
}
private Object convertValue(Object value, BeanProperty property) {
+
public JSONException cannotConvertValueToTargetType(Object value, Class targetType) {
return new JSONException( String.format("%s: Can not convert %s into %s", currentPath, value.getClass().getName(), targetType.getName() ) );
}
Last edit: Dmitry Parhonin 2015-12-03