writeFieldName(fieldName); writeString(value);
Note: many performance-sensitive implementations override this method
7778798081828384858687
@Test public void testWritingBinaryToStream() throws Exception { FastByteArrayOutputStream bos = new FastByteArrayOutputStream(); XContentGenerator gen = XContentFactory.xContent(XContentType.JSON).createGenerator(bos); gen.writeStartObject(); gen.writeStringField("name", "something"); gen.flush(); bos.write(", source : { test : \"value\" }".getBytes("UTF8")); gen.writeStringField("name2", "something2"); gen.writeEndObject(); gen.close();
8081828384858687888990
XContentGenerator gen = XContentFactory.xContent(XContentType.JSON).createGenerator(bos); gen.writeStartObject(); gen.writeStringField("name", "something"); gen.flush(); bos.write(", source : { test : \"value\" }".getBytes("UTF8")); gen.writeStringField("name2", "something2"); gen.writeEndObject(); gen.close(); byte[] data = bos.copiedByteArray(); String sData = new String(data, "UTF8");