Skip to content
Browse files

add field with text/bytes and builder string

  • Loading branch information...
1 parent 71db930 commit 7956d1490331da08fa3978d00a82f12b25918c1b @kimchy kimchy committed
Showing with 30 additions and 2 deletions.
  1. +30 −2 src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java
View
32 src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java
@@ -443,7 +443,7 @@ public XContentBuilder field(String name, Double value) throws IOException {
if (value == null) {
generator.writeNull();
} else {
- generator.writeNumber(value.doubleValue());
+ generator.writeNumber(value);
}
return this;
}
@@ -453,7 +453,7 @@ public XContentBuilder field(XContentBuilderString name, Double value) throws IO
if (value == null) {
generator.writeNull();
} else {
- generator.writeNumber(value.doubleValue());
+ generator.writeNumber(value);
}
return this;
}
@@ -479,6 +479,15 @@ public XContentBuilder field(String name, BytesReference value) throws IOExcepti
return this;
}
+ public XContentBuilder field(XContentBuilderString name, BytesReference value) throws IOException {
+ field(name);
+ if (!value.hasArray()) {
+ value = value.toBytesArray();
+ }
+ generator.writeBinary(value.array(), value.arrayOffset(), value.length());
+ return this;
+ }
+
public XContentBuilder field(String name, Text value) throws IOException {
field(name);
if (value.hasBytes() && value.bytes().hasArray()) {
@@ -495,6 +504,21 @@ public XContentBuilder field(String name, Text value) throws IOException {
return this;
}
+ public XContentBuilder field(XContentBuilderString name, Text value) throws IOException {
+ field(name);
+ if (value.hasBytes() && value.bytes().hasArray()) {
+ generator.writeUTF8String(value.bytes().array(), value.bytes().arrayOffset(), value.bytes().length());
+ return this;
+ }
+ if (value.hasString()) {
+ generator.writeString(value.string());
+ return this;
+ }
+ // TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a request to jackson to support InputStream as well?
+ BytesArray bytesArray = value.bytes().toBytesArray();
+ generator.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length());
+ return this;
+ }
public XContentBuilder field(String name, byte[] value, int offset, int length) throws IOException {
field(name);
@@ -684,6 +708,10 @@ public XContentBuilder field(String name, Object value) throws IOException {
field(name, (float[]) value);
} else if (value instanceof double[]) {
field(name, (double[]) value);
+ } else if (value instanceof BytesReference) {
+ field(name, (BytesReference) value);
+ } else if (value instanceof Text) {
+ field(name, (Text) value);
} else if (value instanceof ToXContent) {
field(name, (ToXContent) value);
} else {

0 comments on commit 7956d14

Please sign in to comment.
Something went wrong with that request. Please try again.