Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/48#discussion_r14703939
  
    --- Diff: core/src/main/java/brooklyn/event/feed/http/JsonFunctions.java ---
    @@ -132,6 +133,22 @@ private JsonFunctions() {} // instead use static 
utility methods
             };
         }
     
    +    /**
    +     * returns an element from a json object given a full path {@link 
com.jayway.jsonpath.JsonPath}
    +     */
    +    public static Function<JsonElement,? extends JsonElement> 
getPath(final String element) {
    +        return new Function<JsonElement, JsonElement>() {
    +            @Override public JsonElement apply(JsonElement input) {
    +                String jsonString = input.toString();
    +                JsonParser jsonParser = new JsonParser();
    +                JsonElement curr = 
jsonParser.parse(JsonPath.<String>read(jsonString, element));
    --- End diff --
    
    Interesting! I hadn't come across jsonPath before.
    
    A few code comments:
    * JsonPath.read(jsonString, element) will not necessarily return a `String` 
- e.g. if with test code 
`{"europe":{"uk":{"edinburgh":{"population":500000,"weather":"wet","lighting":"dark"}},"france":{"population":80000000}}}`
 then `$.europe.france.population` returns an `Integer`.
    * `JsonPath.<String>read(jsonString, element)` returns null if it could not 
be found - this causes an NPE in the method `jsonParser.parse(...)`, rather 
than it getting to your nicer exception below.
    * Would be good to add some unit test cases for this.
    
    For test cases, I played around with the code below in `JsonFunctionsTest`:
    
        @Test
        public void testJsonPath() {
            JsonElement pop = 
JsonFunctions.getPath("$.europe.france.population").apply( europeMap() );
            Assert.assertEquals( 
(int)JsonFunctions.cast(Integer.class).apply(pop), 80*1000*1000 );
        }



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to