davsclaus commented on code in PR #27342:
URL: https://github.com/apache/camel/pull/27342#discussion_r4181088128


##########
components/camel-cloudevents/src/main/java/org/apache/camel/component/cloudevents/transformer/CloudEventJsonDataTypeTransformer.java:
##########
@@ -106,11 +105,52 @@ private String createCouldEventJsonObject(Map<String, 
Object> cloudEventAttribut
         return builder.append("}").toString();
     }
 
-    private boolean isJson(String data) {
-        if (data == null || data.isEmpty()) {
+    /**
+     * Appends the value as a Json string, escaping the quote, the backslash 
and the control characters (RFC 8259,
+     * section 7).
+     */
+    private static void appendJsonString(StringBuilder builder, String value) {
+        builder.append('"');
+        for (int i = 0; i < value.length(); i++) {
+            char ch = value.charAt(i);
+            switch (ch) {
+                case '"' -> builder.append("\\\"");
+                case '\\' -> builder.append("\\\\");
+                case '\n' -> builder.append("\\n");
+                case '\r' -> builder.append("\\r");
+                case '\t' -> builder.append("\\t");
+                case '\b' -> builder.append("\\b");
+                case '\f' -> builder.append("\\f");
+                default -> {
+                    if (ch < 0x20) {
+                        builder.append(String.format("\\u%04x", (int) ch));
+                    } else {
+                        builder.append(ch);
+                    }
+                }
+            }
+        }
+        builder.append('"');
+    }
+
+    /**
+     * Whether the data is a Json object or array, which is then set as nested 
Json value. Text that only starts like
+     * Json (such as a log line "[INFO] ...") is set as a Json string.
+     */
+    private static boolean isJson(String data) {
+        if (data == null || data.isBlank()) {
             return false;
         }
 
-        return data.trim().startsWith("{") || data.trim().startsWith("[");
+        String trimmed = data.trim();
+        if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) {
+            return false;
+        }
+        try {
+            Jsoner.deserialize(trimmed);

Review Comment:
   `isJson` now fully parses every body that starts with `{` or `[` into a 
`JsonObject`/`JsonArray`, only to decide whether to nest it, and then discards 
the result. For large JSON payloads that roughly doubles CPU and allocation per 
event, where this was a free check before. Could you take a cheaper path? For 
example, trust `datacontenttype` when it is `application/json` or `*+json` and 
only validate otherwise, or use a validating scan that does not build the 
object tree. At least, please state the trade-off in the PR.



-- 
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]

Reply via email to