Class JsonObject


  • public class JsonObject
    extends java.lang.Object
    A helper object for evaluating Json content by utilizing JsonPath expressions.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static JsonObject createFrom​(java.io.InputStream in)
      Parses JSON content from the input stream and creates new json object
      static JsonObject createFrom​(java.lang.String json)
      Parses JSON content and creates new json object
      boolean exists​(java.lang.String path)
      Checks whether the specified path exists in this JSON object.
      boolean getBoolean​(java.lang.String jsonPath)
      Looks up a boolean value in the parsed JSON
      java.util.List<java.util.Map<java.lang.String,​java.lang.Object>> getCollection​(java.lang.String jsonPath)
      Looks up entries in the parsed JSONArray
      java.util.List<java.lang.Object> getCollectionOfObjects​(java.lang.String path)
      Retrieves a collection of data at the location in this object specified by the path.
      java.util.List<java.util.Map<java.lang.String,​java.lang.Object>> getFilteredCollection​(java.lang.String jsonPath, com.jayway.jsonpath.Predicate filter)
      Looks up matching entries in the parsed JSONArray
      java.lang.Object getObject​(java.lang.String jsonPath)
      Looks up a value in the parsed JSON.
      java.lang.String getString​(java.lang.String jsonPath)
      Looks up a value in the parsed JSON
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • createFrom

        public static JsonObject createFrom​(java.io.InputStream in)
        Parses JSON content from the input stream and creates new json object
        Parameters:
        in - an input stream containing JSON
        Returns:
        presentation of the parsed content
      • createFrom

        public static JsonObject createFrom​(java.lang.String json)
        Parses JSON content and creates new json object
        Parameters:
        json - a string in JSON format
        Returns:
        presentation of the parsed content
      • getString

        public java.lang.String getString​(java.lang.String jsonPath)
        Looks up a value in the parsed JSON
        Parameters:
        jsonPath - a JSON path, e.g. product.code, pointing to the element whose value needs to be retrieved.
        Returns:
        value of the element matching the jsonPath location.
      • getObject

        public java.lang.Object getObject​(java.lang.String jsonPath)
        Looks up a value in the parsed JSON. This method is convenient when it's not known what type the path should return.
        Parameters:
        jsonPath - a JSON path, e.g. product.code, pointing to the element whose value needs to be retrieved.
        Returns:
        value of the element matching the jsonPath location.
      • getFilteredCollection

        public java.util.List<java.util.Map<java.lang.String,​java.lang.Object>> getFilteredCollection​(java.lang.String jsonPath,
                                                                                                            com.jayway.jsonpath.Predicate filter)
        Looks up matching entries in the parsed JSONArray
        Parameters:
        jsonPath - a JSON path, e.g. product.code, pointing to the element whose value needs to be retrieved.
        filter - Predicate used for filtering matching entry results
        Returns:
        List of entries matching the jsonPath location and the predicate.
      • getCollection

        public java.util.List<java.util.Map<java.lang.String,​java.lang.Object>> getCollection​(java.lang.String jsonPath)
        Looks up entries in the parsed JSONArray
        Parameters:
        jsonPath - a JSON path, e.g. product.code, pointing to the element whose value needs to be retrieved.
        Returns:
        List of entries matching the jsonPath location.
        Throws:
        java.lang.ClassCastException - when the path points to a non-collection value
        com.jayway.jsonpath.PathNotFoundException - when the path does not exist
      • getCollectionOfObjects

        public java.util.List<java.lang.Object> getCollectionOfObjects​(java.lang.String path)
        Retrieves a collection of data at the location in this object specified by the path.
        Parameters:
        path - path expression to select a collection of objects.
        Returns:
        a collection of data selected by the path expression; null means the path is invalid or does not exist or the path is pointing to a non-collection element in this json object.
      • getBoolean

        public boolean getBoolean​(java.lang.String jsonPath)
        Looks up a boolean value in the parsed JSON
        Parameters:
        jsonPath - a JSON path, e.g. catalog.active, pointing to the element whose value needs to be retrieved.
        Returns:
        value of the element matching the jsonPath location.
        See Also:
        for the rules about how the boolean value is derived
      • exists

        public boolean exists​(java.lang.String path)
        Checks whether the specified path exists in this JSON object.
        Parameters:
        path - a path to check.
        Returns:
        true, if the path exists in this JSON object and contains non-null value or non-empty value in case when the path resolves to an array; false, otherwise.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object