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


##########
core/camel-api/src/main/java/org/apache/camel/spi/CamelEvent.java:
##########
@@ -94,6 +98,63 @@ enum Type {
 
     void setTimestamp(long timestamp);
 
+    /**
+     * Dumps the full event as a pretty-printed JSON string.
+     *
+     * @param  indent number of spaces to indent
+     * @return        JSON representation of this event
+     * @since         4.23
+     */
+    default String toJSon(int indent) {
+        Map<String, Object> map = asJSon();
+        String indentText = indent > 0 ? " ".repeat(indent) : "";
+        StringBuilder sb = new StringBuilder(128);
+        sb.append('{');
+        boolean first = true;
+        for (Map.Entry<String, Object> entry : map.entrySet()) {
+            if (!first) {
+                sb.append(',');
+            }
+            first = false;
+            if (indent > 0) {
+                sb.append('\n').append(indentText);
+            }
+            
sb.append(StringQuoteHelper.doubleQuote(entry.getKey())).append(':');
+            if (indent > 0) {
+                sb.append(' ');
+            }
+            Object value = entry.getValue();
+            if (value instanceof Number || value instanceof Boolean) {
+                sb.append(value);
+            } else {
+                
sb.append(StringQuoteHelper.doubleQuote(String.valueOf(value)));

Review Comment:
   `StringQuoteHelper.doubleQuote(...)` only wraps the value in `"` quotes — it 
does **not** JSON-escape `"`, `\`, or control characters. Because the default 
`asJSon()` sets `message` to `toString()` (which commonly contains endpoint 
URIs and exception text), this default `toJSon` produces invalid JSON whenever 
a value contains one of those characters.
   
   Example: a custom `CamelEvent` whose `toString()` is `He said "hi"` yields 
`{"message":"He said "hi""}` — broken JSON. The same applies to the key on line 
122.
   
   Built-in events are unaffected (they override and delegate to 
`CamelEventJsonSupport` → `Jsoner`, which escapes), so this is confined to the 
default SPI path used by external implementers. Since `camel-api` doesn't 
depend on `camel-util-json`, a small manual escape of `"`, `\`, and control 
chars here (for both the key and the string value) would fix it without adding 
a dependency.



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