Claus Ibsen created CAMEL-27150:
-----------------------------------
Summary: camel-groovy - JsonSlurper.parseText reads a message body
of bytes or a stream
Key: CAMEL-27150
URL: https://issues.apache.org/jira/browse/CAMEL-27150
Project: Camel
Issue Type: Improvement
Components: camel-groovy
Reporter: Claus Ibsen
Assignee: Claus Ibsen
Fix For: 4.24.0
A Groovy script often parses a JSON message body like this:
{code}
new groovy.json.JsonSlurper().parseText(body)
{code}
JsonSlurper.parseText takes only a String. Many message bodies are bytes or a
stream:
* JMS sends a file, bytes or a stream as a BytesMessage, so the consumer gets a
byte[].
* A Kafka record with the ByteArrayDeserializer is a byte[].
* An HTTP request body can be a stream or a byte[].
On such a body the script fails with "No signature of method: parseText for
class: groovy.json.JsonSlurper is applicable for argument types: ([B)".
JsonSlurper.parse reads bytes and streams, but it has no form for a String. So
there is no single JsonSlurper call that takes both kinds of body.
In the local-model benchmark (connect-service-jms-request-reply step 3, r32 to
r35), the stock service gets the order as a byte[] from JMS. In 11 of 21 runs
the model wrote parseText(body), and the route failed. Its checks of the
expression passed, because they used a text body.
Proposal: camel-groovy adds two Groovy extension methods to JsonSlurper:
parseText(byte[]) and parseText(InputStream). They read the JSON as
parse(byte[]) and parse(InputStream) do. They are registered as a Groovy
extension module (META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule),
the standard way to add methods to a class in Groovy. Then the common
parseText(body) works on text, bytes and streams.
The methods only add calls that fail today, so no script that works now changes.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)