This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch unmarshal in repository https://gitbox.apache.org/repos/asf/camel.git
commit b6c0b78abfaf20da7afbc2b734a439f2223781f7 Author: Claus Ibsen <[email protected]> AuthorDate: Sun Dec 24 12:23:49 2023 +0100 CAMEL-14028: Allow DataFormats to unmarshal known data formats without first converting to bytes --- .../component/fastjson/FastjsonDataFormat.java | 29 +++++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/components/camel-fastjson/src/main/java/org/apache/camel/component/fastjson/FastjsonDataFormat.java b/components/camel-fastjson/src/main/java/org/apache/camel/component/fastjson/FastjsonDataFormat.java index eeb0053cab1..a21a9cb4c1b 100644 --- a/components/camel-fastjson/src/main/java/org/apache/camel/component/fastjson/FastjsonDataFormat.java +++ b/components/camel-fastjson/src/main/java/org/apache/camel/component/fastjson/FastjsonDataFormat.java @@ -109,11 +109,32 @@ public class FastjsonDataFormat extends ServiceSupport } @Override - public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception { - if (unmarshalGenericType == null) { - return JSON.parseObject(stream, config.getCharset(), unmarshalType, config.getFeatures()); + public Object unmarshal(Exchange exchange, InputStream stream) throws Exception { + return unmarshal(exchange, (Object) stream); + } + + @Override + public Object unmarshal(Exchange exchange, Object body) throws Exception { + if (body instanceof String str) { + if (unmarshalGenericType == null) { + return JSON.parseObject(str, unmarshalType, config.getFeatures()); + } else { + return JSON.parseObject(str, unmarshalGenericType, config.getFeatures()); + } + } else if (body instanceof byte[] arr) { + if (unmarshalGenericType == null) { + return JSON.parseObject(arr, unmarshalType, config.getFeatures()); + } else { + return JSON.parseObject(arr, unmarshalGenericType, config.getFeatures()); + } } else { - return JSON.parseObject(stream, config.getCharset(), unmarshalGenericType, config.getFeatures()); + // fallback to input stream + InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, body); + if (unmarshalGenericType == null) { + return JSON.parseObject(is, config.getCharset(), unmarshalType, config.getFeatures()); + } else { + return JSON.parseObject(is, config.getCharset(), unmarshalGenericType, config.getFeatures()); + } } }
