Repository: apache/streampipes
Pull request #2763 (2024-04-23 11:35:05)

refactor: replace gson by jackson
<!--
  ~ 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.
  ~
  -->
  
  <!--
Thanks for contributing! Here are some tips you can follow to help us incorporate your contribution quickly and easily:
1. If this is your first time, please read our contributor guidelines:
    - https://streampipes.apache.org/getinvolved.html
    - https://cwiki.apache.org/confluence/display/STREAMPIPES/Getting+Started
2. Make sure the PR title is formatted like: `[#<GitHub issue id>] PR title ...`
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., `[WIP][#<GitHub issue id>] PR title ...`.
4. Please write your PR title to summarize what this PR proposes/fixes.
5. Link the PR to the corresponding GitHub issue (if present) in the `Development` section in the right menu bar. 
6. Be sure to keep the PR description updated to reflect all changes.
7. If possible, provide a concise example to reproduce the issue for a faster review.
8. Make sure tests pass via `mvn clean install`.
9. (Optional) If the contribution is large, please file an Apache ICLA
    - http://apache.org/licenses/icla.pdf
-->

### Purpose
<!--
Please clarify what changes you are proposing and describe how those changes will address the issue.
Furthermore, describe potential consequences the changes might have.
-->

### Remarks
<!--
Is there anything left we need to pay attention on?
Are there some references that might be important? E.g. links to Confluence, or discussions
on the mailing list or GitHub.
-->
PR introduces (a) breaking change(s): no

PR introduces (a) deprecation(s): no



Patch:
diff --git a/pom.xml b/pom.xml
index 9e798b52bc..7a38fc7449 100644
--- a/pom.xml
+++ b/pom.xml
@@ -49,7 +49,7 @@
         <commons-codec.version>1.16.0</commons-codec.version>
         <commons-collections.version>3.2.2</commons-collections.version>
         <commons-compress.version>1.26.0</commons-compress.version>
-        <commons-io.version>2.15.1</commons-io.version>
+        <commons-io.version>2.16.1</commons-io.version>
         <commons-lang3.version>3.14.0</commons-lang3.version>
         <commons-pool2.version>2.12.0</commons-pool2.version>
         <commons-text.version>1.11.0</commons-text.version>
diff --git a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/ConfiguredJsonOutputWriter.java b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/ConfiguredJsonOutputWriter.java
index e1311c9e47..e14e866e05 100644
--- a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/ConfiguredJsonOutputWriter.java
+++ b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/ConfiguredJsonOutputWriter.java
@@ -22,7 +22,7 @@ import org.apache.streampipes.dataexplorer.param.ProvidedRestQueryParams;
 import org.apache.streampipes.dataexplorer.query.writer.item.ItemGenerator;
 import org.apache.streampipes.dataexplorer.query.writer.item.JsonItemGenerator;
 
-import com.google.gson.Gson;
+import com.fasterxml.jackson.databind.ObjectMapper;
 
 import java.io.IOException;
 import java.io.OutputStream;
@@ -36,8 +36,7 @@ public class ConfiguredJsonOutputWriter extends ConfiguredOutputWriter {
   private final ItemGenerator itemGenerator;
 
   public ConfiguredJsonOutputWriter() {
-    Gson gson = new Gson();
-    this.itemGenerator = new JsonItemGenerator(gson);
+    this.itemGenerator = new JsonItemGenerator(new ObjectMapper());
   }
 
   @Override
diff --git a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/item/JsonItemGenerator.java b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/item/JsonItemGenerator.java
index e135bb8cdf..03d37197f0 100644
--- a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/item/JsonItemGenerator.java
+++ b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/item/JsonItemGenerator.java
@@ -19,28 +19,35 @@
 
 package org.apache.streampipes.dataexplorer.query.writer.item;
 
-import com.google.gson.Gson;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class JsonItemGenerator extends ItemGenerator {
 
+  private static final Logger LOG = LoggerFactory.getLogger(JsonItemGenerator.class);
+
   private static final String BEGIN_OBJECT = "{";
   private static final String END_OBJECT = "}";
 
-  private final Gson gson;
+  private final ObjectMapper objectMapper;
 
-  public JsonItemGenerator(Gson gson) {
+  public JsonItemGenerator(ObjectMapper objectMapper) {
     super(COMMA_SEPARATOR);
-    this.gson = gson;
+    this.objectMapper = objectMapper;
   }
 
   @Override
   protected String makeItemString(String key,
                                   Object value) {
-    var stringValue = value != null ? gson.toJson(value) : null;
-    return "\""
-        + key
-        + "\": "
-        + stringValue;
+    String valueJsonString = null;
+    try {
+      valueJsonString = value != null ? this.objectMapper.writeValueAsString(value) : null;
+    } catch (JsonProcessingException e) {
+      LOG.error("Error while converting value to JSON string: {}", e.getMessage());
+    }
+    return "\"%s\": %s".formatted(key, valueJsonString);
   }
 
   @Override


Test patch:
diff --git a/streampipes-data-explorer/src/test/java/org/apache/streampipes/dataexplorer/query/writer/item/TestJsonItemGenerator.java b/streampipes-data-explorer/src/test/java/org/apache/streampipes/dataexplorer/query/writer/item/TestJsonItemGenerator.java
index 2b3dcb58b1..786570bc83 100644
--- a/streampipes-data-explorer/src/test/java/org/apache/streampipes/dataexplorer/query/writer/item/TestJsonItemGenerator.java
+++ b/streampipes-data-explorer/src/test/java/org/apache/streampipes/dataexplorer/query/writer/item/TestJsonItemGenerator.java
@@ -18,7 +18,7 @@
 
 package org.apache.streampipes.dataexplorer.query.writer.item;
 
-import com.google.gson.Gson;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -29,7 +29,7 @@ public class TestJsonItemGenerator extends TestItemGenerator {
 
   @Test
   public void testJsonWriter() {
-    var writer = new JsonItemGenerator(new Gson());
+    var writer = new JsonItemGenerator(new ObjectMapper());
 
     String result = writer.createItem(row, columns);