/** Unit test to check for regression of [JACKSON-18]. */
  public void testSmallNumbers() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode root = mapper.createArrayNode();
    for (int i = -20; i <= 20; ++i) {
      JsonNode n = root.numberNode(i);
      root.add(n);
      // Hmmh. Not sure why toString() won't be triggered otherwise...
      assertEquals(String.valueOf(i), n.toString());
    }

    // Loop over 2 different serialization methods
    for (int type = 0; type < 2; ++type) {
      StringWriter sw = new StringWriter();
      if (type == 0) {
        JsonGenerator gen = new JsonFactory().createGenerator(sw);
        root.serialize(gen, null);
        gen.close();
      } else {
        mapper.writeValue(sw, root);
      }

      String doc = sw.toString();
      JsonParser p = new JsonFactory().createParser(new StringReader(doc));

      assertEquals(JsonToken.START_ARRAY, p.nextToken());
      for (int i = -20; i <= 20; ++i) {
        assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
        assertEquals(i, p.getIntValue());
        assertEquals("" + i, p.getText());
      }
      assertEquals(JsonToken.END_ARRAY, p.nextToken());
      p.close();
    }
  }
 @Override
 public Leaf deserialize(JsonParser jp, DeserializationContext ctxt)
     throws IOException, JsonProcessingException {
   JsonNode tree = (JsonNode) jp.readValueAsTree();
   Leaf leaf = new Leaf();
   leaf.value = tree.get("value").intValue();
   return leaf;
 }
 public void testEmbeddedObject() throws Exception {
   TokenBuffer buf = new TokenBuffer(MAPPER);
   buf.writeObject(new byte[3]);
   JsonNode node = MAPPER.readTree(buf.asParser());
   buf.close();
   assertTrue(node.isBinary());
   byte[] data = node.binaryValue();
   assertNotNull(data);
   assertEquals(3, data.length);
 }
 // [Issue#232]
 public void testBigDecimalAsPlainStringTreeConversion() throws Exception {
   ObjectMapper mapper = new ObjectMapper();
   mapper.enable(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN);
   Map<String, Object> map = new HashMap<String, Object>();
   String PI_STR = "3.00000000";
   map.put("pi", new BigDecimal(PI_STR));
   JsonNode tree = mapper.valueToTree(map);
   assertNotNull(tree);
   assertEquals(1, tree.size());
   assertTrue(tree.has("pi"));
 }
Example #5
0
 @Test
 public void testMetadataInputStream() throws Exception {
   MetaDataInputStream mis =
       new MetaDataInputStream(
           getClass()
               .getClassLoader()
               .getResourceAsStream("META-INF/services/org.brutusin.json.spi.JsonCodec"),
           null,
           null,
           Long.MIN_VALUE);
   TestClass instance = new TestClass();
   String json = JsonCodec.getInstance().transform(instance);
   assertTrue(!json.contains("\"metaDataInputStream\""));
   instance.setMetaDataInputStream(mis);
   json = JsonCodec.getInstance().transform(instance);
   assertTrue(json.contains("\"metaDataInputStream\":\"#1#"));
   JsonNode node = JsonCodec.getInstance().toJsonNode(instance);
   assertEquals(node.get("metaDataInputStream").asStream(), mis);
   TestClass instance2 = JsonCodec.getInstance().load(node, TestClass.class);
   assertEquals(instance.getMetaDataInputStream(), instance2.getMetaDataInputStream());
   Map<String, InputStream> streams = new HashMap();
   streams.put(node.get("metaDataInputStream").asString(), mis);
   JsonNode node2 = JsonCodec.getInstance().parse(node.toString(), streams);
   assertEquals(node2.get("metaDataInputStream").asStream(), mis);
   JsonNode node3 = JsonCodec.getInstance().parse(node.toString(), streams);
   TestClass instance3 = JsonCodec.getInstance().load(node3, TestClass.class);
   assertEquals(instance.getInputStream(), instance3.getInputStream());
   assertTrue(
       JsonCodec.getInstance()
           .getReferencedStreamCount(node, JsonCodec.getInstance().getSchema(TestClass.class))
           .equals(1));
 }
 public void testEmbeddedObjectInArray() throws Exception {
   TokenBuffer buf = new TokenBuffer(MAPPER);
   buf.writeStartArray();
   buf.writeObject(MARKER);
   buf.writeEndArray();
   JsonNode node = MAPPER.readTree(buf.asParser());
   buf.close();
   assertTrue(node.isArray());
   assertEquals(1, node.size());
   JsonNode n = node.get(0);
   assertTrue(n.isPojo());
   assertSame(MARKER, ((POJONode) n).getPojo());
 }