Package org.elasticsearch.common.xcontent

Examples of org.elasticsearch.common.xcontent.XContentGenerator


//        gen.writeEndObject();
//    }

    @Test public void compareParsingTokens() throws IOException {
        FastByteArrayOutputStream xsonOs = new FastByteArrayOutputStream();
        XContentGenerator xsonGen = XContentFactory.xContent(XContentType.SMILE).createGenerator(xsonOs);

        FastByteArrayOutputStream jsonOs = new FastByteArrayOutputStream();
        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.copiedByteArray()), XContentFactory.xContent(XContentType.SMILE).createParser(xsonOs.copiedByteArray()));
    }
View Full Code Here


@Test
public class XContentBuilderTests {

    @Test public void verifyReuseJsonGenerator() throws Exception {
        FastCharArrayWriter writer = new FastCharArrayWriter();
        XContentGenerator generator = XContentFactory.xContent(XContentType.JSON).createGenerator(writer);
        generator.writeStartObject();
        generator.writeStringField("test", "value");
        generator.writeEndObject();
        generator.flush();

        assertThat(writer.toStringTrim(), equalTo("{\"test\":\"value\"}"));

        // try again...
        writer.reset();
        generator.writeStartObject();
        generator.writeStringField("test", "value");
        generator.writeEndObject();
        generator.flush();
        // we get a space at the start here since it thinks we are not in the root object (fine, we will ignore it in the real code we use)
        assertThat(writer.toStringTrim(), equalTo("{\"test\":\"value\"}"));
    }
View Full Code Here

    }

    @Test public void testWritingBinaryToStream() throws Exception {
        FastByteArrayOutputStream bos = new FastByteArrayOutputStream();

        XContentGenerator gen = XContentFactory.xContent(XContentType.JSON).createGenerator(bos);
        gen.writeStartObject();
        gen.writeStringField("name", "something");
        gen.flush();
        bos.write(", source : { test : \"value\" }".getBytes("UTF8"));
        gen.writeStringField("name2", "something2");
        gen.writeEndObject();
        gen.close();

        byte[] data = bos.copiedByteArray();
        String sData = new String(data, "UTF8");
        System.out.println("DATA: " + sData);
    }
View Full Code Here

                    }
                    ;
                    // Store underlying facet configuration
                  token = parser.nextToken(); // move the start of object...
                    ByteArrayOutputStream memstream = new ByteArrayOutputStream(20);
                    XContentGenerator generator = new JsonXContentGenerator(jsonFactory.createJsonGenerator(memstream));
                    XContentHelper.copyCurrentStructure(generator, parser);
                    generator.close();
                    memstream.close();
                    internalConfig = memstream.toByteArray(); // now we're at the end of the underlying config.
                                       
                    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT); // eat everything else under the "facet" clause
                 
View Full Code Here

//    }

    @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()));
    }
View Full Code Here

public class JsonVsCborTests extends ElasticsearchTestCase {

    @Test
    public void compareParsingTokens() throws IOException {
        BytesStreamOutput xsonOs = new BytesStreamOutput();
        XContentGenerator xsonGen = XContentFactory.xContent(XContentType.CBOR).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.CBOR).createParser(xsonOs.bytes().toBytes()));
    }
View Full Code Here

TOP

Related Classes of org.elasticsearch.common.xcontent.XContentGenerator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.