add field with text/bytes and builder string

This commit is contained in:
Shay Banon 2012-09-06 20:47:13 +02:00
parent b26fd600f0
commit 8038616fbc
1 changed files with 30 additions and 2 deletions

View File

@ -445,7 +445,7 @@ public final class XContentBuilder implements BytesStream {
if (value == null) { if (value == null) {
generator.writeNull(); generator.writeNull();
} else { } else {
generator.writeNumber(value.doubleValue()); generator.writeNumber(value);
} }
return this; return this;
} }
@ -455,7 +455,7 @@ public final class XContentBuilder implements BytesStream {
if (value == null) { if (value == null) {
generator.writeNull(); generator.writeNull();
} else { } else {
generator.writeNumber(value.doubleValue()); generator.writeNumber(value);
} }
return this; return this;
} }
@ -517,6 +517,15 @@ public final class XContentBuilder implements BytesStream {
return this; 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 { public XContentBuilder field(String name, Text value) throws IOException {
field(name); field(name);
if (value.hasBytes() && value.bytes().hasArray()) { if (value.hasBytes() && value.bytes().hasArray()) {
@ -533,6 +542,21 @@ public final class XContentBuilder implements BytesStream {
return this; 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 { public XContentBuilder field(String name, byte[] value, int offset, int length) throws IOException {
field(name); field(name);
@ -722,6 +746,10 @@ public final class XContentBuilder implements BytesStream {
field(name, (float[]) value); field(name, (float[]) value);
} else if (value instanceof double[]) { } else if (value instanceof double[]) {
field(name, (double[]) value); 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) { } else if (value instanceof ToXContent) {
field(name, (ToXContent) value); field(name, (ToXContent) value);
} else { } else {