コード例 #1
1
  /** 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
  private void verifyFromMap(String input) throws Exception {
    JsonParser p = new JsonFactory().createParser(input);
    assertEquals(JsonToken.START_OBJECT, p.nextToken());
    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals(FIELD4, getAndVerifyText(p));
    assertEquals(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals(TEXT2, getAndVerifyText(p));

    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals(FIELD3, getAndVerifyText(p));
    assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(-1, p.getIntValue());

    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals(FIELD2, getAndVerifyText(p));
    assertEquals(JsonToken.START_ARRAY, p.nextToken());
    assertEquals(JsonToken.END_ARRAY, p.nextToken());

    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals(FIELD1, getAndVerifyText(p));
    assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
    assertEquals(DOUBLE_VALUE, p.getDoubleValue(), 0);

    assertEquals(JsonToken.END_OBJECT, p.nextToken());

    assertNull(p.nextToken());
    p.close();
  }
コード例 #3
0
  private void verifyFromArray(String input) throws Exception {
    JsonParser p = new JsonFactory().createParser(new StringReader(input));

    assertEquals(JsonToken.START_ARRAY, p.nextToken());

    assertEquals(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals(TEXT1, getAndVerifyText(p));

    assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(3, p.getIntValue());

    assertEquals(JsonToken.START_OBJECT, p.nextToken());
    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals(FIELD1, getAndVerifyText(p));

    assertEquals(JsonToken.VALUE_TRUE, p.nextToken());
    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals(FIELD2, getAndVerifyText(p));

    assertEquals(JsonToken.START_ARRAY, p.nextToken());
    assertEquals(JsonToken.END_ARRAY, p.nextToken());
    assertEquals(JsonToken.END_OBJECT, p.nextToken());

    assertEquals(JsonToken.VALUE_FALSE, p.nextToken());

    assertEquals(JsonToken.END_ARRAY, p.nextToken());
    assertNull(p.nextToken());
    p.close();
  }
コード例 #4
0
  public void testBinary() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    final int LENGTH = 13045;
    byte[] data = new byte[LENGTH];
    for (int i = 0; i < LENGTH; ++i) {
      data[i] = (byte) i;
    }
    StringWriter sw = new StringWriter();
    mapper.writeValue(sw, BinaryNode.valueOf(data));

    JsonParser p = new JsonFactory().createParser(sw.toString());
    // note: can't determine it's binary from json alone:
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertArrayEquals(data, p.getBinaryValue());
    p.close();
  }
コード例 #5
0
 protected final double _testRawDeser(int reps, byte[] json, ObjectReader reader)
     throws IOException {
   long start = System.nanoTime();
   final JsonFactory f = reader.getFactory();
   while (--reps >= 0) {
     JsonParser p = f.createParser(new ByteArrayInputStream(json));
     JsonToken t;
     while ((t = p.nextToken()) != null) {
       if (t == JsonToken.VALUE_STRING) {
         p.getText();
       } else if (t.isNumeric()) {
         p.getNumberValue();
       }
       ;
     }
     p.close();
   }
   hash = f.hashCode();
   return _msecsFromNanos(System.nanoTime() - start);
 }