Пример #1
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;
 }
Пример #2
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;
 }
Пример #3
0
 static {
   Map<Class<?>, Writer> map = new HashMap<>();
   map.put(String.class, (g, v) -> g.writeString((String) v));
   map.put(Integer.class, (g, v) -> g.writeNumber((Integer) v));
   map.put(Long.class, (g, v) -> g.writeNumber((Long) v));
   map.put(Float.class, (g, v) -> g.writeNumber((Float) v));
   map.put(Double.class, (g, v) -> g.writeNumber((Double) v));
   map.put(Byte.class, (g, v) -> g.writeNumber((Byte) v));
   map.put(Short.class, (g, v) -> g.writeNumber((Short) v));
   map.put(Boolean.class, (g, v) -> g.writeBoolean((Boolean) v));
   map.put(
       GeoPoint.class,
       (g, v) -> {
         g.writeStartObject();
         g.writeNumberField("lat", ((GeoPoint) v).lat());
         g.writeNumberField("lon", ((GeoPoint) v).lon());
         g.writeEndObject();
       });
   map.put(
       int[].class,
       (g, v) -> {
         g.writeStartArray();
         for (int item : (int[]) v) {
           g.writeNumber(item);
         }
         g.writeEndArray();
       });
   map.put(
       long[].class,
       (g, v) -> {
         g.writeStartArray();
         for (long item : (long[]) v) {
           g.writeNumber(item);
         }
         g.writeEndArray();
       });
   map.put(
       float[].class,
       (g, v) -> {
         g.writeStartArray();
         for (float item : (float[]) v) {
           g.writeNumber(item);
         }
         g.writeEndArray();
       });
   map.put(
       double[].class,
       (g, v) -> {
         g.writeStartArray();
         for (double item : (double[]) v) {
           g.writeNumber(item);
         }
         g.writeEndArray();
       });
   map.put(byte[].class, (g, v) -> g.writeBinary((byte[]) v));
   map.put(
       short[].class,
       (g, v) -> {
         g.writeStartArray();
         for (short item : (short[]) v) {
           g.writeNumber(item);
         }
         g.writeEndArray();
       });
   map.put(
       BytesRef.class,
       (g, v) -> {
         BytesRef bytes = (BytesRef) v;
         g.writeBinary(bytes.bytes, bytes.offset, bytes.length);
       });
   map.put(
       Text.class,
       (g, v) -> {
         Text text = (Text) v;
         if (text.hasBytes() && text.bytes().hasArray()) {
           g.writeUTF8String(
               text.bytes().array(), text.bytes().arrayOffset(), text.bytes().length());
         } else if (text.hasString()) {
           g.writeString(text.string());
         } else {
           BytesArray bytesArray = text.bytes().toBytesArray();
           g.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length());
         }
       });
   MAP = Collections.unmodifiableMap(map);
 }
Пример #4
0
 /** Returns a string representation of the builder (only applicable for text based xcontent). */
 public String string() throws IOException {
   close();
   BytesArray bytesArray = bytes().toBytesArray();
   return new String(
       bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length(), StandardCharsets.UTF_8);
 }