Skip to content

overrideStandardValueWriter only applied to first java.nio.file.Path valued field of bean #112

@jhonnen

Description

@jhonnen

The overrideStandardValueWriter mechanism seems to work only for the first field of the given type for a Bean. Here's a failing test case that demonstrates the issue (can be added to ValueWriterModifierTest):

public void testMultipleFields() throws Exception {

    TestBean input = new TestBean();
    input.p1 = Paths.get("some/path");
    input.p2 = Paths.get("some/other/path");

    String json = JSON.builder()
            .register(new JacksonJrExtension() {
                @Override
                protected void register(ExtensionContext ctxt) {
                    ctxt.insertModifier(new ReaderWriterModifier() {
                        @Override
                        public ValueWriter overrideStandardValueWriter(JSONWriter writeContext, Class<?> type, int stdTypeId) {
                            if (type == Path.class) {
                                return new PathWriter();
                            }
                            return super.overrideStandardValueWriter(writeContext, type, stdTypeId);
                        }

                    });

                    ctxt.insertProvider(new ReaderWriterProvider() {
                        @Override
                        public ValueWriter findValueWriter(JSONWriter writeContext, Class<?> type) {
                            if (Path.class.isAssignableFrom(type)) {
                                return new PathWriter();
                            }
                            return super.findValueWriter(writeContext, type);
                        }
                    });
                }
            }).build().asString(input);
    assertEquals(aposToQuotes("{'p1':'some/path','p2':'some/other/path'}"), json);
}

private static class TestBean {
    public Path p1;
    public Path p2;
}

private static class PathWriter implements ValueWriter {
    @Override
    public void writeValue(JSONWriter context, JsonGenerator g, Object value) throws IOException {
        g.writeString(((Path) value).toString().replace(File.separatorChar, '/'));
    }

    @Override
    public Class<?> valueType() {
        return Path.class;
    }
}

The test fails with

junit.framework.ComparisonFailure: 
Expected :{"p1":"some/path","p2":"some/other/path"}
Actual   :{"p1":"some/path","p2":["some","other","path"]}

--> on the second Path field, the Iterable serialization wrongly has precedence over my configured writer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions