1 package com.fasterxml.jackson.databind.node; 2 3 import java.io.*; 4 import java.util.*; 5 6 import com.fasterxml.jackson.core.*; 7 import com.fasterxml.jackson.databind.*; 8 import com.fasterxml.jackson.databind.node.TestTreeDeserialization.Bean; 9 10 /** 11 * This unit test suite tries to verify that ObjectMapper 12 * can properly parse JSON and bind contents into appropriate 13 * JsonNode instances. 14 */ 15 public class TreeReadViaMapperTest extends BaseMapTest 16 { 17 private final ObjectMapper MAPPER = objectMapper(); 18 testSimple()19 public void testSimple() throws Exception 20 { 21 final String JSON = SAMPLE_DOC_JSON_SPEC; 22 23 for (int type = 0; type < 2; ++type) { 24 JsonNode result; 25 26 if (type == 0) { 27 result = MAPPER.readTree(new StringReader(JSON)); 28 } else { 29 result = MAPPER.readTree(JSON); 30 } 31 32 assertType(result, ObjectNode.class); 33 assertEquals(1, result.size()); 34 assertTrue(result.isObject()); 35 36 ObjectNode main = (ObjectNode) result; 37 assertEquals("Image", main.fieldNames().next()); 38 JsonNode ob = main.elements().next(); 39 assertType(ob, ObjectNode.class); 40 ObjectNode imageMap = (ObjectNode) ob; 41 42 assertEquals(5, imageMap.size()); 43 ob = imageMap.get("Width"); 44 assertTrue(ob.isIntegralNumber()); 45 assertFalse(ob.isFloatingPointNumber()); 46 assertEquals(SAMPLE_SPEC_VALUE_WIDTH, ob.intValue()); 47 ob = imageMap.get("Height"); 48 assertTrue(ob.isIntegralNumber()); 49 assertEquals(SAMPLE_SPEC_VALUE_HEIGHT, ob.intValue()); 50 51 ob = imageMap.get("Title"); 52 assertTrue(ob.isTextual()); 53 assertEquals(SAMPLE_SPEC_VALUE_TITLE, ob.textValue()); 54 55 ob = imageMap.get("Thumbnail"); 56 assertType(ob, ObjectNode.class); 57 ObjectNode tn = (ObjectNode) ob; 58 ob = tn.get("Url"); 59 assertTrue(ob.isTextual()); 60 assertEquals(SAMPLE_SPEC_VALUE_TN_URL, ob.textValue()); 61 ob = tn.get("Height"); 62 assertTrue(ob.isIntegralNumber()); 63 assertEquals(SAMPLE_SPEC_VALUE_TN_HEIGHT, ob.intValue()); 64 ob = tn.get("Width"); 65 assertTrue(ob.isTextual()); 66 assertEquals(SAMPLE_SPEC_VALUE_TN_WIDTH, ob.textValue()); 67 68 ob = imageMap.get("IDs"); 69 assertTrue(ob.isArray()); 70 ArrayNode idList = (ArrayNode) ob; 71 assertEquals(4, idList.size()); 72 assertEquals(4, calcLength(idList.elements())); 73 assertEquals(4, calcLength(idList.iterator())); 74 { 75 int[] values = new int[] { 76 SAMPLE_SPEC_VALUE_TN_ID1, 77 SAMPLE_SPEC_VALUE_TN_ID2, 78 SAMPLE_SPEC_VALUE_TN_ID3, 79 SAMPLE_SPEC_VALUE_TN_ID4 80 }; 81 for (int i = 0; i < values.length; ++i) { 82 assertEquals(values[i], idList.get(i).intValue()); 83 } 84 int i = 0; 85 for (JsonNode n : idList) { 86 assertEquals(values[i], n.intValue()); 87 ++i; 88 } 89 } 90 } 91 } 92 testMixed()93 public void testMixed() throws IOException 94 { 95 String JSON = "{\"node\" : { \"a\" : 3 }, \"x\" : 9 }"; 96 Bean bean = MAPPER.readValue(JSON, Bean.class); 97 98 assertEquals(9, bean._x); 99 JsonNode n = bean._node; 100 assertNotNull(n); 101 assertEquals(1, n.size()); 102 ObjectNode on = (ObjectNode) n; 103 assertEquals(3, on.get("a").intValue()); 104 } 105 106 /** 107 * Type mappers should be able to gracefully deal with end of 108 * input. 109 */ testEOF()110 public void testEOF() throws Exception 111 { 112 String JSON = 113 "{ \"key\": [ { \"a\" : { \"name\": \"foo\", \"type\": 1\n" 114 +"}, \"type\": 3, \"url\": \"http://www.google.com\" } ],\n" 115 +"\"name\": \"xyz\", \"type\": 1, \"url\" : null }\n " 116 ; 117 JsonFactory jf = new JsonFactory(); 118 JsonParser p = jf.createParser(new StringReader(JSON)); 119 JsonNode result = MAPPER.readTree(p); 120 121 assertTrue(result.isObject()); 122 assertEquals(4, result.size()); 123 124 assertNull(MAPPER.readTree(p)); 125 p.close(); 126 } 127 testNullViaParser()128 public void testNullViaParser() throws Exception 129 { 130 final String JSON = " null "; 131 JsonFactory jf = new JsonFactory(); 132 133 try (JsonParser p = jf.createParser(new StringReader(JSON))) { 134 final JsonNode result = MAPPER.readTree(p); 135 assertTrue(result.isNull()); 136 } 137 } 138 testMultiple()139 public void testMultiple() throws Exception 140 { 141 String JSON = "12 \"string\" [ 1, 2, 3 ]"; 142 JsonFactory jf = new JsonFactory(); 143 JsonParser p = jf.createParser(new StringReader(JSON)); 144 final ObjectMapper mapper = objectMapper(); 145 JsonNode result = mapper.readTree(p); 146 147 assertTrue(result.isIntegralNumber()); 148 assertTrue(result.isInt()); 149 assertFalse(result.isTextual()); 150 assertEquals(12, result.intValue()); 151 152 result = mapper.readTree(p); 153 assertTrue(result.isTextual()); 154 assertFalse(result.isIntegralNumber()); 155 assertFalse(result.isInt()); 156 assertEquals("string", result.textValue()); 157 158 result = mapper.readTree(p); 159 assertTrue(result.isArray()); 160 assertEquals(3, result.size()); 161 162 assertNull(mapper.readTree(p)); 163 p.close(); 164 } 165 166 /* 167 /********************************************** 168 /* Helper methods 169 /********************************************** 170 */ 171 calcLength(Iterator<JsonNode> it)172 private int calcLength(Iterator<JsonNode> it) 173 { 174 int count = 0; 175 while (it.hasNext()) { 176 it.next(); 177 ++count; 178 } 179 return count; 180 } 181 } 182 183