ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ZNRecord record = null;
try {
JsonFactory f = new JsonFactory();
JsonParser jp = f.createJsonParser(bais);
jp.nextToken(); // will return JsonToken.START_OBJECT (verify?)
while (jp.nextToken() != JsonToken.END_OBJECT) {
String fieldname = jp.getCurrentName();
jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY
if ("id".equals(fieldname)) {
// contains an object
record = new ZNRecord(jp.getText());
} else if ("simpleFields".equals(fieldname)) {
while (jp.nextToken() != JsonToken.END_OBJECT) {
String key = jp.getCurrentName();
jp.nextToken(); // move to value
record.setSimpleField(key, jp.getText());
}
} else if ("mapFields".equals(fieldname)) {
// user.setVerified(jp.getCurrentToken() == JsonToken.VALUE_TRUE);
while (jp.nextToken() != JsonToken.END_OBJECT) {
String key = jp.getCurrentName();
record.setMapField(key, new TreeMap<String, String>());
jp.nextToken(); // move to value
while (jp.nextToken() != JsonToken.END_OBJECT) {
String mapKey = jp.getCurrentName();
jp.nextToken(); // move to value
record.getMapField(key).put(mapKey, jp.getText());
}
}
} else if ("listFields".equals(fieldname)) {
// user.setUserImage(jp.getBinaryValue());
while (jp.nextToken() != JsonToken.END_OBJECT) {
String key = jp.getCurrentName();
record.setListField(key, new ArrayList<String>());
jp.nextToken(); // move to value
while (jp.nextToken() != JsonToken.END_ARRAY) {
record.getListField(key).add(jp.getText());
}
}
} else if ("rawPayload".equals(fieldname)) {
record.setRawPayload(Base64.decode(jp.getText()));
} else {
throw new IllegalStateException("Unrecognized field '" + fieldname + "'!");
}
}
jp.close(); // ensure resources get cleaned up timely and properly
} catch (Exception e) {
LOG.error("Exception during deserialization of bytes: " + new String(bytes), e);
}
return record;