1 package com.fasterxml.jackson.databind.ser; 2 3 4 import java.io.*; 5 import java.util.*; 6 7 8 import com.fasterxml.jackson.core.*; 9 import com.fasterxml.jackson.databind.*; 10 import com.fasterxml.jackson.databind.node.*; 11 12 /** 13 * This unit test suite tries to verify that JsonNode-based trees 14 * can be serialized as expected 15 */ 16 public class TestTreeSerialization 17 extends BaseMapTest 18 { 19 final static class Bean { getX()20 public String getX() { return "y"; } getY()21 public int getY() { return 13; } 22 } 23 24 @SuppressWarnings("unchecked") testSimpleViaObjectMapper()25 public void testSimpleViaObjectMapper() 26 throws IOException 27 { 28 ObjectMapper mapper = new ObjectMapper(); 29 // also need tree mapper to construct tree to serialize 30 ObjectNode n = mapper.getNodeFactory().objectNode(); 31 n.put("number", 15); 32 n.put("string", "abc"); 33 ObjectNode n2 = n.putObject("ob"); 34 n2.putArray("arr"); 35 StringWriter sw = new StringWriter(); 36 JsonGenerator jg = mapper.createGenerator(sw); 37 mapper.writeTree(jg, n); 38 39 Map<String,Object> result = (Map<String,Object>) mapper.readValue(sw.toString(), Map.class); 40 41 assertEquals(3, result.size()); 42 assertEquals("abc", result.get("string")); 43 assertEquals(Integer.valueOf(15), result.get("number")); 44 Map<String,Object> ob = (Map<String,Object>) result.get("ob"); 45 assertEquals(1, ob.size()); 46 List<Object> list = (List<Object>) ob.get("arr"); 47 assertEquals(0, list.size()); 48 jg.close(); 49 } 50 51 /** 52 * Simple test to verify that POJONodes (JsonNode wrapper around 53 * any old Java object) work with serialization 54 */ 55 @SuppressWarnings("unchecked") testPOJOString()56 public void testPOJOString() 57 throws Exception 58 { 59 ObjectMapper mapper = new ObjectMapper(); 60 // also need tree mapper to construct tree to serialize 61 ObjectNode n = mapper.getNodeFactory().objectNode(); 62 n.set("pojo", mapper.getNodeFactory().pojoNode("abc")); 63 StringWriter sw = new StringWriter(); 64 JsonGenerator jg = mapper.createGenerator(sw); 65 mapper.writeTree(jg, n); 66 Map<String,Object> result = (Map<String,Object>) mapper.readValue(sw.toString(), Map.class); 67 assertEquals(1, result.size()); 68 assertEquals("abc", result.get("pojo")); 69 jg.close(); 70 } 71 72 @SuppressWarnings("unchecked") testPOJOIntArray()73 public void testPOJOIntArray() 74 throws IOException 75 { 76 ObjectMapper mapper = new ObjectMapper(); 77 ObjectNode n = mapper.getNodeFactory().objectNode(); 78 n.set("pojo", mapper.getNodeFactory().pojoNode(new int[] { 1, 2, 3 })); 79 StringWriter sw = new StringWriter(); 80 JsonGenerator jg = mapper.createGenerator(sw); 81 mapper.writeTree(jg, n); 82 83 Map<String,Object> result = (Map<String,Object>) mapper.readValue(sw.toString(), Map.class); 84 85 assertEquals(1, result.size()); 86 // int array becomes a list when mapped to general Object: 87 List<Object> list = (List<Object>) result.get("pojo"); 88 assertEquals(3, list.size()); 89 for (int i = 0; i < 3; ++i) { 90 assertEquals(Integer.valueOf(i+1), list.get(i)); 91 } 92 jg.close(); 93 } 94 95 @SuppressWarnings("unchecked") testPOJOBean()96 public void testPOJOBean() 97 throws IOException 98 { 99 ObjectMapper mapper = new ObjectMapper(); 100 // also need tree mapper to construct tree to serialize 101 ObjectNode n = mapper.getNodeFactory().objectNode(); 102 n.set("pojo", mapper.getNodeFactory().pojoNode(new Bean())); 103 StringWriter sw = new StringWriter(); 104 JsonGenerator jg = mapper.createGenerator(sw); 105 mapper.writeTree(jg, n); 106 107 Map<String,Object> result = (Map<String,Object>) mapper.readValue(sw.toString(), Map.class); 108 109 assertEquals(1, result.size()); 110 Map<String,Object> bean = (Map<String,Object>) result.get("pojo"); 111 assertEquals(2, bean.size()); 112 assertEquals("y", bean.get("x")); 113 assertEquals(Integer.valueOf(13), bean.get("y")); 114 jg.close(); 115 } 116 } 117