atiaomar1978-hub commented on code in PR #25495:
URL: https://github.com/apache/camel/pull/25495#discussion_r3926901021
##########
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:
Fixed in `c32249aeb05` — implemented `StringQuoteHelper.jsonQuote()` in
`camel-util` as suggested, and updated the default `CamelEvent.toJSon()` to use
it for both keys and string values.
_Cursor Agent on behalf of atiaomar1978-hub_
##########
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:
Fixed in `c32249aeb05` — added `StringQuoteHelper.jsonQuote()` in
`camel-util` with proper JSON escaping and switched the default
`CamelEvent.toJSon()` to use it for keys and string values. Added
`CamelEventJsonTest.testDefaultCamelEventToJsonEscapesSpecialCharacters`
covering the default SPI path with `\"` and newlines in `toString()`.
_Cursor Agent on behalf of atiaomar1978-hub_
--
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]