Debian Codegraph @

src/test/java/com/fasterxml/jackson/dataformat/smile/TestTreeHandling.java

raw
1 package com.fasterxml.jackson.dataformat.smile;
2
3 import org.junit.Assert;
4
5 import com.fasterxml.jackson.databind.JsonNode;
6
7 import com.fasterxml.jackson.databind.ObjectMapper;
8 import com.fasterxml.jackson.databind.node.ObjectNode;
9
10 public class TestTreeHandling extends SmileTestBase
11 {
12 public void testSimple() throws Exception
13 {
14 // create the serialized JSON with byte array
15 ObjectMapper mapper = new ObjectMapper(new SmileFactory());
16
17 ObjectNode top1 = mapper.createObjectNode();
18 ObjectNode foo1 = top1.putObject("foo");
19 foo1.put("bar", "baz");
20 final String TEXT = "Caf\u00e9 1\u20ac";
21 final byte[] TEXT_BYTES = TEXT.getBytes("UTF-8");
22 foo1.put("dat", TEXT_BYTES);
23
24 byte[] doc = mapper.writeValueAsBytes(top1);
25 // now, deserialize
26 JsonNode top2 = mapper.readValue(doc, JsonNode.class);
27 JsonNode foo2 = top2.get("foo");
28 assertEquals("baz", foo2.get("bar").textValue());
29
30 JsonNode datNode = foo2.get("dat");
31 if (!datNode.isBinary()) {
32 fail("Expected binary node; got "+datNode.getClass().getName());
33 }
34 byte[] bytes = datNode.binaryValue();
35 Assert.assertArrayEquals(TEXT_BYTES, bytes);
36 }
37 }