boonproject / boon

Simple opinionated Java for the novice to expert level Java Programmer. Low Ceremony. High Productivity.
http://richardhightower.github.io/site/Boon/Welcome.html
Apache License 2.0
520 stars 102 forks source link

JsonMapper not working with nulls in lists properly #343

Closed RichardHightower closed 8 years ago

RichardHightower commented 8 years ago
Anil    1:48 PM
QBit.factory().createJsonMapper().toJson(object) .. will add [,,,,,123,,13]
JsonFactory.toJson(object) will add [null, null, null, null, 123, null, 13]
how do i get the same behavior as second one using QBit.factory jsonmapper

Rick Hightower  1:50 PM
ok
let me write a unit test and try to reproduce this
Anil    1:50 PM
ok. awesome!

Rick Hightower  1:54 PM
ok JsonFactory uses the lax parser but QBit use the strict one and the bug seems to be in the strict parser.
    @Test
    public void badSerializer() throws Exception {

        String json = QBit.factory().createJsonMapper().toJson(Lists.list(1, 2, null, 3));

        puts(json);

        json = JsonFactory.toJson(Lists.list(1, 2, null, 3));

        puts(json);

    }
Show less
[1,2,,3]

[1,2,null,3]
Anil    1:56 PM
hmm. I don't have the control if use Callback<MyObject> then in the endpoint.
Then need to do Callback<HttpTextResponse>
Any other way to tell QBit factory use the lax one?

Rick Hightower  1:59 PM
ok
it has to do with JsonSimpleSerializerImpl vs BasicObjectSerializerImpl
here is SimpleSerializer
    public final void serializeCollection( Collection<?> collection, CharBuf builder )  {

        if ( collection.size () == 0 ) {
             builder.addChars ( EMPTY_LIST_CHARS );
             return;
        }

        builder.addChar( '[' );
        for ( Object o : collection ) {
            if (o == null) {
                builder.addNull();
            } else {
                serializeObject(o, builder);
            }
            builder.addChar ( ',' );

        }
        builder.removeLastChar ();
        builder.addChar( ']' );

    }
Show more
Rick Hightower  2:04 PM
here is the wrong one
    @Override
    public final void serializeCollection ( JsonSerializerInternal serializer, Collection<?> collection, CharBuf builder ) {
        if ( collection.size () == 0 ) {
            builder.addChars ( EMPTY_LIST_CHARS );
            return;
        }

        builder.addChar( '[' );
        for ( Object o : collection ) {
            serializer.serializeObject ( o, builder );
            builder.addChar ( ',' );
        }
        builder.removeLastChar ();
        builder.addChar( ']' );

    }
Show more
Anil    2:05 PM
hmm.. looking at it.
Rick Hightower  2:06 PM
it is missing the addNull
 if (o == null) {
                builder.addNull();
            } else {
                serializeObject(o, builder);
            }
            builder.addChar ( ',' );
Show more
Anil    2:06 PM
I see
ok its in CollectionSerializerImpl
Anil    2:12 PM
Any workaround that you can think of?
like using our own custom CollectionSerializer?
Rick Hightower  2:13 PM
I will get a new release to you with a fix in the pub repo in a few hours.. and snapshot fix in a few minutes.
RichardHightower commented 8 years ago

This was fixed over here.

https://github.com/advantageous/boon/issues/7