{ "items": [ { "name": "foo" } ]}
static class Container { Collection<Item> items;
public Collection<Item> getItems() { return items; }
public void setItems(Collection<Item> items) { this.items = items; }}
static class Item {
@JsonDeserialize(using = ItemNameDeserializer.class) String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }}
static class ItemNameDeserializer extends StdDeserializer<String> { private static final long serialVersionUID = 1L;
public ItemNameDeserializer() { super(String.class); }
@Override public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { debugOutContext(p.getParsingContext()); return p.readValueAs(String.class); }
private void debugOutContext(JsonStreamContext ctx) { System.out.println("Context: " + ctx.hashCode() + ",\tcurName=" + ctx.getCurrentName() + ",\tcurIndex=" + ctx.getCurrentIndex() +",\ttype=" + ctx.getTypeDesc() + ",\tcurValue=" + ctx.getCurrentValue()); if (ctx.getParent() != null) { debugOutContext(ctx.getParent()); } } }
ObjectMapper mapper = new ObjectMapper();
Container container = new Container();Item item = new Item();item.name = "foo";container.items = new ArrayList<>();container.items.add(item);
String json = mapper.writeValueAsString(container);Container containerMashalled = mapper.readValue(json, Container.class);
Context: 486898233, curName=name, curIndex=0, type=OBJECT, curValue=ch.pbz.jackson.Test$Item@26be92adContext: 1282473384, curName=null, curIndex=0, type=ARRAY, curValue=[]Context: 575593575, curName=items, curIndex=0, type=OBJECT, curValue=ch.pbz.jackson.Test$Container@14acaea5Context: 1188392295, curName=null, curIndex=0, type=ROOT, curValue=null
ObjectMapper mapper = new ObjectMapper();JsonParser jsonParser = mapper.getFactory().createParser(json);JsonNode doc = jsonParser.readValueAsTree();JsonParser traverse = doc.traverse(jsonParser.getCodec());Container container = mapper.readValue(traverse, Container.class);
Context: 466505482, curName=name, curIndex=0, type=OBJECT, curValue=ch.pbz.jackson.Test$Item@5e3a8624Context: 1547425104, curName=null, curIndex=0, type=ARRAY, curValue=nullContext: 152134087, curName=items, curIndex=0, type=OBJECT, curValue=[]