• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.node;
2 
3 import com.fasterxml.jackson.databind.BaseMapTest;
4 import com.fasterxml.jackson.databind.JsonNode;
5 import com.fasterxml.jackson.databind.ObjectMapper;
6 
7 /**
8  * This unit test suite tries to verify that JsonNode-based trees
9  * can be deserialized as expected.
10  */
11 public class TestTreeDeserialization
12     extends BaseMapTest
13 {
14     final static class Bean {
15         int _x;
16         JsonNode _node;
17 
setX(int x)18         public void setX(int x) { _x = x; }
setNode(JsonNode n)19         public void setNode(JsonNode n) { _node = n; }
20     }
21 
22     /*
23     /**********************************************************
24     /* Unit tests
25     /**********************************************************
26      */
27 
testObjectNodeEquality()28     public void testObjectNodeEquality()
29     {
30         ObjectNode n1 = new ObjectNode(null);
31         ObjectNode n2 = new ObjectNode(null);
32 
33         assertTrue(n1.equals(n2));
34         assertTrue(n2.equals(n1));
35 
36         n1.set("x", TextNode.valueOf("Test"));
37 
38         assertFalse(n1.equals(n2));
39         assertFalse(n2.equals(n1));
40 
41         n2.set("x", TextNode.valueOf("Test"));
42 
43         assertTrue(n1.equals(n2));
44         assertTrue(n2.equals(n1));
45     }
46 
testReadFromString()47     public void testReadFromString() throws Exception
48     {
49         String json = "{\"field\":\"{\\\"name\\\":\\\"John Smith\\\"}\"}";
50         ObjectMapper mapper = new ObjectMapper();
51         JsonNode jNode = mapper.readValue(json, JsonNode.class);
52 
53         String generated = mapper.writeValueAsString( jNode);  //back slashes are gone
54         JsonNode out = mapper.readValue( generated, JsonNode.class );   //crashes here
55         assertTrue(out.isObject());
56         assertEquals(1, out.size());
57         String value = out.path("field").asText();
58         assertNotNull(value);
59     }
60 }
61