davsclaus commented on code in PR #25684: URL: https://github.com/apache/camel/pull/25684#discussion_r3855621412
########## components/camel-toon/src/main/java/org/apache/camel/dataformat/toon/ToonDataFormat.java: ########## @@ -0,0 +1,187 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dataformat.toon; + +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.util.Locale; + +import dev.toonformat.jtoon.DecodeOptions; +import dev.toonformat.jtoon.Delimiter; +import dev.toonformat.jtoon.EncodeOptions; +import dev.toonformat.jtoon.JToon; +import dev.toonformat.jtoon.KeyFolding; +import dev.toonformat.jtoon.PathExpansion; +import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; +import org.apache.camel.Exchange; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatContentTypeHeader; +import org.apache.camel.spi.DataFormatName; +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.annotations.Dataformat; +import org.apache.camel.support.ExchangeHelper; +import org.apache.camel.support.service.ServiceSupport; +import org.apache.camel.util.IOHelper; + +/** + * Marshal JSON-compatible Java values to TOON (Token-Oriented Object Notation) and unmarshal TOON back to Java objects + * using the <a href="https://github.com/toon-format/toon-java">JToon</a> library. + */ +@Dataformat("toon") +@Metadata(firstVersion = "4.23.0", title = "TOON") +public class ToonDataFormat extends ServiceSupport + implements DataFormat, DataFormatName, DataFormatContentTypeHeader, CamelContextAware { + + static final String CONTENT_TYPE = "text/toon"; + + private CamelContext camelContext; + @Metadata(description = "Number of spaces per indentation level.", defaultValue = "2", javaType = "java.lang.Integer") + private int indent = EncodeOptions.DEFAULT.indent(); + @Metadata(description = "Delimiter used for tabular array rows and inline primitive arrays.", + defaultValue = "COMMA", enums = "COMMA,TAB,PIPE") + private String delimiter = Delimiter.COMMA.name(); + @Metadata(description = "Whether to prefix array lengths with a hash marker so arrays render as hash-prefixed lengths instead of plain lengths.", + defaultValue = "false", javaType = "java.lang.Boolean") + private boolean lengthMarker = EncodeOptions.DEFAULT.lengthMarker(); + @Metadata(description = "Whether to enable strict validation when unmarshalling TOON. When false, JToon uses best-effort parsing.", + defaultValue = "true", javaType = "java.lang.Boolean") + private boolean strict = DecodeOptions.DEFAULT.strict(); + @Metadata(description = "Whether the data format should set the Content-Type header to text/toon when marshalling.", + defaultValue = "true", javaType = "java.lang.Boolean") + private boolean contentTypeHeader = true; + + @Override + public CamelContext getCamelContext() { + return camelContext; + } + + @Override + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + @Override + public String getDataFormatName() { + return "toon"; + } + + @Override + public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception { + EncodeOptions options = encodeOptions(); + String toon; + if (graph instanceof String json) { Review Comment: Since every `String` body is routed through `JToon.encodeJson()`, `.marshal().toon()` on a plain non-JSON string body (e.g. `"hello world"`) always throws `IllegalArgumentException: Invalid JSON` rather than encoding it as a TOON scalar. This is documented in the adoc and covered by tests, so it looks intentional, but it's a bit surprising for a Camel data format where `String` bodies are common and not always JSON. Was gating this behind a separate option (rather than making it the default for all strings) considered? ########## components/camel-toon/pom.xml: ########## @@ -0,0 +1,64 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>components</artifactId> + <version>4.23.0-SNAPSHOT</version> + </parent> + + <artifactId>camel-toon</artifactId> + <packaging>jar</packaging> + <name>Camel :: TOON</name> + <description>Camel TOON (Token-Oriented Object Notation) data format</description> + + <dependencies> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-support</artifactId> + </dependency> + <dependency> + <groupId>dev.toonformat</groupId> + <artifactId>jtoon</artifactId> Review Comment: This transitively pulls in Jackson v3 `3.2.1` (and `jackson-annotations` `2.22`), which doesn't match Camel's already-managed `jackson3-version` (`3.2.2`) used by `camel-jackson3`/`camel-jackson3xml`/etc. See the top-level comment for the dependency-tree output and suggested fix. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
