writeFieldName(fieldName); writeString(value);
Note: many performance-sensitive implementations override this method
21112112211321142115211621172118211921202121
String[] resources = config.updatingResource.get(item.getKey()); String resource = UNKNOWN_RESOURCE; if(resources != null && resources.length > 0) { resource = resources[0]; } dumpGenerator.writeStringField("resource", resource); dumpGenerator.writeEndObject(); } } dumpGenerator.writeEndArray(); dumpGenerator.writeEndObject();
8485868788899091929394
g.writeStartObject(); // write id field g.writeRaw("\n "); g.writeStringField("id", record.getId()); // write simepleFields g.writeRaw("\n "); g.writeObjectFieldStart("simpleFields"); for (String key : record.getSimpleFields().keySet()) {
919293949596979899100101
// write simepleFields g.writeRaw("\n "); g.writeObjectFieldStart("simpleFields"); for (String key : record.getSimpleFields().keySet()) { g.writeRaw("\n "); g.writeStringField(key, record.getSimpleField(key)); } g.writeRaw("\n "); g.writeEndObject(); // for simpleFields // write listFields
126127128129130131132133134135136
g.writeRaw("\n "); g.writeObjectFieldStart(key); Map<String, String> map = record.getMapField(key); for (String mapKey : map.keySet()) { g.writeRaw("\n "); g.writeStringField(mapKey, map.get(mapKey)); } g.writeRaw("\n "); g.writeEndObject(); }
139140141142143144145146147148149
byte[] rawPayload = record.getRawPayload(); if (rawPayload != null && rawPayload.length > 0) { // write rawPayload g.writeRaw("\n "); g.writeStringField("rawPayload", new String(Base64.encodeBase64(rawPayload), "UTF-8")); } g.writeRaw("\n"); g.writeEndObject(); // for whole znrecord
31283129313031313132313331343135313631373138
g.writeFieldName(cfEntry.getKey()); g.writeStartArray(); for (Map.Entry<String, String> asEntry : cfEntry.getValue().entrySet()) { g.writeStartObject(); g.writeStringField(asEntry.getKey(), asEntry.getValue()); g.writeEndObject(); } g.writeEndArray(); g.writeEndObject(); }
157158159160161162163164165166167
JsonFactory jsonFactory = new JsonFactory(); JsonGenerator jg = jsonFactory.createJsonGenerator(writer); jg.useDefaultPrettyPrinter(); jg.writeStartObject(); if (mBeanServer == null) { jg.writeStringField("result", "ERROR"); jg.writeStringField("message", "No MBeanServer could be found"); jg.close(); LOG.error("No MBeanServer could be found."); response.setStatus(HttpServletResponse.SC_NOT_FOUND); return;
5455565758596061626364
xsonGen.writeStartObject(); jsonGen.writeStartObject(); xsonGen.writeStringField("test", "value"); jsonGen.writeStringField("test", "value"); xsonGen.writeArrayFieldStart("arr"); jsonGen.writeArrayFieldStart("arr"); xsonGen.writeNumber(1); jsonGen.writeNumber(1);
4041424344454647484950
@Test public void verifyReuseJsonGenerator() throws Exception { FastCharArrayWriter writer = new FastCharArrayWriter(); XContentGenerator generator = XContentFactory.xContent(XContentType.JSON).createGenerator(writer); generator.writeStartObject(); generator.writeStringField("test", "value"); generator.writeEndObject(); generator.flush(); assertThat(writer.toStringTrim(), equalTo("{\"test\":\"value\"}"));
4950515253545556575859
assertThat(writer.toStringTrim(), equalTo("{\"test\":\"value\"}")); // try again... writer.reset(); generator.writeStartObject(); generator.writeStringField("test", "value"); generator.writeEndObject(); generator.flush(); // we get a space at the start here since it thinks we are not in the root object (fine, we will ignore it in the real code we use) assertThat(writer.toStringTrim(), equalTo("{\"test\":\"value\"}")); }