public static JsonObject dumpAsJSonObject()

in core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java [946:1114]


    public static JsonObject dumpAsJSonObject(
            Message message, boolean includeExchangeProperties, boolean includeExchangeVariables, boolean includeBody,
            boolean allowCachedStreams, boolean allowStreams, boolean allowFiles, int maxChars) {

        JsonObject root = new JsonObject();
        JsonObject jo = new JsonObject();
        root.put("message", jo);
        jo.put("exchangeId", message.getExchange().getExchangeId());
        jo.put("exchangePattern", message.getExchange().getPattern().name());
        jo.put("exchangeType", ObjectHelper.classCanonicalName(message.getExchange()));
        jo.put("messageType", ObjectHelper.classCanonicalName(message));

        // exchange variables
        if (includeExchangeVariables && message.getExchange().hasVariables()) {
            JsonArray arr = new JsonArray();
            // sort the exchange variables so they are listed A..Z
            Map<String, Object> properties = new TreeMap<>(message.getExchange().getVariables());
            for (Map.Entry<String, Object> entry : properties.entrySet()) {
                Object value = entry.getValue();
                String type = ObjectHelper.classCanonicalName(value);
                JsonObject jh = new JsonObject();
                String key = entry.getKey();
                jh.put("key", key);
                if (type != null) {
                    jh.put("type", type);
                }
                if (value != null) {
                    Object s = Jsoner.trySerialize(value);
                    if (s == null) {
                        // cannot JSon serialize out of the box, so we need to use string value
                        try {
                            s = extractValueForLogging(value, message, allowCachedStreams, allowStreams, allowFiles, maxChars);
                        } catch (Exception e) {
                            // ignore
                        }
                    } else {
                        // use the value as-is because it can be serialized in json
                        s = value;
                    }
                    jh.put("value", s);
                }
                arr.add(jh);
            }
            if (!arr.isEmpty()) {
                jo.put("exchangeVariables", arr);
            }
        }
        // exchange properties
        if (includeExchangeProperties) {
            JsonArray arr = new JsonArray();
            // sort the exchange properties so they are listed A..Z
            Map<String, Object> properties = new TreeMap<>(message.getExchange().getAllProperties());
            for (Map.Entry<String, Object> entry : properties.entrySet()) {
                Object value = entry.getValue();
                String type = ObjectHelper.classCanonicalName(value);
                JsonObject jh = new JsonObject();
                String key = entry.getKey();
                // skip some special that are too big
                if (Exchange.MESSAGE_HISTORY.equals(key) || Exchange.GROUPED_EXCHANGE.equals(key)
                        || Exchange.FILE_EXCHANGE_FILE.equals(key)) {
                    continue;
                }
                jh.put("key", key);
                if (type != null) {
                    jh.put("type", type);
                }
                if (value != null) {
                    Object s = Jsoner.trySerialize(value);
                    if (s == null) {
                        // cannot JSon serialize out of the box, so we need to use string value
                        try {
                            s = extractValueForLogging(value, message, allowCachedStreams, allowStreams, allowFiles, maxChars);
                        } catch (Exception e) {
                            // ignore
                        }
                    } else {
                        // use the value as-is because it can be serialized in json
                        s = value;
                    }
                    jh.put("value", s);
                }
                arr.add(jh);
            }
            if (!arr.isEmpty()) {
                jo.put("exchangeProperties", arr);
            }
        }
        // headers
        if (message.hasHeaders()) {
            JsonArray arr = new JsonArray();
            // sort the headers so they are listed A..Z
            Map<String, Object> headers = new TreeMap<>(message.getHeaders());
            for (Map.Entry<String, Object> entry : headers.entrySet()) {
                Object value = entry.getValue();
                String type = ObjectHelper.classCanonicalName(value);
                JsonObject jh = new JsonObject();
                jh.put("key", entry.getKey());
                if (type != null) {
                    jh.put("type", type);
                }
                // dump header value as JSon, use Camel type converter to convert to String
                if (value != null) {
                    Object s = Jsoner.trySerialize(value);
                    if (s == null) {
                        // cannot JSon serialize out of the box, so we need to use string value
                        try {
                            s = extractValueForLogging(value, message, allowCachedStreams, allowStreams, allowFiles, maxChars);
                        } catch (Exception e) {
                            // ignore
                        }
                    } else {
                        // use the value as-is because it can be serialized in json
                        s = value;
                    }
                    jh.put("value", s);
                }
                arr.add(jh);
            }
            if (!arr.isEmpty()) {
                jo.put("headers", arr);
            }
        }
        if (includeBody) {
            JsonObject jb = new JsonObject();
            jo.put("body", jb);
            Object body = message.getBody();
            String type = ObjectHelper.classCanonicalName(body);
            if (type != null) {
                jb.put("type", type);
            }
            if (body instanceof Collection) {
                long size = ((Collection<?>) body).size();
                jb.put("size", size);
            }
            if (body != null && body.getClass().isArray()) {
                int size = Array.getLength(body);
                jb.put("size", size);
            }
            if (body instanceof WrappedFile<?> wf) {
                if (wf.getFile() instanceof File f) {
                    jb.put("size", f.length());
                }
            } else if (body instanceof File f) {
                jb.put("size", f.length());
            } else if (body instanceof Path p) {
                jb.put("size", p.toFile().length());
            }
            if (body instanceof StreamCache streamCache) {
                long pos = streamCache.position();
                if (pos != -1) {
                    jb.put("position", pos);
                }
                long size = streamCache.length();
                if (size > 0) {
                    jb.put("size", size);
                }
            }
            String data = extractBodyForLogging(message, null, allowCachedStreams, allowStreams, allowFiles, maxChars);
            if (data != null) {
                if ("[Body is null]".equals(data)) {
                    jb.put("value", null);
                } else {
                    jb.put("value", Jsoner.escape(data));
                }
            }
        }

        return root;
    }