Menu

#44 Default transformers exception with null values

3.2.0
closed-fixed
nobody
None
5
2014-10-15
2014-02-12
vlastik
No

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);
    }

Discussion

  • vlastik

    vlastik - 2014-02-12

    Or better, without breaking the backwards compatibility:

        public void transform(Object value) {
            JSONContext context = getContext();
            if (value != null) {
                context.writeQuoted(getFormatter().format(value));
            } else {
                context.write(null);
            }
        }
    
     
  • Charlie Hubbard

    Charlie Hubbard - 2014-10-15
    • status: open --> closed-fixed
     

Log in to post a comment.

MongoDB Logo MongoDB