|
| 1 | +package com.fasterxml.jackson.databind.interop; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.JsonParser; |
| 6 | +import com.fasterxml.jackson.core.StreamReadCapability; |
| 7 | +import com.fasterxml.jackson.core.util.JacksonFeatureSet; |
| 8 | +import com.fasterxml.jackson.core.util.JsonParserDelegate; |
| 9 | +import com.fasterxml.jackson.databind.*; |
| 10 | + |
| 11 | +// Mostly for XML but can be tested via JSON with some trickery |
| 12 | +public class UntypedObjectWithDupsTest extends BaseMapTest |
| 13 | +{ |
| 14 | + private final ObjectMapper JSON_MAPPER = newJsonMapper(); |
| 15 | + |
| 16 | + // For [dataformat-xml#???] |
| 17 | + public void testDocWithDupsAsUntyped() throws Exception |
| 18 | + { |
| 19 | + _readWriteDocAs(Object.class); |
| 20 | + } |
| 21 | + |
| 22 | + // For [dataformat-xml#498] / [databind#3484] |
| 23 | + public void testDocWithDupsAsMap() throws Exception |
| 24 | + { |
| 25 | +// _readWriteDocAs(Map.class); |
| 26 | + } |
| 27 | + |
| 28 | + private <T> void _readWriteDocAs(Class<T> cls) throws Exception |
| 29 | + { |
| 30 | + final String doc = a2q( |
| 31 | + "{'hello': 'world',\n" |
| 32 | + + "'lists' : 1,\n" |
| 33 | + + "'lists' : 2,\n" |
| 34 | + + "'lists' : {\n" |
| 35 | + + " 'inner' : 'internal',\n" |
| 36 | + + " 'time' : 123\n" |
| 37 | + + "},\n" |
| 38 | + + "'lists' : 3,\n" |
| 39 | + + "'single' : 'one'\n" |
| 40 | + + "}"); |
| 41 | + // This is where need some trickery |
| 42 | + T value; |
| 43 | + try (JsonParser p = new WithDupsParser(JSON_MAPPER.createParser(doc))) { |
| 44 | + value = JSON_MAPPER.readValue(p, cls); |
| 45 | + } |
| 46 | + |
| 47 | + String json = JSON_MAPPER.writeValueAsString(value); |
| 48 | + assertEquals(a2q( |
| 49 | +"{'hello':'world','lists':[1,2," |
| 50 | ++"{'inner':'internal','time':123},3],'single':'one'}"), |
| 51 | + json); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Helper class to fake "DUPLICATE_PROPERTIES" on JSON parser |
| 56 | + * (which usually does not expose this). |
| 57 | + */ |
| 58 | + static class WithDupsParser extends JsonParserDelegate |
| 59 | + { |
| 60 | + public WithDupsParser(JsonParser p) { |
| 61 | + super(p); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public JacksonFeatureSet<StreamReadCapability> getReadCapabilities() { |
| 66 | + JacksonFeatureSet<StreamReadCapability> caps = super.getReadCapabilities(); |
| 67 | + caps = caps.with(StreamReadCapability.DUPLICATE_PROPERTIES); |
| 68 | + return caps; |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments