INT-4543: JacksonJsonObjectMapper: fix toJsonNode (#2592)
* INT-4543: JacksonJsonObjectMapper: fix toJsonNode JIRA: https://jira.spring.io/browse/INT-4543 When inbound payload is type of `String`, `byte[]`, `File`, `URL`, `InputStream` or `Reader`, we have to use an `ObjectMapper.readTree()` function. For all other types the `valueToTree()` should be used as a fallback **Cherry-pick to 5.0.x** * * Fallback to the `valueToTree()` if not valid JSON * * Fallback to `valueToTree()` only for `String` and `byte[]`
This commit is contained in:
committed by
Gary Russell
parent
11286907e8
commit
ddcfa25fc4
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,6 +30,7 @@ import org.springframework.integration.mapping.support.JsonHeaders;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
@@ -91,8 +92,35 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode toJsonNode(Object value) throws Exception {
|
||||
return this.objectMapper.valueToTree(value);
|
||||
public JsonNode toJsonNode(Object json) throws Exception {
|
||||
try {
|
||||
if (json instanceof String) {
|
||||
return this.objectMapper.readTree((String) json);
|
||||
}
|
||||
else if (json instanceof byte[]) {
|
||||
return this.objectMapper.readTree((byte[]) json);
|
||||
}
|
||||
else if (json instanceof File) {
|
||||
return this.objectMapper.readTree((File) json);
|
||||
}
|
||||
else if (json instanceof URL) {
|
||||
return this.objectMapper.readTree((URL) json);
|
||||
}
|
||||
else if (json instanceof InputStream) {
|
||||
return this.objectMapper.readTree((InputStream) json);
|
||||
}
|
||||
else if (json instanceof Reader) {
|
||||
return this.objectMapper.readTree((Reader) json);
|
||||
}
|
||||
}
|
||||
catch (JsonParseException e) {
|
||||
if (!(json instanceof String) && !(json instanceof byte[])) {
|
||||
throw e;
|
||||
}
|
||||
// Otherwise the input might not be valid JSON, fallback to TextNode with ObjectMapper.valueToTree()
|
||||
}
|
||||
|
||||
return this.objectMapper.valueToTree(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -137,7 +165,8 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
|
||||
JavaType contentClassType = this.createJavaType(javaTypes, JsonHeaders.CONTENT_TYPE_ID);
|
||||
if (classType.getKeyType() == null) {
|
||||
return this.objectMapper.getTypeFactory()
|
||||
.constructCollectionType((Class<? extends Collection<?>>) classType.getRawClass(), contentClassType);
|
||||
.constructCollectionType((Class<? extends Collection<?>>) classType.getRawClass(),
|
||||
contentClassType);
|
||||
}
|
||||
|
||||
JavaType keyClassType = this.createJavaType(javaTypes, JsonHeaders.KEY_TYPE_ID);
|
||||
|
||||
Reference in New Issue
Block a user