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 ff108e6be382738792165b981eadc4986e21aa51 Author: Claus Ibsen <[email protected]> AuthorDate: Sun Dec 24 12:34:04 2023 +0100 CAMEL-14028: Allow DataFormats to unmarshal known data formats without first converting to bytes --- .../camel/component/gson/GsonDataFormat.java | 49 +++++++++++++++++----- .../camel/component/gson/GsonDataFormatTest.java | 7 +--- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/components/camel-gson/src/main/java/org/apache/camel/component/gson/GsonDataFormat.java b/components/camel-gson/src/main/java/org/apache/camel/component/gson/GsonDataFormat.java index e35b04b3f99..294fd30e1b1 100644 --- a/components/camel-gson/src/main/java/org/apache/camel/component/gson/GsonDataFormat.java +++ b/components/camel-gson/src/main/java/org/apache/camel/component/gson/GsonDataFormat.java @@ -16,12 +16,12 @@ */ package org.apache.camel.component.gson; -import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.io.Reader; import java.lang.reflect.Type; import java.util.Arrays; import java.util.List; @@ -32,6 +32,7 @@ import com.google.gson.FieldNamingStrategy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.LongSerializationPolicy; +import com.google.gson.stream.JsonReader; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; import org.apache.camel.Exchange; @@ -152,17 +153,43 @@ public class GsonDataFormat extends ServiceSupport @Override public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception { - try (final InputStreamReader isr = new InputStreamReader(stream, ExchangeHelper.getCharsetName(exchange)); - final BufferedReader reader = IOHelper.buffered(isr)) { - - String type = exchange.getIn().getHeader(GsonConstants.UNMARSHAL_TYPE, String.class); - if (type != null) { - Class<?> clazz = exchange.getContext().getClassResolver().resolveMandatoryClass(type); - return gson.fromJson(reader, clazz); - } else if (unmarshalGenericType == null) { - return gson.fromJson(reader, unmarshalType); + return unmarshal(exchange, (Object) stream); + } + + @Override + public Object unmarshal(Exchange exchange, Object body) throws Exception { + Class<?> clazz = unmarshalType; + String type = exchange.getIn().getHeader(GsonConstants.UNMARSHAL_TYPE, String.class); + if (type != null) { + clazz = exchange.getContext().getClassResolver().resolveMandatoryClass(type); + } + + if (body instanceof String str) { + if (unmarshalGenericType == null) { + return gson.fromJson(str, clazz); + } else { + return gson.fromJson(str, unmarshalGenericType); + } + } else if (body instanceof Reader r) { + if (unmarshalGenericType == null) { + return gson.fromJson(r, clazz); + } else { + return gson.fromJson(r, unmarshalGenericType); + } + } else if (body instanceof JsonReader r) { + if (unmarshalGenericType == null) { + return gson.fromJson(r, clazz); + } else { + return gson.fromJson(r, unmarshalGenericType); + } + } else { + // fallback to input stream + InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, body); + Reader r = new InputStreamReader(is); + if (unmarshalGenericType == null) { + return gson.fromJson(r, clazz); } else { - return gson.fromJson(reader, unmarshalGenericType); + return gson.fromJson(r, unmarshalGenericType); } } } diff --git a/components/camel-gson/src/test/java/org/apache/camel/component/gson/GsonDataFormatTest.java b/components/camel-gson/src/test/java/org/apache/camel/component/gson/GsonDataFormatTest.java index dfc17e89e0b..8aedcd8608a 100644 --- a/components/camel-gson/src/test/java/org/apache/camel/component/gson/GsonDataFormatTest.java +++ b/components/camel-gson/src/test/java/org/apache/camel/component/gson/GsonDataFormatTest.java @@ -16,8 +16,6 @@ */ package org.apache.camel.component.gson; -import java.io.*; -import java.nio.charset.StandardCharsets; import java.util.*; import org.apache.camel.Exchange; @@ -41,7 +39,6 @@ public class GsonDataFormatTest { @BeforeEach public void setup() { - when(message.getHeader(Exchange.CHARSET_NAME, String.class)).thenReturn(StandardCharsets.UTF_8.name()); when(exchange.getIn()).thenReturn(message); } @@ -64,9 +61,7 @@ public class GsonDataFormatTest { Object unmarshalled; try (GsonDataFormat gsonDataFormat = new GsonDataFormat()) { gsonDataFormat.doStart(); - try (InputStream in = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))) { - unmarshalled = gsonDataFormat.unmarshal(exchange, in); - } + unmarshalled = gsonDataFormat.unmarshal(exchange, json); assertEquals(expected, unmarshalled); } }
