|
| 1 | +package com.fasterxml.jackson.jr.failing; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.nio.file.Path; |
| 6 | +import java.nio.file.Paths; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.jr.ob.*; |
| 11 | +import com.fasterxml.jackson.jr.ob.api.*; |
| 12 | +import com.fasterxml.jackson.jr.ob.impl.JSONWriter; |
| 13 | + |
| 14 | +public class ValueWriterModifier112Test extends TestBase |
| 15 | +{ |
| 16 | + static class TestBean112 { |
| 17 | + public Path p1; |
| 18 | + public Path p2; |
| 19 | + } |
| 20 | + |
| 21 | + static class PathWriter implements ValueWriter { |
| 22 | + @Override |
| 23 | + public void writeValue(JSONWriter context, JsonGenerator g, Object value) throws IOException { |
| 24 | + g.writeString(((Path) value).toString().replace(File.separatorChar, '/')); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public Class<?> valueType() { |
| 29 | + return Path.class; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // [jackson-jr#112] |
| 34 | + public void testMultipleFieldOverrides() throws Exception |
| 35 | + { |
| 36 | + TestBean112 input = new TestBean112(); |
| 37 | + input.p1 = Paths.get("some/path"); |
| 38 | + input.p2 = Paths.get("some/other/path"); |
| 39 | + |
| 40 | + JSON writer = JSON.builder() |
| 41 | + .register(new JacksonJrExtension() { |
| 42 | + @Override |
| 43 | + protected void register(ExtensionContext ctxt) { |
| 44 | + ctxt.insertModifier(new ReaderWriterModifier() { |
| 45 | + @Override |
| 46 | + public ValueWriter overrideStandardValueWriter(JSONWriter writeContext, Class<?> type, int stdTypeId) { |
| 47 | + if (type == Path.class) { |
| 48 | + return new PathWriter(); |
| 49 | + } |
| 50 | + return super.overrideStandardValueWriter(writeContext, type, stdTypeId); |
| 51 | + } |
| 52 | + }); |
| 53 | + ctxt.insertProvider(new ReaderWriterProvider() { |
| 54 | + @Override |
| 55 | + public ValueWriter findValueWriter(JSONWriter writeContext, Class<?> type) { |
| 56 | + if (Path.class.isAssignableFrom(type)) { |
| 57 | + return new PathWriter(); |
| 58 | + } |
| 59 | + return super.findValueWriter(writeContext, type); |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + }).build(); |
| 64 | + String json = writer.asString(input); |
| 65 | + assertEquals(a2q("{'p1':'some/path','p2':'some/other/path'}"), json); |
| 66 | + } |
| 67 | +} |
0 commit comments