Пример #1
0
 private void writeObjectArray(Object[] value) throws IOException {
   generator.writeStartArray();
   for (Object v : value) {
     writeValue(v);
   }
   generator.writeEndArray();
 }
Пример #2
0
 public XContentBuilder field(String name, Float value) throws IOException {
   field(name);
   if (value == null) {
     generator.writeNull();
   } else {
     generator.writeNumber(value.floatValue());
   }
   return this;
 }
Пример #3
0
 public XContentBuilder field(String name, byte[] value) throws IOException {
   field(name);
   if (value == null) {
     generator.writeNull();
   } else {
     generator.writeBinary(value);
   }
   return this;
 }
Пример #4
0
 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;
 }
Пример #5
0
  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();
  }
Пример #6
0
 public XContentBuilder field(String name) throws IOException {
   if (name == null) {
     throw new IllegalArgumentException("field name cannot be null");
   }
   generator.writeFieldName(name);
   return this;
 }
Пример #7
0
 public XContentBuilder value(byte[] value, int offset, int length) throws IOException {
   if (value == null) {
     return nullValue();
   }
   generator.writeBinary(value, offset, length);
   return this;
 }
Пример #8
0
 /**
  * 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;
 }
Пример #9
0
 public XContentBuilder value(String value) throws IOException {
   if (value == null) {
     return nullValue();
   }
   generator.writeString(value);
   return this;
 }
Пример #10
0
 private void writeBytesReference(BytesReference value) throws IOException {
   BytesReference bytes = value;
   if (!bytes.hasArray()) {
     bytes = bytes.toBytesArray();
   }
   generator.writeBinary(bytes.array(), bytes.arrayOffset(), bytes.length());
 }
Пример #11
0
 public XContentBuilder value(byte[] value) throws IOException {
   if (value == null) {
     return nullValue();
   }
   generator.writeBinary(value);
   return this;
 }
Пример #12
0
 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;
 }
Пример #13
0
 @Override
 public void close() {
   try {
     generator.close();
   } catch (IOException e) {
     // ignore
   }
 }
Пример #14
0
 /**
  * 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;
 }
Пример #15
0
 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;
 }
Пример #16
0
 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;
 }
Пример #17
0
 /**
  * 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;
 }
Пример #18
0
 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);
   }
 }
Пример #19
0
 public XContentBuilder flush() throws IOException {
   generator.flush();
   return this;
 }
Пример #20
0
 public XContentBuilder copyCurrentStructure(XContentParser parser) throws IOException {
   generator.copyCurrentStructure(parser);
   return this;
 }
Пример #21
0
 public XContentBuilder rawField(String fieldName, BytesReference content) throws IOException {
   generator.writeRawField(fieldName, content);
   return this;
 }
Пример #22
0
 /**
  * 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;
 }
Пример #23
0
 public XContentBuilder nullField(String name) throws IOException {
   generator.writeNullField(name);
   return this;
 }
Пример #24
0
 public XContentBuilder field(String name, byte[] value, int offset, int length)
     throws IOException {
   field(name);
   generator.writeBinary(value, offset, length);
   return this;
 }
Пример #25
0
 public XContentBuilder nullValue() throws IOException {
   generator.writeNull();
   return this;
 }
Пример #26
0
 public XContentBuilder value(double value) throws IOException {
   generator.writeNumber(value);
   return this;
 }
Пример #27
0
 public XContentBuilder rawValue(BytesReference content) throws IOException {
   generator.writeRawValue(content);
   return this;
 }
Пример #28
0
 public XContentBuilder field(String name, boolean value) throws IOException {
   field(name);
   generator.writeBoolean(value);
   return this;
 }
Пример #29
0
 public XContentBuilder value(boolean value) throws IOException {
   generator.writeBoolean(value);
   return this;
 }
Пример #30
0
 public XContentBuilder rawField(String fieldName, InputStream content) throws IOException {
   generator.writeRawField(fieldName, content);
   return this;
 }