use of org.candlepin.common.exceptions.CandlepinJsonProcessingException in project candlepin by candlepin.
the class CandlepinAttributeDeserializer method deserialize.
@Override
public Map<String, String> deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
Map<String, String> output = new HashMap<>();
TreeNode node = parser.readValueAsTree();
if (node.isObject()) {
log.debug("Processing attributes as a mapping of key/value pairs");
// This is what we want, key/value pairs (hopefully).
for (Iterator<String> fieldNames = node.fieldNames(); fieldNames.hasNext(); ) {
String field = fieldNames.next();
TreeNode valueNode = node.get(field);
if (!valueNode.isValueNode()) {
throw new CandlepinJsonProcessingException("Unexpected value type in map: " + valueNode.asToken(), parser.getCurrentLocation());
}
JsonParser subparser = valueNode.traverse();
subparser.nextValue();
String value = subparser.getValueAsString();
subparser.close();
log.debug("Found key/value pair: {} = {}", field, value);
output.put(field, value);
}
} else if (node.isArray()) {
log.debug("Processing attributes as an array of attribute objects");
// Probably old collection of objects containing name/value attribute.
// Iterate over the objects, adding the values to the map.
int size = node.size();
for (int i = 0; i < size; ++i) {
TreeNode obj = node.get(i);
if (!obj.isObject()) {
throw new CandlepinJsonProcessingException("Unexpected value type in array: " + obj.asToken(), parser.getCurrentLocation());
}
TreeNode fieldNode = obj.get("name");
if (fieldNode == null) {
throw new CandlepinJsonProcessingException("No attribute name defined in attribute object", parser.getCurrentLocation());
}
if (!fieldNode.isValueNode()) {
throw new CandlepinJsonProcessingException("Unexpected value type for attribute name: " + fieldNode.asToken(), parser.getCurrentLocation());
}
JsonParser subparser = fieldNode.traverse();
subparser.nextValue();
String field = subparser.getValueAsString();
subparser.close();
TreeNode valueNode = obj.get("value");
if (valueNode != null) {
if (!valueNode.isValueNode()) {
throw new CandlepinJsonProcessingException("Unexpected value type for attribute value: " + valueNode.asToken(), parser.getCurrentLocation());
}
subparser = valueNode.traverse();
subparser.nextValue();
String value = subparser.getValueAsString();
subparser.close();
log.debug("Found key/value pair: {} = {}", field, value);
output.put(field, value);
} else {
log.debug("Found key/value pair: {} = {}", field, null);
output.put(field, null);
}
}
} else {
log.debug("Processing attributes as an array of attribute objects");
// Uh oh.
throw new CandlepinJsonProcessingException("Unexpected attribute value type: " + node.asToken(), parser.getCurrentLocation());
}
return output;
}
use of org.candlepin.common.exceptions.CandlepinJsonProcessingException in project candlepin by candlepin.
the class SingleValueWrapDeserializer method deserialize.
@Override
public String deserialize(JsonParser parser, DeserializationContext context) throws IOException {
TreeNode node = parser.readValueAsTree();
if (node.isObject()) {
log.debug("Processing {} as a containing object node.", this.fieldName);
TreeNode valueNode = node.path(this.fieldName);
if (valueNode.isMissingNode()) {
throw new CandlepinJsonProcessingException("The field " + this.fieldName + " is missing from: " + node.asToken(), parser.getCurrentLocation());
}
return parseValueNode(valueNode);
} else if (node.isValueNode()) {
log.debug("Processing {} as a value node.", this.fieldName);
return parseValueNode(node);
} else {
// Uh oh.
throw new CandlepinJsonProcessingException("Unexpected " + this.fieldName + " node type: " + node.asToken(), parser.getCurrentLocation());
}
}
Aggregations