Fix JsonXContentGenerator to write comma after rawWrite if needed

This commit is contained in:
Simon Willnauer 2014-03-24 19:46:01 +01:00
parent 333e7df3bc
commit 76e595278a
3 changed files with 44 additions and 10 deletions

View File

@ -20,6 +20,7 @@
package org.elasticsearch.common.xcontent.json;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.base.GeneratorBase;
import com.fasterxml.jackson.core.io.SerializedString;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.Streams;
@ -36,9 +37,16 @@ public class JsonXContentGenerator implements XContentGenerator {
protected final JsonGenerator generator;
private boolean writeLineFeedAtEnd;
private final GeneratorBase base;
public JsonXContentGenerator(JsonGenerator generator) {
this.generator = generator;
if (generator instanceof GeneratorBase) {
base = (GeneratorBase) generator;
} else {
base = null;
}
}
@Override
@ -253,29 +261,29 @@ public class JsonXContentGenerator implements XContentGenerator {
@Override
public void writeRawField(String fieldName, byte[] content, OutputStream bos) throws IOException {
generator.writeRaw(", \"");
generator.writeRaw(fieldName);
generator.writeRaw("\" : ");
generator.writeFieldName(fieldName);
generator.writeRaw(':');
flush();
bos.write(content);
finishWriteRaw();
}
@Override
public void writeRawField(String fieldName, byte[] content, int offset, int length, OutputStream bos) throws IOException {
generator.writeRaw(", \"");
generator.writeRaw(fieldName);
generator.writeRaw("\" : ");
generator.writeFieldName(fieldName);
generator.writeRaw(':');
flush();
bos.write(content, offset, length);
finishWriteRaw();
}
@Override
public void writeRawField(String fieldName, InputStream content, OutputStream bos) throws IOException {
generator.writeRaw(", \"");
generator.writeRaw(fieldName);
generator.writeRaw("\" : ");
generator.writeFieldName(fieldName);
generator.writeRaw(':');
flush();
Streams.copy(content, bos);
finishWriteRaw();
}
@Override
@ -304,6 +312,14 @@ public class JsonXContentGenerator implements XContentGenerator {
generator.writeRaw(':');
flush();
content.writeTo(bos);
finishWriteRaw();
}
private void finishWriteRaw() {
assert base != null : "JsonGenerator should be of instance GeneratorBase but was: " + generator.getClass();
if (base != null) {
base.getOutputContext().writeValue();
}
}
@Override

View File

@ -91,6 +91,14 @@ public class XContentBuilderTests extends ElasticsearchTestCase {
xContentBuilder.endObject();
assertThat(xContentBuilder.bytes().toUtf8(), equalTo("{\"foo\":{\"test\":\"value\"}}"));
}
{
XContentBuilder xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
xContentBuilder.startObject();
xContentBuilder.rawField("foo", new BytesArray("{\"test\":\"value\"}"));
xContentBuilder.rawField("foo1", new BytesArray("{\"test\":\"value\"}"));
xContentBuilder.endObject();
assertThat(xContentBuilder.bytes().toUtf8(), equalTo("{\"foo\":{\"test\":\"value\"},\"foo1\":{\"test\":\"value\"}}"));
}
{
XContentBuilder xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
xContentBuilder.startObject();
@ -108,6 +116,16 @@ public class XContentBuilderTests extends ElasticsearchTestCase {
xContentBuilder.endObject();
assertThat(xContentBuilder.bytes().toUtf8(), equalTo("{\"test\":\"value\",\"foo\":{\"test\":\"value\"},\"test1\":\"value1\"}"));
}
{
XContentBuilder xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
xContentBuilder.startObject();
xContentBuilder.field("test", "value");
xContentBuilder.rawField("foo", new BytesArray("{\"test\":\"value\"}"));
xContentBuilder.rawField("foo1", new BytesArray("{\"test\":\"value\"}"));
xContentBuilder.field("test1", "value1");
xContentBuilder.endObject();
assertThat(xContentBuilder.bytes().toUtf8(), equalTo("{\"test\":\"value\",\"foo\":{\"test\":\"value\"},\"foo1\":{\"test\":\"value\"},\"test1\":\"value1\"}"));
}
}
@Test

View File

@ -109,7 +109,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest {
SearchResponse searchResponse = client().prepareSearch()
.setSearchType(SearchType.COUNT)
.setFacets(new BytesArray(
"{\"facet1\":{\"terms\":{\"field\":\"tag\"},\"facet_filter\":{ }}}").array())
"{\"facet1\":{\"terms\":{\"field\":\"tag\"},\"facet_filter\":{ }}}"))
.get();
assertHitCount(searchResponse, 1l);