package com.fasterxml.jackson.databind.deser; import java.io.*; import java.util.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; /** * Test to check that customizations work as expected. */ @SuppressWarnings("serial") public class TestCustomFactory extends BaseMapTest { /* /********************************************************** /* Helper classes /********************************************************** */ static class DummyDeserializer<T> extends StdDeserializer<T> { final T value; public DummyDeserializer(T v, Class<T> cls) { super(cls); value = v; } @Override public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // need to skip, if structured... jp.skipChildren(); return value; } } static class TestBeans { public List<TestBean> beans; } static class TestBean { public CustomBean c; public String d; } @JsonDeserialize(using=CustomBeanDeserializer.class) static class CustomBean { protected final int a, b; public CustomBean(int a, int b) { this.a = a; this.b = b; } } static class CustomBeanDeserializer extends JsonDeserializer<CustomBean> { @Override public CustomBean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { int a = 0, b = 0; JsonToken t = jp.getCurrentToken(); if (t == JsonToken.START_OBJECT) { t = jp.nextToken(); } else if (t != JsonToken.FIELD_NAME) { throw new Error(); } while(t == JsonToken.FIELD_NAME) { final String fieldName = jp.getCurrentName(); t = jp.nextToken(); if (t != JsonToken.VALUE_NUMBER_INT) { throw new JsonParseException(jp, "expecting number got "+ t); } if (fieldName.equals("a")) { a = jp.getIntValue(); } else if (fieldName.equals("b")) { b = jp.getIntValue(); } else { throw new Error(); } t = jp.nextToken(); } return new CustomBean(a, b); } } /* /********************************************************** /* Unit tests /********************************************************** */ public void testCustomBeanDeserializer() throws Exception { final ObjectMapper map = new ObjectMapper(); String json = "{\"beans\":[{\"c\":{\"a\":10,\"b\":20},\"d\":\"hello, tatu\"}]}"; TestBeans beans = map.readValue(json, TestBeans.class); assertNotNull(beans); List<TestBean> results = beans.beans; assertNotNull(results); assertEquals(1, results.size()); TestBean bean = results.get(0); assertEquals("hello, tatu", bean.d); CustomBean c = bean.c; assertNotNull(c); assertEquals(10, c.a); assertEquals(20, c.b); json = "{\"beans\":[{\"c\":{\"b\":3,\"a\":-4},\"d\":\"\"}," +"{\"d\":\"abc\", \"c\":{\"b\":15}}]}"; beans = map.readValue(json, TestBeans.class); assertNotNull(beans); results = beans.beans; assertNotNull(results); assertEquals(2, results.size()); bean = results.get(0); assertEquals("", bean.d); c = bean.c; assertNotNull(c); assertEquals(-4, c.a); assertEquals(3, c.b); bean = results.get(1); assertEquals("abc", bean.d); c = bean.c; assertNotNull(c); assertEquals(0, c.a); assertEquals(15, c.b); } }