|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Iterator; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.*; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.databind.*; |
| 11 | +import com.fasterxml.jackson.databind.deser.*; |
| 12 | +import com.fasterxml.jackson.databind.deser.std.CollectionDeserializer; |
| 13 | +import com.fasterxml.jackson.databind.deser.std.DelegatingDeserializer; |
| 14 | +import com.fasterxml.jackson.databind.type.CollectionLikeType; |
| 15 | + |
| 16 | +public class NodeContext2049Test extends BaseMapTest |
| 17 | +{ |
| 18 | + public interface HasParent { |
| 19 | + void setParent(Parent parent); |
| 20 | + Parent getParent(); |
| 21 | + } |
| 22 | + |
| 23 | + static class Child implements HasParent { |
| 24 | + public Parent parent; |
| 25 | + public String property; |
| 26 | + |
| 27 | + public void setParent(Parent p) { parent = p; } |
| 28 | + public Parent getParent() { return parent; } |
| 29 | + } |
| 30 | + |
| 31 | + static class Parent { |
| 32 | + public List<Child> children; |
| 33 | + public Child singleChild; |
| 34 | + } |
| 35 | + |
| 36 | + static class ListValueInstantiator extends ValueInstantiator { |
| 37 | + @Override |
| 38 | + public String getValueTypeDesc() { |
| 39 | + return List.class.getName(); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public Object createUsingDefault(DeserializationContext ctxt) throws IOException { |
| 44 | + return new ArrayList<>(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + static class ParentSettingDeserializerModifier extends BeanDeserializerModifier { |
| 49 | + @Override |
| 50 | + public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanDescription beanDesc, |
| 51 | + BeanDeserializerBuilder builder) { |
| 52 | + for (Iterator<SettableBeanProperty> propertyIt = builder.getProperties(); propertyIt.hasNext(); ) { |
| 53 | + SettableBeanProperty property = propertyIt.next(); |
| 54 | + builder.addOrReplaceProperty(property.withValueDeserializer(new ParentSettingDeserializerContextual()), false); |
| 55 | + } |
| 56 | + return builder; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @SuppressWarnings("serial") |
| 61 | + static class ParentSettingDeserializer extends DelegatingDeserializer { |
| 62 | + public ParentSettingDeserializer(JsonDeserializer<?> delegatee) { |
| 63 | + super(delegatee); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { |
| 68 | + Object retValue = super.deserialize(jp, ctxt); |
| 69 | + if (retValue instanceof HasParent) { |
| 70 | + HasParent obj = (HasParent) retValue; |
| 71 | + Parent parent = null; |
| 72 | + JsonStreamContext parsingContext = jp.getParsingContext(); |
| 73 | + while (parent == null && parsingContext != null) { |
| 74 | + Object currentValue = parsingContext.getCurrentValue(); |
| 75 | + if (currentValue != null && currentValue instanceof Parent) { |
| 76 | + parent = (Parent) currentValue; |
| 77 | + } |
| 78 | + parsingContext = parsingContext.getParent(); |
| 79 | + } |
| 80 | + if (parent != null) { |
| 81 | + obj.setParent(parent); |
| 82 | + } |
| 83 | + } |
| 84 | + return retValue; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected JsonDeserializer<?> newDelegatingInstance(JsonDeserializer<?> newDelegatee) { |
| 89 | + return new ParentSettingDeserializer(newDelegatee); |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + static class ParentSettingDeserializerContextual extends JsonDeserializer<Object> implements ContextualDeserializer { |
| 95 | + |
| 96 | + @Override |
| 97 | + public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) |
| 98 | + throws JsonMappingException { |
| 99 | + JavaType propertyType = property.getType(); |
| 100 | + JavaType contentType = propertyType; |
| 101 | + if (propertyType.isCollectionLikeType()) { |
| 102 | + contentType = propertyType.getContentType(); |
| 103 | + } |
| 104 | + JsonDeserializer<Object> delegatee = ctxt.findNonContextualValueDeserializer(contentType); |
| 105 | + JsonDeserializer<Object> objectDeserializer = new ParentSettingDeserializer(delegatee); |
| 106 | + JsonDeserializer<?> retValue; |
| 107 | + if (propertyType.isCollectionLikeType()) { |
| 108 | + CollectionLikeType collectionType = ctxt.getTypeFactory().constructCollectionLikeType(propertyType.getRawClass(), |
| 109 | + contentType); |
| 110 | + ValueInstantiator instantiator = new ListValueInstantiator(); |
| 111 | + retValue = new CollectionDeserializer(collectionType, objectDeserializer, null, instantiator); |
| 112 | + } else { |
| 113 | + retValue = objectDeserializer; |
| 114 | + } |
| 115 | + return retValue; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { |
| 120 | + // TODO Auto-generated method stub |
| 121 | + return null; |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + /* |
| 127 | + /********************************************************************** |
| 128 | + /* Test methods |
| 129 | + /********************************************************************** |
| 130 | + */ |
| 131 | + |
| 132 | + private ObjectMapper objectMapper; |
| 133 | + { |
| 134 | + objectMapper = new ObjectMapper(); |
| 135 | + objectMapper.registerModule(new Module() { |
| 136 | + @Override |
| 137 | + public String getModuleName() { |
| 138 | + return "parentSetting"; |
| 139 | + } |
| 140 | + @Override |
| 141 | + public Version version() { |
| 142 | + return Version.unknownVersion(); |
| 143 | + } |
| 144 | + @Override |
| 145 | + public void setupModule(SetupContext context) { |
| 146 | + context.addBeanDeserializerModifier(new ParentSettingDeserializerModifier()); |
| 147 | + } |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + final static String JSON = "{\n" + |
| 152 | + " \"children\": [\n" + |
| 153 | + " {\n" + |
| 154 | + " \"property\": \"value1\"\n" + |
| 155 | + " },\n" + |
| 156 | + " {\n" + |
| 157 | + " \"property\": \"value2\"\n" + |
| 158 | + " }\n" + |
| 159 | + " ],\n" + |
| 160 | + " \"singleChild\": {\n" + |
| 161 | + " \"property\": \"value3\"\n" + |
| 162 | + " }\n" + |
| 163 | + "}"; |
| 164 | + |
| 165 | + public void testReadNoBuffering() throws IOException { |
| 166 | + Parent obj = objectMapper.readerFor(Parent.class).readValue(JSON); |
| 167 | + assertSame(obj, obj.singleChild.getParent()); |
| 168 | + for (Child child : obj.children) { |
| 169 | + assertSame(obj, child.getParent()); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + public void testReadFromTree() throws IOException { |
| 174 | + JsonNode tree = objectMapper.readTree(JSON); |
| 175 | + Parent obj = objectMapper.reader().forType(Parent.class).readValue(tree); |
| 176 | + assertSame(obj, obj.singleChild.getParent()); |
| 177 | + for (Child child : obj.children) { |
| 178 | + assertSame(obj, child.getParent()); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments