Ejemplo n.º 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()));
  }
 private static void writeObjectArray(XContentGenerator gen, Object[] array) throws IOException {
   gen.writeStartArray();
   for (Object value : array) {
     writeValue(gen, value);
   }
   gen.writeEndArray();
 }
 private static void writeIterable(XContentGenerator gen, Iterable iterable) throws IOException {
   gen.writeStartArray();
   for (Object value : iterable) {
     writeValue(gen, value);
   }
   gen.writeEndArray();
 }