/** 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();
    }
  }
예제 #2
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));
 }