-
Notifications
You must be signed in to change notification settings - Fork 36
Closed
Description
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
Labels
No labels