• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.contextual;
2 
3 import java.io.IOException;
4 
5 import com.fasterxml.jackson.core.*;
6 import com.fasterxml.jackson.databind.*;
7 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
8 import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
9 
10 public class TestContextAttributeWithDeser extends BaseMapTest
11 {
12     final static String KEY = "foobar";
13 
14     @SuppressWarnings("serial")
15     static class PrefixStringDeserializer extends StdScalarDeserializer<String>
16     {
PrefixStringDeserializer()17         protected PrefixStringDeserializer() {
18             super(String.class);
19         }
20 
21         @Override
deserialize(JsonParser jp, DeserializationContext ctxt)22         public String deserialize(JsonParser jp, DeserializationContext ctxt)
23             throws IOException
24         {
25             Integer I = (Integer) ctxt.getAttribute(KEY);
26             if (I == null) {
27                 I = Integer.valueOf(0);
28             }
29             int i = I.intValue();
30             ctxt.setAttribute(KEY, Integer.valueOf(i + 1));
31             return jp.getText()+"/"+i;
32         }
33 
34     }
35 
36     static class TestPOJO
37     {
38         @JsonDeserialize(using=PrefixStringDeserializer.class)
39         public String value;
40     }
41 
42     /*
43     /**********************************************************
44     /* Test methods
45     /**********************************************************
46      */
47 
48     final ObjectMapper MAPPER = sharedMapper();
49 
testSimplePerCall()50     public void testSimplePerCall() throws Exception
51     {
52         final String INPUT = aposToQuotes("[{'value':'a'},{'value':'b'}]");
53         TestPOJO[] pojos = MAPPER.readerFor(TestPOJO[].class).readValue(INPUT);
54         assertEquals(2, pojos.length);
55         assertEquals("a/0", pojos[0].value);
56         assertEquals("b/1", pojos[1].value);
57 
58         // and verify that state does not linger
59         TestPOJO[] pojos2 = MAPPER.readerFor(TestPOJO[].class).readValue(INPUT);
60         assertEquals(2, pojos2.length);
61         assertEquals("a/0", pojos2[0].value);
62         assertEquals("b/1", pojos2[1].value);
63     }
64 
testSimpleDefaults()65     public void testSimpleDefaults() throws Exception
66     {
67         final String INPUT = aposToQuotes("{'value':'x'}");
68         TestPOJO pojo = MAPPER.readerFor(TestPOJO.class)
69                 .withAttribute(KEY, Integer.valueOf(3))
70                 .readValue(INPUT);
71         assertEquals("x/3", pojo.value);
72 
73         // as above, should not carry on state
74         TestPOJO pojo2 = MAPPER.readerFor(TestPOJO.class)
75                 .withAttribute(KEY, Integer.valueOf(5))
76                 .readValue(INPUT);
77         assertEquals("x/5", pojo2.value);
78     }
79 
testHierarchic()80     public void testHierarchic() throws Exception
81     {
82         final String INPUT = aposToQuotes("[{'value':'x'},{'value':'y'}]");
83         ObjectReader r = MAPPER.readerFor(TestPOJO[].class).withAttribute(KEY, Integer.valueOf(2));
84         TestPOJO[] pojos = r.readValue(INPUT);
85         assertEquals(2, pojos.length);
86         assertEquals("x/2", pojos[0].value);
87         assertEquals("y/3", pojos[1].value);
88 
89         // and once more to verify transiency of per-call state
90         TestPOJO[] pojos2 = r.readValue(INPUT);
91         assertEquals(2, pojos2.length);
92         assertEquals("x/2", pojos2[0].value);
93         assertEquals("y/3", pojos2[1].value);
94     }
95 }
96