metron-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ottobackwards <...@git.apache.org>
Subject [GitHub] metron pull request #727: METRON-1146: Add ability to parse JSON string into...
Date Tue, 12 Sep 2017 11:06:00 GMT
Github user ottobackwards commented on a diff in the pull request:

    https://github.com/apache/metron/pull/727#discussion_r138315823
  
    --- Diff: metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/dsl/functions/StringFunctions.java
---
    @@ -550,4 +548,90 @@ else if(var.length() == 0) {
           return new ParseException("Unable to parse JSON string");
         }
       }
    +
    +  @Stellar(name = "JSON_TO_MAP"
    +          , description = "Returns a MAP object for the specified JSON string"
    +          , params = {
    +          "str - the JSON String to convert, may be null"
    +  }
    +          , returns = "a MAP object containing the parsed JSON string"
    +  )
    +  public static class JsonToMap extends BaseStellarFunction {
    +
    +    @Override
    +    public Object apply(List<Object> strings) {
    +
    +      if (strings == null || strings.size() == 0) {
    +        throw new IllegalArgumentException("[JSON_TO_MAP] incorrect arguments. Usage:
JSON_TO_MAP <JSON String>");
    +      }
    +      String var = (strings.get(0) == null) ? null : (String) strings.get(0);
    +      if (var == null) {
    +        return null;
    +      } else if (var.length() == 0) {
    +        return var;
    +      } else {
    +        ObjectMapper objectMapper = new ObjectMapper();
    +
    +        // First parse and check if input is valid JSON string
    +        try {
    +          objectMapper.readTree((String) strings.get(0));
    +        } catch (JsonProcessingException ex) {
    +          throw new ParseException("Valid JSON string not supplied", ex);
    +        } catch (IOException e) {
    +          e.printStackTrace();
    +        }
    +
    +        // Return parsed JSON Object as a HashMap
    +        try {
    +            return (HashMap) JSONUtils.INSTANCE.load((String) strings.get(0), Object.class);
    +        } catch (IOException e) {
    +          e.printStackTrace();
    +        }
    +      }
    +      return new ParseException("Unable to parse JSON string");
    +    }
    +  }
    +
    +  @Stellar(name = "JSON_TO_LIST"
    +          , description = "Returns a List object for the specified JSON string"
    +          , params = {
    +          "str - the JSON String to convert, may be null"
    +  }
    +          , returns = "a List object containing the parsed JSON string"
    +  )
    +  public static class JsonToList extends BaseStellarFunction {
    +
    +    @Override
    +    public Object apply(List<Object> strings) {
    +
    +      if (strings == null || strings.size() == 0) {
    +        throw new IllegalArgumentException("[JSON_TO_LIST] incorrect arguments. Usage:
JSON_TO_LIST <JSON String>");
    +      }
    +      String var = (strings.get(0) == null) ? null : (String) strings.get(0);
    +      if (var == null) {
    +        return null;
    +      } else if (var.length() == 0) {
    +        return var;
    +      } else {
    +        ObjectMapper objectMapper = new ObjectMapper();
    +
    +        // First parse and check if input is valid JSON string
    +        try {
    +          objectMapper.readTree((String) strings.get(0));
    +        } catch (JsonProcessingException ex) {
    +          throw new ParseException("Valid JSON string not supplied", ex);
    +        } catch (IOException e) {
    +          e.printStackTrace();
    +        }
    +
    --- End diff --
    
    Ok, I see from your tests it is list object:
    
    SO variables = JSONUtils.INSTANCE.load(new FileInputStream(variablesFile.get()), new TypeReference<List<Object>()
{


---

Mime
View raw message