}
StringWriter sw = new StringWriter();
try {
JsonFactory f = new JsonFactory();
JsonGenerator g = f.createJsonGenerator(sw);
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()) {
g.writeRaw("\n ");
g.writeStringField(key, record.getSimpleField(key));
}
g.writeRaw("\n ");
g.writeEndObject(); // for simpleFields
// write listFields
g.writeRaw("\n ");
g.writeObjectFieldStart("listFields");
for (String key : record.getListFields().keySet()) {
// g.writeStringField(key, record.getListField(key).toString());
// g.writeObjectFieldStart(key);
g.writeRaw("\n ");
g.writeArrayFieldStart(key);
List<String> list = record.getListField(key);
for (String listValue : list) {
g.writeString(listValue);
}
// g.writeEndObject();
g.writeEndArray();
}
g.writeRaw("\n ");
g.writeEndObject(); // for listFields
// write mapFields
g.writeRaw("\n ");
g.writeObjectFieldStart("mapFields");
for (String key : record.getMapFields().keySet()) {
// g.writeStringField(key, record.getMapField(key).toString());
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();
}
g.writeRaw("\n ");
g.writeEndObject(); // for mapFields
byte[] rawPayload = record.getRawPayload();
if (rawPayload != null && rawPayload.length > 0) {
// write rawPayload
g.writeRaw("\n ");
g.writeStringField("rawPayload", Base64.encode(rawPayload, false));
}
g.writeRaw("\n");
g.writeEndObject(); // for whole znrecord
// important: will force flushing of output, close underlying output
// stream
g.close();
} catch (Exception e) {
LOG.error("Exception during data serialization. Will not write to zk. Data (first 1k): "
+ sw.toString().substring(0, 1024), e);
throw new HelixException(e);
}