• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.ser;
2 
3 import java.util.HashMap;
4 import java.util.Map;
5 
6 import com.fasterxml.jackson.annotation.*;
7 import com.fasterxml.jackson.databind.JsonNode;
8 import com.fasterxml.jackson.databind.ObjectMapper;
9 import com.fasterxml.jackson.databind.util.RawValue;
10 
11 /**
12  * This unit test suite tests functioning of {@link JsonRawValue}
13  * annotation with bean serialization.
14  */
15 public class RawValueTest
16     extends com.fasterxml.jackson.databind.BaseMapTest
17 {
18     /*
19     /*********************************************************
20     /* Helper bean classes
21     /*********************************************************
22      */
23 
24     /// Class for testing {@link JsonRawValue} annotations with getters returning String
25     @JsonPropertyOrder(alphabetic=true)
26     final static class ClassGetter<T>
27     {
28         protected final T _value;
29 
ClassGetter(T value)30         protected ClassGetter(T value) { _value = value;}
31 
getNonRaw()32         public T getNonRaw() { return _value; }
33 
foobar()34         @JsonProperty("raw") @JsonRawValue public T foobar() { return _value; }
35 
value()36         @JsonProperty @JsonRawValue protected T value() { return _value; }
37     }
38 
39     // [databind#348]
40     static class RawWrapped
41     {
42         @JsonRawValue
43         private final String json;
44 
RawWrapped(String str)45         public RawWrapped(String str) {
46             json = str;
47         }
48     }
49 
50     /*
51     /*********************************************************
52     /* Test cases
53     /*********************************************************
54      */
55 
56     private final ObjectMapper MAPPER = objectMapper();
57 
testSimpleStringGetter()58     public void testSimpleStringGetter() throws Exception
59     {
60         String value = "abc";
61         String result = MAPPER.writeValueAsString(new ClassGetter<String>(value));
62         String expected = String.format("{\"nonRaw\":\"%s\",\"raw\":%s,\"value\":%s}", value, value, value);
63         assertEquals(expected, result);
64     }
65 
testSimpleNonStringGetter()66     public void testSimpleNonStringGetter() throws Exception
67     {
68         int value = 123;
69         String result = MAPPER.writeValueAsString(new ClassGetter<Integer>(value));
70         String expected = String.format("{\"nonRaw\":%d,\"raw\":%d,\"value\":%d}", value, value, value);
71         assertEquals(expected, result);
72     }
73 
testNullStringGetter()74     public void testNullStringGetter() throws Exception
75     {
76         String result = MAPPER.writeValueAsString(new ClassGetter<String>(null));
77         String expected = "{\"nonRaw\":null,\"raw\":null,\"value\":null}";
78         assertEquals(expected, result);
79     }
80 
testWithValueToTree()81     public void testWithValueToTree() throws Exception
82     {
83         JsonNode w = MAPPER.valueToTree(new RawWrapped("{ }"));
84         assertNotNull(w);
85         assertEquals("{\"json\":{ }}", MAPPER.writeValueAsString(w));
86     }
87 
88     // for [databind#743]
testRawFromMapToTree()89     public void testRawFromMapToTree() throws Exception
90     {
91         RawValue myType = new RawValue("Jackson");
92 
93         Map<String, Object> object = new HashMap<String, Object>();
94         object.put("key", myType);
95         JsonNode jsonNode = MAPPER.valueToTree(object);
96         String json = MAPPER.writeValueAsString(jsonNode);
97         assertEquals("{\"key\":Jackson}", json);
98     }
99 }
100