• 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.JsonGenerator;
6 import com.fasterxml.jackson.databind.*;
7 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
8 import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
9 
10 public class TestContextAttributeWithSer extends BaseMapTest
11 {
12     final static String KEY = "foobar";
13 
14     @SuppressWarnings("serial")
15     static class PrefixStringSerializer extends StdScalarSerializer<String>
16     {
PrefixStringSerializer()17         protected PrefixStringSerializer() {
18             super(String.class);
19         }
20 
21         @Override
serialize(String value, JsonGenerator jgen, SerializerProvider provider)22         public void serialize(String value, JsonGenerator jgen,
23                 SerializerProvider provider)
24             throws IOException
25         {
26             Integer I = (Integer) provider.getAttribute(KEY);
27             if (I == null) {
28                 I = Integer.valueOf(0);
29             }
30             int i = I.intValue();
31             provider.setAttribute(KEY, Integer.valueOf(i + 1));
32             jgen.writeString("" +i+":"+value);
33         }
34     }
35 
36     static class TestPOJO
37     {
38         @JsonSerialize(using=PrefixStringSerializer.class)
39         public String value;
40 
TestPOJO(String str)41         public TestPOJO(String str) { value = str; }
42     }
43 
44     /*
45     /**********************************************************
46     /* Test methods
47     /**********************************************************
48      */
49 
50     final ObjectMapper MAPPER = sharedMapper();
51 
testSimplePerCall()52     public void testSimplePerCall() throws Exception
53     {
54         final String EXP = aposToQuotes("[{'value':'0:a'},{'value':'1:b'}]");
55         ObjectWriter w = MAPPER.writer();
56         final TestPOJO[] INPUT = new TestPOJO[] {
57                 new TestPOJO("a"), new TestPOJO("b") };
58         assertEquals(EXP, w.writeValueAsString(INPUT));
59 
60         // also: ensure that we don't retain per-call state accidentally:
61         assertEquals(EXP, w.writeValueAsString(INPUT));
62     }
63 
testSimpleDefaults()64     public void testSimpleDefaults() throws Exception
65     {
66         final String EXP = aposToQuotes("{'value':'3:xyz'}");
67         final TestPOJO INPUT = new TestPOJO("xyz");
68         String json = MAPPER.writer().withAttribute(KEY, Integer.valueOf(3))
69                 .writeValueAsString(INPUT);
70         assertEquals(EXP, json);
71 
72         String json2 = MAPPER.writer().withAttribute(KEY, Integer.valueOf(3))
73                 .writeValueAsString(INPUT);
74         assertEquals(EXP, json2);
75     }
76 
testHierarchic()77     public void testHierarchic() throws Exception
78     {
79         final TestPOJO[] INPUT = new TestPOJO[] { new TestPOJO("a"), new TestPOJO("b") };
80         final String EXP = aposToQuotes("[{'value':'2:a'},{'value':'3:b'}]");
81         ObjectWriter w = MAPPER.writer().withAttribute(KEY, Integer.valueOf(2));
82         assertEquals(EXP, w.writeValueAsString(INPUT));
83 
84         // and verify state clearing:
85         assertEquals(EXP, w.writeValueAsString(INPUT));
86     }
87 }
88