Skip to content

Commit

Permalink
Use build-in function to write field name in JsonXContentGenerator#wr…
Browse files Browse the repository at this point in the history
…iteRawField

The #writeRawField method forcefully writes a `,` spearator expecting a raw field
to never start as the first value in an object.

Closes elastic#5514
  • Loading branch information
s1monw committed Mar 24, 2014
1 parent c379aa2 commit 45e1a13
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Expand Up @@ -300,9 +300,8 @@ public final void writeRawField(String fieldName, BytesReference content, Output
}

protected void writeObjectRaw(String fieldName, BytesReference content, OutputStream bos) throws IOException {
generator.writeRaw(", \"");
generator.writeRaw(fieldName);
generator.writeRaw("\" : ");
generator.writeFieldName(fieldName);
generator.writeRaw(':');
flush();
content.writeTo(bos);
}
Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.common.xcontent.builder;

import com.google.common.collect.Lists;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.io.FastCharArrayWriter;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand All @@ -29,6 +30,7 @@
import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Test;

import java.io.IOException;
import java.util.*;

import static org.elasticsearch.common.xcontent.XContentBuilder.FieldCaseConversion.CAMELCASE;
Expand Down Expand Up @@ -80,6 +82,34 @@ public void verifyReuseJsonGenerator() throws Exception {
assertThat(writer.toStringTrim(), equalTo("{\"test\":\"value\"}"));
}

@Test
public void testRaw() throws IOException {
{
XContentBuilder xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
xContentBuilder.startObject();
xContentBuilder.rawField("foo", new BytesArray("{\"test\":\"value\"}"));
xContentBuilder.endObject();
assertThat(xContentBuilder.bytes().toUtf8(), equalTo("{\"foo\":{\"test\":\"value\"}}"));
}
{
XContentBuilder xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
xContentBuilder.startObject();
xContentBuilder.field("test", "value");
xContentBuilder.rawField("foo", new BytesArray("{\"test\":\"value\"}"));
xContentBuilder.endObject();
assertThat(xContentBuilder.bytes().toUtf8(), equalTo("{\"test\":\"value\",\"foo\":{\"test\":\"value\"}}"));
}
{
XContentBuilder xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
xContentBuilder.startObject();
xContentBuilder.field("test", "value");
xContentBuilder.rawField("foo", new BytesArray("{\"test\":\"value\"}"));
xContentBuilder.field("test1", "value1");
xContentBuilder.endObject();
assertThat(xContentBuilder.bytes().toUtf8(), equalTo("{\"test\":\"value\",\"foo\":{\"test\":\"value\"},\"test1\":\"value1\"}"));
}
}

@Test
public void testSimpleGenerator() throws Exception {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
Expand Down

0 comments on commit 45e1a13

Please sign in to comment.