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


##########
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(String.valueOf(value))` does not JSON-escape 
— it's literally `'"' + text + '"'`. Any value containing `"`, `\`, or control 
chars produces invalid JSON.
   
   Minimal fix — add a proper JSON escape to `StringQuoteHelper` (or inline 
here):
   
   ```suggestion
                   sb.append(jsonQuote(String.valueOf(value)));
   ```
   
   With a helper like:
   ```java
   private static String jsonQuote(String text) {
       StringBuilder sb = new StringBuilder(text.length() + 2);
       sb.append('"');
       for (int i = 0; i < text.length(); i++) {
           char c = text.charAt(i);
           switch (c) {
               case '"': sb.append("\\\"")); break;
               case '\\': sb.append("\\\\"); break;
               case '\n': sb.append("\\n"); break;
               case '\r': sb.append("\\r"); break;
               case '\t': sb.append("\\t"); break;
               default:
                   if (c < 0x20) sb.append(String.format("\\u%04x", (int) c));
                   else sb.append(c);
           }
       }
       sb.append('"');
       return sb.toString();
   }
   ```
   
   The same escaping must also be applied to the key on line 118 
(`StringQuoteHelper.doubleQuote(entry.getKey())`).



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