• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.node;
2 
3 import com.fasterxml.jackson.core.JsonPointer;
4 import com.fasterxml.jackson.databind.*;
5 
6 public class TestJsonPointer
7     extends BaseMapTest
8 {
testIt()9     public void testIt() throws Exception
10     {
11         final JsonNode SAMPLE_ROOT = objectMapper().readTree(SAMPLE_DOC_JSON_SPEC);
12 
13         // first: "empty" pointer points to context node:
14         assertSame(SAMPLE_ROOT, SAMPLE_ROOT.at(JsonPointer.compile("")));
15 
16         // then simple reference
17         assertTrue(SAMPLE_ROOT.at(JsonPointer.compile("/Image")).isObject());
18 
19         JsonNode n = SAMPLE_ROOT.at(JsonPointer.compile("/Image/Width"));
20         assertTrue(n.isNumber());
21         assertEquals(SAMPLE_SPEC_VALUE_WIDTH, n.asInt());
22 
23         // ok also with implicit compile() for pointer:
24         assertEquals(SAMPLE_SPEC_VALUE_HEIGHT,
25                 SAMPLE_ROOT.at("/Image/Height").asInt());
26 
27         assertEquals(SAMPLE_SPEC_VALUE_TN_ID3,
28                 SAMPLE_ROOT.at(JsonPointer.compile("/Image/IDs/2")).asInt());
29 
30         // and then check that "missing" paths are ok too but
31         assertTrue(SAMPLE_ROOT.at("/Image/Depth").isMissingNode());
32         assertTrue(SAMPLE_ROOT.at("/Image/1").isMissingNode());
33     }
34 
35     // To help verify [Core#133]; should be fine with "big numbers" as property keys
testLongNumbers()36     public void testLongNumbers() throws Exception
37     {
38 
39         // First, with small int key
40         JsonNode root = objectMapper().readTree("{\"123\" : 456}");
41         JsonNode jn2 = root.at("/123");
42         assertEquals(456, jn2.asInt());
43 
44         // and then with above int-32:
45         root = objectMapper().readTree("{\"35361706045\" : 1234}");
46         jn2 = root.at("/35361706045");
47         assertEquals(1234, jn2.asInt());
48     }
49 }
50