diff --git a/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java b/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java index cd56f8da093..05d0d95868f 100644 --- a/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java +++ b/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java @@ -300,9 +300,8 @@ public class JsonXContentGenerator implements XContentGenerator { } 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); } diff --git a/src/test/java/org/elasticsearch/common/xcontent/builder/XContentBuilderTests.java b/src/test/java/org/elasticsearch/common/xcontent/builder/XContentBuilderTests.java index 67bb99f4e35..ec074ccd4dd 100644 --- a/src/test/java/org/elasticsearch/common/xcontent/builder/XContentBuilderTests.java +++ b/src/test/java/org/elasticsearch/common/xcontent/builder/XContentBuilderTests.java @@ -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; @@ -29,6 +30,7 @@ import org.elasticsearch.common.xcontent.XContentType; 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; @@ -80,6 +82,34 @@ public class XContentBuilderTests extends ElasticsearchTestCase { 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);