This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 4672fe8298fa91a03d0c991a1b061be95403b405
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Mon Feb 13 18:32:53 2023 +0100

    CAMEL-19033: camel-jbang - get trace in color output of json so its easier 
to read
---
 .../org/apache/camel/support/MessageHelper.java    | 39 ++++++++++++--------
 .../camel/dsl/jbang/core/common/JSonHelper.java    |  7 +++-
 .../java/org/apache/camel/util/json/Jsoner.java    | 37 ++++++++++++++++++-
 .../org/apache/camel/util/json/JSonerTest.java     | 41 ++++++++++++++++++++++
 4 files changed, 107 insertions(+), 17 deletions(-)

diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java 
b/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java
index ee85d1603b4..d4a7a3db911 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java
@@ -881,17 +881,21 @@ public final class MessageHelper {
                 if (type != null) {
                     jh.put("type", type);
                 }
-                // dump property value as JSon, use Camel type converter to 
convert to String
                 if (value != null) {
-                    try {
-                        String data = 
message.getExchange().getContext().getTypeConverter().tryConvertTo(String.class,
-                                message.getExchange(), value);
-                        if (data != null) {
-                            jh.put("value", Jsoner.unescape(data));
+                    Object s = Jsoner.trySerialize(value);
+                    if (s == null) {
+                        // cannot JSon serialize out of the box, so we need to 
use string value
+                        try {
+                            s = 
message.getExchange().getContext().getTypeConverter().tryConvertTo(String.class,
+                                    message.getExchange(), value);
+                        } catch (Throwable e) {
+                            // ignore
                         }
-                    } catch (Throwable e) {
-                        // ignore as the body is for logging purpose
+                    } else {
+                        // use the value as-is because it can be serialized in 
json
+                        s = value;
                     }
+                    jh.put("value", s);
                 }
                 arr.add(jh);
             }
@@ -914,15 +918,20 @@ public final class MessageHelper {
                 }
                 // dump header value as JSon, use Camel type converter to 
convert to String
                 if (value != null) {
-                    try {
-                        String data = 
message.getExchange().getContext().getTypeConverter().tryConvertTo(String.class,
-                                message.getExchange(), value);
-                        if (data != null) {
-                            jh.put("value", Jsoner.unescape(data));
+                    Object s = Jsoner.trySerialize(value);
+                    if (s == null) {
+                        // cannot JSon serialize out of the box, so we need to 
use string value
+                        try {
+                            s = 
message.getExchange().getContext().getTypeConverter().tryConvertTo(String.class,
+                                    message.getExchange(), value);
+                        } catch (Throwable e) {
+                            // ignore
                         }
-                    } catch (Throwable e) {
-                        // ignore as the body is for logging purpose
+                    } else {
+                        // use the value as-is because it can be serialized in 
json
+                        s = value;
                     }
+                    jh.put("value", s);
                 }
                 arr.add(jh);
             }
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/JSonHelper.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/JSonHelper.java
index 7d1b06f2d35..b2c1bb22194 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/JSonHelper.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/JSonHelper.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.dsl.jbang.core.common;
 
+import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.json.Jsoner;
 import org.apache.camel.util.json.Yytoken;
 import org.fusesource.jansi.Ansi;
@@ -40,7 +41,11 @@ public final class JSonHelper {
                         s = 
Ansi.ansi().fg(Ansi.Color.WHITE).bold().a(s).reset().toString();
                     case VALUE -> {
                         if (Yytoken.Types.COLON == prev) {
-                            s = 
Ansi.ansi().fg(Ansi.Color.GREEN).a(s).reset().toString();
+                            if (StringHelper.isQuoted(s)) {
+                                s = 
Ansi.ansi().fg(Ansi.Color.GREEN).a(s).reset().toString();
+                            } else {
+                                s = 
Ansi.ansi().fg(Ansi.Color.WHITE).a(s).reset().toString();
+                            }
                         } else {
                             s = 
Ansi.ansi().fgBright(Ansi.Color.BLUE).a(s).reset().toString();
                         }
diff --git 
a/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/Jsoner.java 
b/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/Jsoner.java
index 573fe7eb484..1abb19d7af5 100644
--- 
a/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/Jsoner.java
+++ 
b/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/Jsoner.java
@@ -72,6 +72,12 @@ public final class Jsoner {
          * deserialize.
          */
         ALLOW_INVALIDS,
+        /**
+         * Instead of aborting serialization on non-JSON values it will do 
nothing and continue serialization by
+         * serializing the non-JSON value directly into the now invalid JSON. 
Be mindful that invalid JSON will not
+         * successfully deserialize.
+         */
+        ALLOW_INVALIDS_NOOP,
         /**
          * Instead of aborting serialization on non-JSON values that implement 
Jsonable it will continue serialization
          * by deferring serialization to the Jsonable.
@@ -862,6 +868,32 @@ public final class Jsoner {
         return writableDestination.toString();
     }
 
+    /**
+     * A convenience method that assumes a StringWriter.
+     *
+     * @param  jsonSerializable         represents the object that should be 
serialized as a string in JSON format.
+     * @return                          a string, in JSON format, that 
represents the object provided, or <tt>null</tt>
+     *                                  if not possible to serialize.
+     * @throws IllegalArgumentException if the jsonSerializable isn't 
serializable in JSON.
+     * @see                             Jsoner#serialize(Object, Writer)
+     * @see                             StringWriter
+     */
+    public static String trySerialize(final Object jsonSerializable) {
+        final StringWriter writableDestination = new StringWriter();
+        try {
+            Jsoner.serialize(jsonSerializable, writableDestination,
+                    EnumSet.of(SerializationOptions.ALLOW_JSONABLES, 
SerializationOptions.ALLOW_FULLY_QUALIFIED_ENUMERATIONS,
+                            SerializationOptions.ALLOW_INVALIDS_NOOP));
+        } catch (final IOException caught) {
+            /* See StringWriter. */
+        }
+        String answer = writableDestination.toString();
+        if ("SerializationOptions.ALLOW_INVALIDS_NOOP".equals(answer)) {
+            answer = null;
+        }
+        return answer;
+    }
+
     /**
      * Serializes values according to the RFC 4627 JSON specification. It will 
also trust the serialization provided by
      * any Jsonables it serializes and serializes Enums that don't implement 
Jsonable as a string of their fully
@@ -1116,7 +1148,10 @@ public final class Jsoner {
              * It cannot by any measure be safely serialized according to
              * specification.
              */
-            if (flags.contains(SerializationOptions.ALLOW_INVALIDS)) {
+            if (flags.contains(SerializationOptions.ALLOW_INVALIDS_NOOP)) {
+                // noop marker
+                
writableDestination.write("SerializationOptions.ALLOW_INVALIDS_NOOP");
+            } else if (flags.contains(SerializationOptions.ALLOW_INVALIDS)) {
                 /* Can be helpful for debugging how it isn't valid. */
                 writableDestination.write(jsonSerializable.toString());
             } else {
diff --git 
a/tooling/camel-util-json/src/test/java/org/apache/camel/util/json/JSonerTest.java
 
b/tooling/camel-util-json/src/test/java/org/apache/camel/util/json/JSonerTest.java
new file mode 100644
index 00000000000..fb7754cf092
--- /dev/null
+++ 
b/tooling/camel-util-json/src/test/java/org/apache/camel/util/json/JSonerTest.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.util.json;
+
+import java.util.Date;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class JSonerTest {
+
+    @Test
+    public void testTrySerialize() throws Exception {
+        String s = Jsoner.trySerialize("Hello World");
+        Assertions.assertEquals("\"Hello World\"", s);
+
+        s = Jsoner.trySerialize(123);
+        Assertions.assertEquals("123", s);
+
+        s = Jsoner.trySerialize(true);
+        Assertions.assertEquals("true", s);
+
+        s = Jsoner.trySerialize(new Date());
+        Assertions.assertNull(s);
+
+    }
+}

Reply via email to