Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra ',' in generated JSON if all attributes are filtered #381

Open
creinig opened this issue Jan 29, 2018 · 0 comments
Open

Extra ',' in generated JSON if all attributes are filtered #381

creinig opened this issue Jan 29, 2018 · 0 comments

Comments

@creinig
Copy link

creinig commented Jan 29, 2018

Scenario:

  • class "Outer" contains field of type "Inner" (interface, with concrete instance "InnerImpl")
  • InnerImpl contains one field
  • The serializer is configured with a custom filter which filters out that field

This causes InstanceSerializerImpl#serializeSubtypeInstance() to insert a "class" attribute into the JSON stream for InnerImpl, followed by a comma. Since InnerImpl does contain fields, that comma is not removed. But since all fields are filtered, the comma is not followed by another field either. => The generated JSON is invalid:
{"inner":{"class":"com.iisy.solvatio.common.util.BoonIssueTest$InnerImpl",}}

Problematic code location:

Unit test to reproduce the problem:

package com.iisy.solvatio.common.util;

import static org.junit.Assert.assertFalse;

import org.boon.core.reflection.fields.FieldAccess;
import org.boon.json.JsonSerializer;
import org.boon.json.JsonSerializerFactory;
import org.boon.json.serializers.FieldFilter;
import org.boon.primitive.CharBuf;
import org.junit.Test;

public class BoonIssueTest {

    public static class Outer {
        private Inner inner;

        public Inner getInner() {
            return inner;
        }

        public void setInner(Inner inner) {
            this.inner = inner;
        }
    }

    public static interface Inner {

    }

    public static class InnerImpl implements Inner {
        private String implementationDetail = "Psst";

        public String getImplementationDetail() {
            return implementationDetail;
        }

        public void setImplementationDetail(String implementationDetail) {
            this.implementationDetail = implementationDetail;
        }

    }

    public class InternalsFilter implements FieldFilter {
        @Override
        public boolean include(final Object parent, final FieldAccess fieldAccess) {
            if (fieldAccess.name().equals("implementationDetail")) {
                return false;
            }
            return true;
        }

    }

    @Test
    public void emptySubtype() {
        Outer obj = new Outer();
        obj.setInner(new InnerImpl());
        JsonSerializerFactory factory = new JsonSerializerFactory().addFilter(new InternalsFilter());
        JsonSerializer serializer = factory.create();

        CharBuf serialized = serializer.serialize(obj);
        System.out.println(serialized);
        assertFalse(serialized.toString().contains(",}"));
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant