private void writeObjectArray(Object[] value) throws IOException { generator.writeStartArray(); for (Object v : value) { writeValue(v); } generator.writeEndArray(); }
public XContentBuilder field(String name, Float value) throws IOException { field(name); if (value == null) { generator.writeNull(); } else { generator.writeNumber(value.floatValue()); } return this; }
public XContentBuilder field(String name, byte[] value) throws IOException { field(name); if (value == null) { generator.writeNull(); } else { generator.writeBinary(value); } return this; }
public XContentBuilder field(String name, char[] value, int offset, int length) throws IOException { field(name); if (value == null) { generator.writeNull(); } else { generator.writeString(value, offset, length); } return this; }
private void writeMap(Map<String, ?> map) throws IOException { generator.writeStartObject(); for (Map.Entry<String, ?> entry : map.entrySet()) { field(entry.getKey()); Object value = entry.getValue(); if (value == null) { generator.writeNull(); } else { writeValue(value); } } generator.writeEndObject(); }
public XContentBuilder field(String name) throws IOException { if (name == null) { throw new IllegalArgumentException("field name cannot be null"); } generator.writeFieldName(name); return this; }
public XContentBuilder value(byte[] value, int offset, int length) throws IOException { if (value == null) { return nullValue(); } generator.writeBinary(value, offset, length); return this; }
/** * Writes the binary content of the given BytesRef Use {@link * org.elasticsearch.common.xcontent.XContentParser#binaryValue()} to read the value back */ public XContentBuilder value(BytesRef value) throws IOException { if (value == null) { return nullValue(); } generator.writeBinary(value.bytes, value.offset, value.length); return this; }
public XContentBuilder value(String value) throws IOException { if (value == null) { return nullValue(); } generator.writeString(value); return this; }
private void writeBytesReference(BytesReference value) throws IOException { BytesReference bytes = value; if (!bytes.hasArray()) { bytes = bytes.toBytesArray(); } generator.writeBinary(bytes.array(), bytes.arrayOffset(), bytes.length()); }
public XContentBuilder value(byte[] value) throws IOException { if (value == null) { return nullValue(); } generator.writeBinary(value); return this; }
public XContentBuilder field(String 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; }
@Override public void close() { try { generator.close(); } catch (IOException e) { // ignore } }
/** * Writes the binary content of the given BytesReference Use {@link * org.elasticsearch.common.xcontent.XContentParser#binaryValue()} to read the value back */ public XContentBuilder field(String 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 value(Text value) throws IOException { if (value == null) { return nullValue(); } 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; } BytesArray bytesArray = value.bytes().toBytesArray(); generator.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length()); return this; }
public XContentBuilder field( String name, BigDecimal value, int scale, RoundingMode rounding, boolean toDouble) throws IOException { field(name); if (value == null) { generator.writeNull(); } else { if (toDouble) { try { generator.writeNumber(value.setScale(scale, rounding).doubleValue()); } catch (ArithmeticException e) { generator.writeString(value.toEngineeringString()); } } else { generator.writeString(value.toEngineeringString()); } } return this; }
/** * Writes the binary content of the given BytesReference Use {@link * org.elasticsearch.common.xcontent.XContentParser#binaryValue()} to read the value back */ public XContentBuilder value(BytesReference value) throws IOException { if (value == null) { return nullValue(); } if (!value.hasArray()) { value = value.toBytesArray(); } generator.writeBinary(value.array(), value.arrayOffset(), value.length()); return this; }
private void writeValue(Object value) throws IOException { if (value == null) { generator.writeNull(); return; } Class<?> type = value.getClass(); Writer writer = MAP.get(type); if (writer != null) { writer.write(generator, value); } else if (value instanceof Map) { writeMap((Map) value); } else if (value instanceof Path) { // Path implements Iterable<Path> and causes endless recursion and a StackOverFlow if treated // as an Iterable here generator.writeString(value.toString()); } else if (value instanceof Iterable) { writeIterable((Iterable<?>) value); } else if (value instanceof Object[]) { writeObjectArray((Object[]) value); } else if (value instanceof Date) { generator.writeString(XContentBuilder.defaultDatePrinter.print(((Date) value).getTime())); } else if (value instanceof Calendar) { generator.writeString( XContentBuilder.defaultDatePrinter.print((((Calendar) value)).getTimeInMillis())); } else if (value instanceof ReadableInstant) { generator.writeString( XContentBuilder.defaultDatePrinter.print((((ReadableInstant) value)).getMillis())); } else if (value instanceof BytesReference) { writeBytesReference((BytesReference) value); } else if (value instanceof ToXContent) { ((ToXContent) value).toXContent(this, ToXContent.EMPTY_PARAMS); } else { // if this is a "value" object, like enum, DistanceUnit, ..., just toString it // yea, it can be misleading when toString a Java class, but really, jackson should be used in // that case generator.writeString(value.toString()); // throw new ElasticsearchIllegalArgumentException("type not supported for generic value // conversion: " + type); } }
public XContentBuilder flush() throws IOException { generator.flush(); return this; }
public XContentBuilder copyCurrentStructure(XContentParser parser) throws IOException { generator.copyCurrentStructure(parser); return this; }
public XContentBuilder rawField(String fieldName, BytesReference content) throws IOException { generator.writeRawField(fieldName, content); return this; }
/** * Writes the binary content of the given BytesRef as UTF-8 bytes Use {@link * XContentParser#utf8Bytes()} to read the value back */ public XContentBuilder utf8Field(String name, BytesRef value) throws IOException { field(name); generator.writeUTF8String(value.bytes, value.offset, value.length); return this; }
public XContentBuilder nullField(String name) throws IOException { generator.writeNullField(name); return this; }
public XContentBuilder field(String name, byte[] value, int offset, int length) throws IOException { field(name); generator.writeBinary(value, offset, length); return this; }
public XContentBuilder nullValue() throws IOException { generator.writeNull(); return this; }
public XContentBuilder value(double value) throws IOException { generator.writeNumber(value); return this; }
public XContentBuilder rawValue(BytesReference content) throws IOException { generator.writeRawValue(content); return this; }
public XContentBuilder field(String name, boolean value) throws IOException { field(name); generator.writeBoolean(value); return this; }
public XContentBuilder value(boolean value) throws IOException { generator.writeBoolean(value); return this; }
public XContentBuilder rawField(String fieldName, InputStream content) throws IOException { generator.writeRawField(fieldName, content); return this; }