Malformed Json for an object with a rawfield only using XContentBuilder


(Andrea Zinicola) #1

Hi,

I couldn't find a way to create a valid document using the XContentBuilder
including a raw field only.

Following the code I used:

  jsonStr = "{\"field\":\"value\"}";
  XContentBuilder xb = jsonBuilder()
      .startObject()
      .rawField("object_name", jsonStr.getBytes())
   .endObject();

This code resulted in a malformed json document { , "object_name" :
{"field":"value"} }. Looking at the JsonXContentGenerator implementation of
writeRawField it looks like this is what the code is expected to do, in
fact:

@Override
public void writeRawField(String fieldName, byte[] content, 

OutputStream bos) throws IOException {
generator.writeRaw(", "");
generator.writeRaw(fieldName);
generator.writeRaw("" : ");
flush();
bos.write(content);
}

In order to overcome this I had to add a dummy empty field to the document:

  XContentBuilder xb = jsonBuilder()
      .startObject()
      .field("dummy", "")
      .rawField(DOC_OBJECT_NAME, jsonStr.getBytes())
   .endObject();

I wonder if there is any other method allowing to add a raw field only to a
document using XContentBuilder or not.

Thank you in advance,

Andrea

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/1eb49552-df19-4548-9696-534c1c3428b7%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(system) #2