metron-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ceste...@apache.org
Subject incubator-metron git commit: METRON-449 JSONMapParser should unfold maps to arbitrary depths closes apache/incubator-metron#271
Date Tue, 27 Sep 2016 13:23:01 GMT
Repository: incubator-metron
Updated Branches:
  refs/heads/master 3d5f279ca -> d0d6c2c35


METRON-449 JSONMapParser should unfold maps to arbitrary depths closes apache/incubator-metron#271


Project: http://git-wip-us.apache.org/repos/asf/incubator-metron/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-metron/commit/d0d6c2c3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-metron/tree/d0d6c2c3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-metron/diff/d0d6c2c3

Branch: refs/heads/master
Commit: d0d6c2c359f5e4afaaeabbd9e32331c688405c2f
Parents: 3d5f279
Author: Otto Fowler <ofowler@industrialdefender.com>
Authored: Tue Sep 27 09:22:41 2016 -0400
Committer: cstella <cestella@gmail.com>
Committed: Tue Sep 27 09:22:41 2016 -0400

----------------------------------------------------------------------
 .../metron/parsers/json/JSONMapParser.java      | 21 ++++++++------
 .../metron/parsers/json/JSONMapParserTest.java  | 30 ++++++++++++++++++--
 2 files changed, 41 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/d0d6c2c3/metron-platform/metron-parsers/src/main/java/org/apache/metron/parsers/json/JSONMapParser.java
----------------------------------------------------------------------
diff --git a/metron-platform/metron-parsers/src/main/java/org/apache/metron/parsers/json/JSONMapParser.java
b/metron-platform/metron-parsers/src/main/java/org/apache/metron/parsers/json/JSONMapParser.java
index 0bcf0f5..5d82456 100644
--- a/metron-platform/metron-parsers/src/main/java/org/apache/metron/parsers/json/JSONMapParser.java
+++ b/metron-platform/metron-parsers/src/main/java/org/apache/metron/parsers/json/JSONMapParser.java
@@ -24,8 +24,6 @@ import org.apache.metron.common.utils.JSONUtils;
 import org.apache.metron.parsers.BasicParser;
 import org.json.simple.JSONObject;
 
-import java.io.IOException;
-import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -37,12 +35,7 @@ public class JSONMapParser extends BasicParser {
   public static enum MapStrategy implements Handler {
      DROP((key, value, obj) -> obj)
     ,UNFOLD( (key, value, obj) -> {
-      Set<Map.Entry<Object, Object>> entrySet = value.entrySet();
-      for(Map.Entry<Object, Object> kv : entrySet) {
-        String newKey = Joiner.on(".").join(key, kv.getKey().toString());
-        obj.put(newKey, kv.getValue());
-      }
-      return obj;
+      return recursiveUnfold(key,value,obj);
     })
     ,ALLOW((key, value, obj) -> {
       obj.put(key, value);
@@ -57,6 +50,18 @@ public class JSONMapParser extends BasicParser {
       this.handler = handler;
     }
 
+    private static JSONObject recursiveUnfold(String key, Map value, JSONObject obj){
+      Set<Map.Entry<Object, Object>> entrySet = value.entrySet();
+      for(Map.Entry<Object, Object> kv : entrySet) {
+        String newKey = Joiner.on(".").join(key, kv.getKey().toString());
+        if(kv.getValue() instanceof Map){
+          recursiveUnfold(newKey,(Map)kv.getValue(),obj);
+        }else {
+          obj.put(newKey, kv.getValue());
+        }
+      }
+      return obj;
+    }
     @Override
     public JSONObject handle(String key, Map value, JSONObject obj) {
       return handler.handle(key, value, obj);

http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/d0d6c2c3/metron-platform/metron-parsers/src/test/java/org/apache/metron/parsers/json/JSONMapParserTest.java
----------------------------------------------------------------------
diff --git a/metron-platform/metron-parsers/src/test/java/org/apache/metron/parsers/json/JSONMapParserTest.java
b/metron-platform/metron-parsers/src/test/java/org/apache/metron/parsers/json/JSONMapParserTest.java
index 1299d97..61748e7 100644
--- a/metron-platform/metron-parsers/src/test/java/org/apache/metron/parsers/json/JSONMapParserTest.java
+++ b/metron-platform/metron-parsers/src/test/java/org/apache/metron/parsers/json/JSONMapParserTest.java
@@ -56,12 +56,23 @@ public class JSONMapParserTest {
 
   /**
    {
-    "collection" : { "blah" : 7, "blah2" : "foo" }
+    "collection" : { "blah" : 7, "blah2" : "foo", "bigblah" : { "innerBlah" : "baz", "reallyInnerBlah"
: { "color" : "grey" }}}
    }
    */
    @Multiline
    static String collectionHandlingJSON;
 
+  /**
+    {
+     "collection" : {
+        "key" : "value"
+      },
+     "key" : "value"
+    }
+   */
+  @Multiline
+  static String mixCollectionHandlingJSON;
+
   @Test
   public void testCollectionHandlingDrop() {
     JSONMapParser parser = new JSONMapParser();
@@ -102,11 +113,26 @@ public class JSONMapParserTest {
     List<JSONObject> output = parser.parse(collectionHandlingJSON.getBytes());
     Assert.assertEquals(output.size(), 1);
     //don't forget the timestamp field!
-    Assert.assertEquals(output.get(0).size(), 4);
+    Assert.assertEquals(output.get(0).size(), 6);
     JSONObject message = output.get(0);
     Assert.assertEquals(message.get("collection.blah"), 7);
     Assert.assertEquals(message.get("collection.blah2"), "foo");
+    Assert.assertEquals(message.get("collection.bigblah.innerBlah"),"baz");
+    Assert.assertEquals(message.get("collection.bigblah.reallyInnerBlah.color"),"grey");
     Assert.assertNotNull(message.get("timestamp"));
     Assert.assertTrue(message.get("timestamp") instanceof Number);
   }
+
+  @Test
+  public void testMixedCollectionHandlingUnfold() {
+    JSONMapParser parser = new JSONMapParser();
+    parser.configure(ImmutableMap.of(JSONMapParser.MAP_STRATEGY_CONFIG,JSONMapParser.MapStrategy.UNFOLD.name()));
+    List<JSONObject> output = parser.parse(mixCollectionHandlingJSON.getBytes());
+    Assert.assertEquals(output.get(0).size(), 4);
+    JSONObject message = output.get(0);
+    Assert.assertEquals(message.get("collection.key"), "value");
+    Assert.assertEquals(message.get("key"),"value");
+    Assert.assertNotNull(message.get("timestamp"));
+    Assert.assertTrue(message.get("timestamp") instanceof Number );
+  }
 }


Mime
View raw message