Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,11 @@
"message": {
"type": "string",
"description": "The event's string representation"
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Structured event metadata as JSON"
}
}
},
Expand All @@ -1881,6 +1886,11 @@
"message": {
"type": "string",
"description": "The event's string representation"
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Structured event metadata as JSON"
}
}
},
Expand All @@ -1906,6 +1916,11 @@
"message": {
"type": "string",
"description": "The event's string representation"
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Structured event metadata as JSON"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
"message": {
"type": "string",
"description": "The event's string representation"
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Structured event metadata as JSON"
}
}
},
Expand All @@ -60,6 +65,11 @@
"message": {
"type": "string",
"description": "The event's string representation"
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Structured event metadata as JSON"
}
}
},
Expand All @@ -85,6 +95,11 @@
"message": {
"type": "string",
"description": "The event's string representation"
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Structured event metadata as JSON"
}
}
},
Expand Down
65 changes: 65 additions & 0 deletions core/camel-api/src/main/java/org/apache/camel/spi/CamelEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
*/
package org.apache.camel.spi;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.Route;
import org.apache.camel.util.StringQuoteHelper;
import org.jspecify.annotations.Nullable;

/**
Expand Down Expand Up @@ -94,6 +98,67 @@ enum Type {

void setTimestamp(long timestamp);

/**
* Dumps the full event as a pretty-printed JSON string.
*
* The default implementation serializes flat scalar values ({@link Number}, {@link Boolean}, and {@link String})
* from {@link #asJSon()}. If a custom implementer overrides {@link #asJSon()} to include nested {@link Map} or
* {@link List} objects, they should also override this method to produce valid JSON.
*
* @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.jsonQuote(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.jsonQuote(String.valueOf(value)));
}
}
if (indent > 0) {
sb.append('\n');
}
sb.append('}');
return sb.toString();
}

/**
* The full event as a {@link Map} suitable for JSON serialization, containing structured metadata for this event.
*
* The default implementation returns a minimal map with {@code type}, optional {@code timestamp}, and
* {@code message}. Camel's built-in event classes override this to provide richer structured metadata.
*
* @since 4.23
*/
default Map<String, Object> asJSon() {
Map<String, Object> map = new LinkedHashMap<>();
map.put("type", getType().name());
if (getTimestamp() > 0) {
map.put("timestamp", getTimestamp());
}
map.put("message", toString());
return map;
}

/**
* This interface is implemented by all events that contain an exception and is used to retrieve the exception in a
* universal way.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.Serial;
import java.util.EventObject;
import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.spi.CamelEvent.CamelContextEvent;
Expand Down Expand Up @@ -49,4 +50,14 @@ public long getTimestamp() {
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}

@Override
public Map<String, Object> asJSon() {
return CamelEventJsonSupport.asJSon(this);
}

@Override
public String toJSon(int indent) {
return CamelEventJsonSupport.toJSon(this, indent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.Serial;
import java.util.EventObject;
import java.util.Map;

import org.apache.camel.Exchange;
import org.apache.camel.spi.CamelEvent.ExchangeEvent;
Expand Down Expand Up @@ -50,4 +51,14 @@ public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}

@Override
public Map<String, Object> asJSon() {
return CamelEventJsonSupport.asJSon(this);
}

@Override
public String toJSon(int indent) {
return CamelEventJsonSupport.toJSon(this, indent);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.Serial;
import java.util.EventObject;
import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.Route;
Expand Down Expand Up @@ -51,4 +52,14 @@ public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}

@Override
public Map<String, Object> asJSon() {
return CamelEventJsonSupport.asJSon(this);
}

@Override
public String toJSon(int indent) {
return CamelEventJsonSupport.toJSon(this, indent);
}

}
Loading