Default transformers exception with null values
Brought to you by:
charliehubbard
Hello,
in bug #39 there was a change in the transformer processing of the null values. But this fix has broken the transformers, as they have to deal withthe null values now.
You should probably update the documentation to reflect that. And the builtin transformers - as DateTransformer, should be fixed as well, as they are generating flexjson.JSONException for the null values.
Example:
public class date {
public class TestData {
public Date null_date = null;
public Date notnull_date = new Date();
}
@Test
public void testDateTransforming() {
TestData data = new TestData();
// flexjson.JSONException: [ null_date ]: Error while trying to deepSerialize.
System.out.println(new JSONSerializer().transform(new DateTransformer("dd.MM.yyyy HH:mm"), "notnull_date").serialize(data));
// OK
System.out.println(new JSONSerializer().transform(new DateTransformer("dd.MM.yyyy HH:mm"), "null_date").serialize(data));
}
}
Possible fix:
public void transform(Object value) {
String result = "";
if (value != null) {
result = getFormatter().format(value);
}
getContext().writeQuoted(result);
}
Or better, without breaking the backwards compatibility: