Map<String, Map<String, String>> mapFields = Maps.newHashMap();
byte[] rawPayload = 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
id = jp.getText();
} else if ("simpleFields".equals(fieldname)) {
while (jp.nextToken() != JsonToken.END_OBJECT) {
String key = jp.getCurrentName();
jp.nextToken(); // move to value
simpleFields.put(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();
mapFields.put(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
mapFields.get(key).put(mapKey, jp.getText());
}
}
} else if ("listFields".equals(fieldname)) {
// user.setUserImage(jp.getBinaryValue());
while (jp.nextToken() != JsonToken.END_OBJECT) {
String key = jp.getCurrentName();
listFields.put(key, new ArrayList<String>());
jp.nextToken(); // move to value
while (jp.nextToken() != JsonToken.END_ARRAY) {
listFields.get(key).add(jp.getText());
}
}
} else if ("rawPayload".equals(fieldname)) {
rawPayload = Base64.decodeBase64(jp.getText());
} else {
throw new IllegalStateException("Unrecognized field '" + fieldname + "'!");
}
}
jp.close(); // ensure resources get cleaned up timely and properly
if (id == null) {
throw new IllegalStateException("ZNRecord id field is required!");
}
record = new ZNRecord(id);