if (!metadata.isObject()) {
throw new JsonFormatException("The value for the metadata should be an object, field: " + recordField);
}
ObjectNode object = (ObjectNode)metadata;
MetadataBuilder builder = new MetadataBuilder();
Iterator<Map.Entry<String, JsonNode>> it = object.getFields();
while (it.hasNext()) {
Map.Entry<String, JsonNode> entry = it.next();
String name = entry.getKey();
JsonNode value = entry.getValue();
if (value.isTextual()) {
builder.value(name, value.getTextValue());
} else if (value.isInt()) {
builder.value(name, value.getIntValue());
} else if (value.isLong()) {
builder.value(name, value.getLongValue());
} else if (value.isBoolean()) {
builder.value(name, value.getBooleanValue());
} else if (value.isFloatingPointNumber()) {
// In the JSON format, for simplicity, we don't make distinction between float & double, so you
// can't control which of the two is created.
builder.value(name, value.getDoubleValue());
} else if (value.isObject()) {
String type = JsonUtil.getString(value, "type", null);
if (type == null) {
throw new JsonFormatException("Missing required 'type' property on object in metadata field '"
+ name + "' of record field " + recordField);
}
if (type.equals("binary")) {
JsonNode binaryValue = value.get("value");
if (!binaryValue.isTextual()) {
throw new JsonFormatException("Invalid binary value for metadata field '"
+ name + "' of record field " + recordField);
}
try {
builder.value(name, new ByteArray(binaryValue.getBinaryValue()));
} catch (IOException e) {
throw new JsonFormatException("Invalid binary value for metadata field '"
+ name + "' of record field " + recordField);
}
} else if (type.equals("datetime")) {
JsonNode datetimeValue = value.get("value");
if (!datetimeValue.isTextual()) {
throw new JsonFormatException("Invalid datetime value for metadata field '"
+ name + "' of record field " + recordField);
}
try {
builder.value(name, ISODateTimeFormat.dateTime().parseDateTime(datetimeValue.getTextValue()));
} catch (Exception e) {
throw new JsonFormatException("Invalid datetime value for metadata field '"
+ name + "' of record field " + recordField);
}
} else {