use of org.elasticsearch.common.xcontent.XContentGenerator in project elasticsearch by elastic.
the class XContentBuilderTests method testPrettyWithLfAtEnd.
public void testPrettyWithLfAtEnd() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
XContentGenerator generator = XContentFactory.xContent(XContentType.JSON).createGenerator(os);
generator.usePrettyPrint();
generator.usePrintLineFeedAtEnd();
generator.writeStartObject();
generator.writeStringField("test", "value");
generator.writeEndObject();
generator.flush();
generator.close();
// double close, and check there is no error...
generator.close();
byte[] bytes = os.toByteArray();
assertThat((char) bytes[bytes.length - 1], equalTo('\n'));
}
use of org.elasticsearch.common.xcontent.XContentGenerator in project elasticsearch by elastic.
the class XContentBuilderTests method testReuseJsonGenerator.
public void testReuseJsonGenerator() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
XContentGenerator generator = XContentFactory.xContent(XContentType.JSON).createGenerator(os);
generator.writeStartObject();
generator.writeStringField("test", "value");
generator.writeEndObject();
generator.flush();
assertThat(new BytesRef(os.toByteArray()), equalTo(new BytesRef("{\"test\":\"value\"}")));
// try again...
os.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(new BytesRef(os.toByteArray()), equalTo(new BytesRef(" {\"test\":\"value\"}")));
}
use of org.elasticsearch.common.xcontent.XContentGenerator in project elasticsearch by elastic.
the class JsonVsSmileTests method testCompareParsingTokens.
public void testCompareParsingTokens() 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.writeFieldName("arr");
xsonGen.writeStartArray();
jsonGen.writeFieldName("arr");
jsonGen.writeStartArray();
xsonGen.writeNumber(1);
jsonGen.writeNumber(1);
xsonGen.writeNull();
jsonGen.writeNull();
xsonGen.writeEndArray();
jsonGen.writeEndArray();
xsonGen.writeEndObject();
jsonGen.writeEndObject();
xsonGen.close();
jsonGen.close();
verifySameTokens(createParser(JsonXContent.jsonXContent, jsonOs.bytes()), createParser(SmileXContent.smileXContent, xsonOs.bytes()));
}
use of org.elasticsearch.common.xcontent.XContentGenerator in project elasticsearch by elastic.
the class AbstractQueryTestCase method alterateQueries.
/**
* Traverses the json tree of the valid query provided as argument and mutates it one or more times by adding one object within each
* object encountered.
*
* For instance given the following valid term query:
* {
* "term" : {
* "field" : {
* "value" : "foo"
* }
* }
* }
*
* The following two mutations will be generated, and an exception is expected when trying to parse them:
* {
* "term" : {
* "newField" : {
* "field" : {
* "value" : "foo"
* }
* }
* }
* }
*
* {
* "term" : {
* "field" : {
* "newField" : {
* "value" : "foo"
* }
* }
* }
* }
*
* Every mutation is then added to the list of results with a boolean flag indicating if a parsing exception is expected or not
* for the mutation. Some specific objects do not cause any exception as they can hold arbitrary content; they are passed using the
* arbitraryMarkers parameter.
*/
static List<Tuple<String, Boolean>> alterateQueries(Set<String> queries, Set<String> arbitraryMarkers) throws IOException {
List<Tuple<String, Boolean>> results = new ArrayList<>();
// Indicate if a part of the query can hold any arbitrary content
boolean hasArbitraryContent = (arbitraryMarkers != null && arbitraryMarkers.isEmpty() == false);
for (String query : queries) {
// Track the number of query mutations
int mutation = 0;
while (true) {
boolean expectException = true;
BytesStreamOutput out = new BytesStreamOutput();
try (XContentGenerator generator = XContentType.JSON.xContent().createGenerator(out);
XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, query)) {
int objectIndex = -1;
Deque<String> levels = new LinkedList<>();
// Parse the valid query and inserts a new object level called "newField"
XContentParser.Token token;
while ((token = parser.nextToken()) != null) {
if (token == XContentParser.Token.START_OBJECT) {
objectIndex++;
levels.addLast(parser.currentName());
if (objectIndex == mutation) {
// We reached the place in the object tree where we want to insert a new object level
generator.writeStartObject();
generator.writeFieldName("newField");
XContentHelper.copyCurrentStructure(generator, parser);
generator.writeEndObject();
if (hasArbitraryContent) {
// field is one (or a child) of those, no exception is expected when parsing the mutated query.
for (String marker : arbitraryMarkers) {
if (levels.contains(marker)) {
expectException = false;
break;
}
}
}
// Jump to next token
continue;
}
} else if (token == XContentParser.Token.END_OBJECT) {
levels.removeLast();
}
// We are walking through the object tree, so we can safely copy the current node
XContentHelper.copyCurrentEvent(generator, parser);
}
if (objectIndex < mutation) {
// We did not reach the insertion point, there's no more mutations to try
break;
} else {
// We reached the expected insertion point, so next time we'll try one step further
mutation++;
}
}
results.add(new Tuple<>(out.bytes().utf8ToString(), expectException));
}
}
return results;
}
use of org.elasticsearch.common.xcontent.XContentGenerator in project elasticsearch by elastic.
the class XContentBuilderTests method testWritingBinaryToStream.
public void testWritingBinaryToStream() throws Exception {
BytesStreamOutput bos = new BytesStreamOutput();
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();
String sData = bos.bytes().utf8ToString();
assertThat(sData, equalTo("{\"name\":\"something\", source : { test : \"value\" },\"name2\":\"something2\"}"));
}
Aggregations