streampipes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zehn...@apache.org
Subject [incubator-streampipes] branch dev updated: Update json parser for connect adapters
Date Fri, 13 Dec 2019 13:43:22 GMT
This is an automated email from the ASF dual-hosted git repository.

zehnder pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampipes.git


The following commit(s) were added to refs/heads/dev by this push:
     new ef8ae30  Update json parser for connect adapters
ef8ae30 is described below

commit ef8ae300fb1d47c770a8cb19d6856e7c1dd195ad
Author: Philipp Zehnder <zehnder@fzi.de>
AuthorDate: Fri Dec 13 14:42:54 2019 +0100

    Update json parser for connect adapters
---
 .../format/json/object/JsonObjectParser.java       | 50 ++++++----------------
 1 file changed, 12 insertions(+), 38 deletions(-)

diff --git a/streampipes-connect/src/main/java/org/apache/streampipes/connect/adapter/format/json/object/JsonObjectParser.java
b/streampipes-connect/src/main/java/org/apache/streampipes/connect/adapter/format/json/object/JsonObjectParser.java
index b653653..f698a77 100644
--- a/streampipes-connect/src/main/java/org/apache/streampipes/connect/adapter/format/json/object/JsonObjectParser.java
+++ b/streampipes-connect/src/main/java/org/apache/streampipes/connect/adapter/format/json/object/JsonObjectParser.java
@@ -19,6 +19,7 @@
 package org.apache.streampipes.connect.adapter.format.json.object;
 
 
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -47,6 +48,9 @@ public class JsonObjectParser extends Parser {
 
     private static final Logger logger = LoggerFactory.getLogger(JsonObjectParser.class);
 
+    private ObjectMapper mapper;
+    private JsonDataFormatDefinition jsonDefinition;
+
     @Override
     public Parser getInstance(FormatDescription formatDescription) {
         return new JsonObjectParser();
@@ -56,52 +60,22 @@ public class JsonObjectParser extends Parser {
      * Use this constructor when just a specific key of the object should be parsed
      */
     public JsonObjectParser() {
+        mapper = new ObjectMapper();
+        jsonDefinition = new JsonDataFormatDefinition();
     }
 
     @Override
     public void parse(InputStream data, EmitBinaryEvent emitBinaryEvent) throws ParseException
{
-        JsonParserFactory factory = Json.createParserFactory(null);
-        javax.json.stream.JsonParser jsonParser = factory.createParser(data);
-
-        // Parse all events
-        JsonDataFormatDefinition jsonDefinition = new JsonDataFormatDefinition();
-        boolean isEvent = true;
-        boolean result = true;
-        int objectCount = 0;
-
-        if (!jsonParser.hasNext())
-            throw new ParseException("No JSONObject found");
 
         try {
-            while (jsonParser.hasNext() && isEvent && result) {
-                Map<String, Object> objectMap = parseObject(jsonParser, true, 1);
-                if (objectMap != null) {
-                    byte[] tmp = new byte[0];
-                    try {
-                        tmp = jsonDefinition.fromMap(objectMap);
-                    } catch (SpRuntimeException e) {
-                        e.printStackTrace();
-                    }
-//                    handleEvent(new EventObjectEndEvent(parseObject(tmp)));
-                    // TODO decide what happens id emit returns false
-                    result = emitBinaryEvent.emit(tmp);
-                } else {
-                    isEvent = false;
-                }
-            }
-
-        } catch(JsonParsingException e) {
-            logger.error("Could not parse Data to JSONObject");
-            try {
-                String event = IOUtils.toString(data, StandardCharsets.UTF_8.name());
-                logger.error("Event no valid json: " + event);
-            } catch (IOException e1) {
-                e1.printStackTrace();
-            }
-            throw new ParseException("Could not parse Data to JSONObject.");
+            Map<String, Object> map = mapper.readValue(data, HashMap.class);
+            emitBinaryEvent.emit(jsonDefinition.fromMap(map));
+        } catch (IOException e) {
+            e.printStackTrace();
+        } catch (SpRuntimeException e) {
+            e.printStackTrace();
         }
 
-
     }
 
     @Override


Mime
View raw message