下面列出了com.fasterxml.jackson.core.ObjectCodec# readTree ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Ports deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
Ports out = new Ports();
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext();) {
Map.Entry<String, JsonNode> field = it.next();
if (!field.getValue().equals(NullNode.getInstance())) {
String hostIp = field.getValue().get(0).get("HostIp").textValue();
String hostPort = field.getValue().get(0).get("HostPort").textValue();
out.addPort(Port.makePort(field.getKey(), hostIp, hostPort));
}
}
return out;
}
@Override
public T deserialize( JsonParser p, DeserializationContext ctxt ) throws IOException
{
final ObjectCodec oc = p.getCodec();
final JsonNode jsonNode;
if ( oc instanceof XmlMapper )
{
jsonNode = createJsonNode( p, ctxt );
return objectMapper.treeToValue( jsonNode, overrideClass );
}
else
{
jsonNode = oc.readTree( p );
// original object mapper must be used since it may have different serialization settings
return oc.treeToValue( jsonNode, overrideClass );
}
}
@Override
public SourceForgeArticle deserialize(JsonParser parser,
DeserializationContext context) throws IOException,
JsonProcessingException {
ObjectCodec oc = parser.getCodec();
JsonNode node = oc.readTree(parser);
JsonNode attachmentsNode = node.path("attachments");
SourceForgeAttachment[] attachments = oc.treeToValue(attachmentsNode, SourceForgeAttachment[].class);
SourceForgeArticle article = new SourceForgeArticle();
article.setArticleNumber(articleNumber++);
article.setSubject(getText(node, "subject"));
article.setText(getText(node, "text"));
article.setUser(getText(node,"author"));
article.setAttachments(attachments);
article.setArticleId(getText(node, "slug"));
article.setDate(getDate(node, SourceForgeConstants.RESPONSE_DATE_FORMATTER, "timestamp"));
article.setUpdateDate(getDate(node, SourceForgeConstants.RESPONSE_DATE_FORMATTER, "last_edited"));
article.setReferences(new String[0]);
return article;
}
@Override
public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
if (node instanceof ArrayNode) {
ArrayNode arrayNode = (ArrayNode) node;
ArrayList<String> ret = new ArrayList<>();
for (int i = 0; i < arrayNode.size(); i++) {
ret.add(arrayNode.get(i).textValue());
}
return ret;
} else {
return Arrays.asList(node.textValue());
}
}
@Override
public List<Pair<AccountName, Long>> deserialize(JsonParser jsonParser,
DeserializationContext deserializationContext) throws IOException {
List<Pair<AccountName, Long>> result = new ArrayList<>();
ObjectCodec codec = jsonParser.getCodec();
TreeNode rootNode = codec.readTree(jsonParser);
if (rootNode.isArray()) {
for (JsonNode node : (ArrayNode) rootNode) {
// result.put((node.get(0)).asText(), (node.get(0)).asInt());
}
return result;
}
throw new IllegalArgumentException("JSON Node is not an array.");
}
@Override
public ExposedPorts deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException {
List<ExposedPort> exposedPorts = new ArrayList<>();
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext(); ) {
Map.Entry<String, JsonNode> field = it.next();
if (!field.getValue().equals(NullNode.getInstance())) {
exposedPorts.add(ExposedPort.parse(field.getKey()));
}
}
return new ExposedPorts(exposedPorts.toArray(new ExposedPort[0]));
}
@Override
public Map<String, Integer> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException {
HashMap<String, Integer> result = new HashMap<>();
ObjectCodec codec = jsonParser.getCodec();
TreeNode rootNode = codec.readTree(jsonParser);
if (rootNode.isArray()) {
for (JsonNode node : (ArrayNode) rootNode) {
result.put((node.get(0)).asText(), (node.get(0)).asInt());
}
return result;
}
throw new IllegalArgumentException("JSON Node is not an array.");
}
@Override
public CoinbasePrice deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
final ObjectCodec oc = jp.getCodec();
final JsonNode node = oc.readTree(jp);
final CoinbaseMoney subTotal =
CoinbaseMoneyDeserializer.getCoinbaseMoneyFromNode(node.path("subtotal"));
final JsonNode feesNode = node.path("fees");
final CoinbaseMoney coinbaseFee =
CoinbaseMoneyDeserializer.getCoinbaseMoneyFromNode(feesNode.path(0).path("coinbase"));
final CoinbaseMoney bankFee =
CoinbaseMoneyDeserializer.getCoinbaseMoneyFromNode(feesNode.path(1).path("bank"));
final CoinbaseMoney total =
CoinbaseMoneyDeserializer.getCoinbaseMoneyFromNode(node.path("total"));
return new CoinbasePrice(coinbaseFee, bankFee, total, subTotal);
}
@Override
public SourceForgeForumSearch deserialize(JsonParser parser,
DeserializationContext context) throws IOException,
JsonProcessingException {
// System.err.println("SourceForgeGetDeserialiser: started");
ObjectCodec oc = parser.getCodec();
JsonNode node = oc.readTree(parser);
SourceForgeForumSearch result = new SourceForgeForumSearch();
result.setCount(node.get("count").asInt());
// System.err.println("SourceForgeGetDeserialiser: count " + node.get("count").asInt());
Iterator<JsonNode> forums = node.path("forums").iterator();
while (forums.hasNext()) {
JsonNode forum = forums.next();
result.addForumId(forum.get("shortname").asInt());
// System.err.println("SourceForgeGetDeserialiser: forumId " + forum.get("shortname").asInt());
}
return result;
}
@Override
public RedmineComment deserialize(JsonParser parser,
DeserializationContext context) throws IOException,
JsonProcessingException {
ObjectCodec oc = parser.getCodec();
JsonNode node = oc.readTree(parser);
RedmineComment comment = new RedmineComment();
comment.setCommentId(getText(node, "id"));
comment.setText(getText(node, "notes"));
comment.setCreationTime(getDate(node, context, "created_on"));
comment.setCreator(getText(node, "user", "name"));
comment.setCreatorId(getInteger(node, "user", "id") );
JsonNode detailsNode = node.get("details");
if (null != detailsNode) {
RedmineCommentDetails[] details = oc.treeToValue(detailsNode,
RedmineCommentDetails[].class);
for (RedmineCommentDetails d : details) {
comment.getDetails().add(d);
}
}
return comment;
}
/**
* Deserializes a Transformation YAML description of the type
*
* <pre>
* shell:
* - _on: start
* cmd: /bin/date
* - _on: end
* cmd: /bin/echo "Finished"
* </pre>
*
* @param parser
* @param dc
* @return
* @throws IOException
* @throws JsonProcessingException
*/
@Override
public Notifications deserialize(JsonParser parser, DeserializationContext dc)
throws IOException, JsonProcessingException {
ObjectCodec oc = parser.getCodec();
JsonNode node = oc.readTree(parser);
Notifications notifications = new Notifications();
for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext(); ) {
Map.Entry<String, JsonNode> e = it.next();
String key = e.getKey();
WorkflowKeywords reservedKey = WorkflowKeywords.getReservedKey(key);
if (reservedKey == null) {
this.complainForIllegalKey(WorkflowKeywords.HOOKS.getReservedName(), key, node);
}
notifications.addAll(this.createNotifications(key, node.get(key)));
}
return notifications;
}
@Override
public CoinbaseCurrency deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
ObjectCodec oc = jp.getCodec();
JsonNode node = oc.readTree(jp);
String name = node.get("name").asText();
String id = node.get("id").asText();
return new CoinbaseCurrency(name, id);
}
@Override
public Rectangle2D.Double deserialize(JsonParser jsonParser,
DeserializationContext deserializationContext) throws IOException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
String[] params = node.get(KEY_SURFACE_BOUNDS).asText().split(CELL_PTRN);
return new Rectangle2D.Double(
Double.parseDouble(params[X - 1]),
Double.parseDouble(params[Y - 1]),
Double.parseDouble(params[W - 1]),
Double.parseDouble(params[H - 1]));
}
@Override
public Sha256Hash deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
ObjectCodec oc = jp.getCodec();
JsonNode node = oc.readTree(jp);
Sha256Hash hash = Sha256Hash.fromString(node.asText());
if (hash == null) {
throw new JsonParseException("Failed to convert string '" + node.asText() + "' into a Sha256 hash",
JsonLocation.NA);
}
return hash;
}
@Override
public TimeStamp deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectCodec oc = parser.getCodec();
JsonNode node = oc.readTree(parser);
final DateTime dateTime = FORMATTER.parseDateTime(node.asText());
return new TimeStamp(0, dateTime.getMillis() / 1000);
}
@Override
public CurrencyPair deserialize(JsonParser jsonParser, final DeserializationContext ctxt)
throws IOException {
final ObjectCodec oc = jsonParser.getCodec();
final JsonNode node = oc.readTree(jsonParser);
final String currencyPairString = node.asText();
return getCurrencyPairFromString(currencyPairString);
}
@Override
public Quantity deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
Quantity quantity = null;
if (node.get("amount") != null && node.get("format") != null) {
quantity = new Quantity(node.get("amount").toString(), node.get("format").toString());
} else if (node.get("amount") != null) {
quantity = new Quantity(node.get("amount").toString());
} else {
quantity = new Quantity(node.asText());
}
return quantity;
}
@Override
public ArrayOrString deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
ArrayOrString arrayOrString;
if (node.isArray()) {
List<String> elements = new ArrayList<>();
node.elements().forEachRemaining(n -> elements.add(n.asText()));
arrayOrString = new ArrayOrString(elements);
} else {
arrayOrString = new ArrayOrString(node.asText());
}
return arrayOrString;
}
/**
* Deserializes a Replica Catalog representation YAML description of the type
*
* <pre>
* pegasus: "5.0"
* replicas:
* - lfn: f1
* pfns:
* - site: local
* pfn: /path/to/file
* - site: condorpool
* pfn: /path/to/file
* checksum:
* sha256: abc123
* metadata:
* owner: vahi
* size: 1024
* - lfn: f2
* pfns:
* - site: local
* pfn: /path/to/file
* - site: condorpool
* pfn: /path/to/file
* checksum:
* sha256: 991232132abc
* metadata:
* owner: pegasus
* size: 1024
* - lfn: .*\.gz
* pfns:
* - site: local
* pfn: input/mono/[0]
* # cant have checksum
* metadata:
* owner: pegasus
* regex: true
* </pre>
*
* @param parser
* @param dc
* @return
* @throws IOException
* @throws JsonProcessingException
*/
@Override
public ReplicaCatalog deserialize(JsonParser parser, DeserializationContext dc)
throws IOException, JsonProcessingException {
ObjectCodec oc = parser.getCodec();
JsonNode node = oc.readTree(parser);
YAML yamlRC = (YAML) dc.findInjectableValue("callback", null, null);
if (yamlRC == null) {
throw new RuntimeException("Callback not initialized when parsing inititated");
}
for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext(); ) {
Map.Entry<String, JsonNode> e = it.next();
String key = e.getKey();
ReplicaCatalogKeywords reservedKey = ReplicaCatalogKeywords.getReservedKey(key);
if (reservedKey == null) {
this.complainForIllegalKey(
ReplicaCatalogKeywords.REPLICAS.getReservedName(), key, node);
}
String keyValue = node.get(key).asText();
switch (reservedKey) {
case PEGASUS:
yamlRC.setVersion(keyValue);
break;
case REPLICAS:
JsonNode replicaNodes = node.get(key);
if (replicaNodes != null) {
if (replicaNodes.isArray()) {
for (JsonNode replicaNode : replicaNodes) {
parser = replicaNode.traverse(oc);
ReplicaLocation rl = parser.readValueAs(ReplicaLocation.class);
yamlRC.insert(rl);
}
}
}
break;
default:
this.complainForUnsupportedKey(
ReplicaCatalogKeywords.REPLICAS.getReservedName(), key, node);
}
}
return yamlRC;
}
@SuppressWarnings("serial")
@Override
public QuotaRestriction deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
ObjectCodec oc = jp.getCodec();
JsonNode node = oc.readTree(jp);
String type = node.get("type").asText();
JsonNode quotaConfig = node.get("config");
ErrorState errorState = ErrorState.getInstance();
if(type.equals("throttlemb")) {
if(!quotaConfig.has("period") || !quotaConfig.has("per") || !quotaConfig.has("mb")) {
errorState.setError("Invalid quota type: '"+type+"'. For type 'throttlemb' the following configs are required: period, per, mb", ErrorCode.INVALID_QUOTA_CONFIG, false);
throw new JsonProcessingException("Invalid quota config. For type 'throttlemb' the following configs are required: period, per, mb"){};
}
} else if(type.equals("throttle")) {
if(!quotaConfig.has("period") || !quotaConfig.has("per") || !quotaConfig.has("messages")) {
errorState.setError("Invalid quota type: '"+type+"'. For type 'throttle' the following configs are required: period, per, messages", ErrorCode.INVALID_QUOTA_CONFIG, false);
throw new JsonProcessingException("Invalid quota config. For type 'throttle' the following configs are required: period, per, messages"){};
}
} else {
errorState.setError("Unsupported Quota-Type: '" + type + "'. Must be either: throttle or throttlemb", ErrorCode.INVALID_QUOTA_CONFIG, false);
throw new JsonProcessingException("Unsupported Quota-Type: '" + type + "'"){};
}
String period = quotaConfig.get("period").asText();
String per = quotaConfig.get("per").asText();
Pattern pattern = Pattern.compile("^("+validPeriods+")$");
Matcher matcher = pattern.matcher(period);
if(!matcher.matches()) {
errorState.setError("Invalid quota period: '"+period+"'. Must be one of the following: "+validPeriods, ErrorCode.INVALID_QUOTA_CONFIG, false);
throw new JsonProcessingException("Quota period must be one of the following: "+validPeriods){};
}
QuotaRestriction restriction = new QuotaRestriction();
restriction.setType(QuotaRestrictiontype.valueOf(type));
restriction.setMethod(node.get("method").asText());
Map<String, String> configMap = new LinkedHashMap<String, String>();
configMap.put("period", period);
configMap.put("per", per);
if(node.has("api")) {
restriction.setApi(node.get("api").asText());
}
if(type.equals("throttle")) {
configMap.put("messages", quotaConfig.get("messages").asText());
} else {
configMap.put("mb", quotaConfig.get("mb").asText());
}
restriction.setConfig(configMap);
return restriction;
}