예제 #1
0
  @Test
  public void compareParsingTokens() throws IOException {
    BytesStreamOutput xsonOs = new BytesStreamOutput();
    XContentGenerator xsonGen =
        XContentFactory.xContent(XContentType.SMILE).createGenerator(xsonOs);

    BytesStreamOutput jsonOs = new BytesStreamOutput();
    XContentGenerator jsonGen = XContentFactory.xContent(XContentType.JSON).createGenerator(jsonOs);

    xsonGen.writeStartObject();
    jsonGen.writeStartObject();

    xsonGen.writeStringField("test", "value");
    jsonGen.writeStringField("test", "value");

    xsonGen.writeArrayFieldStart("arr");
    jsonGen.writeArrayFieldStart("arr");
    xsonGen.writeNumber(1);
    jsonGen.writeNumber(1);
    xsonGen.writeNull();
    jsonGen.writeNull();
    xsonGen.writeEndArray();
    jsonGen.writeEndArray();

    xsonGen.writeEndObject();
    jsonGen.writeEndObject();

    xsonGen.close();
    jsonGen.close();

    verifySameTokens(
        XContentFactory.xContent(XContentType.JSON).createParser(jsonOs.bytes().toBytes()),
        XContentFactory.xContent(XContentType.SMILE).createParser(xsonOs.bytes().toBytes()));
  }
  public static void writeMap(XContentGenerator gen, Map<String, Object> map) throws IOException {
    gen.writeStartObject();

    for (Map.Entry<String, Object> entry : map.entrySet()) {
      gen.writeFieldName(entry.getKey());
      Object value = entry.getValue();
      if (value == null) {
        gen.writeNull();
      } else {
        writeValue(gen, value);
      }
    }

    gen.writeEndObject();
  }