Interface JsonObject

All Superinterfaces:
Serializable

public interface JsonObject extends Serializable
Interface used to expose JSON data.
  • Method Details

    • contains

      boolean contains(String name)
      Parameters:
      name - the name of the property.
      Returns:
      true if the json object contains the given property.
    • isEmpty

      boolean isEmpty()
      Method to check if the underlying json object is empty.
      Returns:
      true if the jsonObject is empty.
    • getAsList

      <T> List<T> getAsList(String name, Class<T> type)
      Parses the json object for the given property name and returns a list of type JsonObject. If the property with the given name is not found, an empty list is returned.
      Type Parameters:
      T - the type of the list elements.
      Parameters:
      name - the property inside this json object which contains a list as values of type JsonObject.
      type - type parameter for generic type JsonObject.
      Returns:
      the list of type JsonObject.
      Throws:
      JsonParsingException - if the json object with the given key is not a list or list elements are not of type JsonObject.
    • getAsStringList

      List<String> getAsStringList(String name)
      Parses the json object for the given property name and returns a String list. If the property with the given name is not found, an empty list is returned.

      For example "aud" : "single-value" or "aud" : ["value-1", "value-2"]

      Parameters:
      name - the property inside this json object which contains a String list.
      Returns:
      the String list.
      Throws:
      JsonParsingException - if the json object with the given key is not a String array or of type String.
      See Also:
    • getAsString

      @Nullable String getAsString(String name)
      Returns the string identified by the given property name. If the property with the given name is not found, null is returned.
      Parameters:
      name - the name of the property.
      Returns:
      the json string object.
      Throws:
      JsonParsingException - if the json object identified by the given property is not a string.
    • getAsInstant

      @Nullable Instant getAsInstant(String name)
      Returns an Instant identified by the given property name. If the property with the given name is not found, null is returned.
      Parameters:
      name - the name of the property.
      Returns:
      the Instant object.
      Throws:
      JsonParsingException - if the json object identified by the given property does not represent a date in unix time.
    • getAsLong

      @Nullable Long getAsLong(String name)
      Returns a Long identified by the given property name. If the property with the given name is not found, null is returned.
      Parameters:
      name - the name of property.
      Returns:
      the Long object.
      Throws:
      JsonParsingException - if the json object identified by the given property does not represent a long value
    • getJsonObject

      @Nullable JsonObject getJsonObject(String name)
      Returns a nested JSON object as @{link JsonObject} instance.
      Parameters:
      name - the name of property.
      Returns:
      the JsonObject.
      Throws:
      JsonParsingException - if the json object identified by the given property is not a JSON object structure.
    • getJsonObjects

      List<JsonObject> getJsonObjects(String name)
      Returns a nested array of JSON objects as list of JsonObject instances. If the property with the given name is not found, an empty list is returned.
      Parameters:
      name - the name of property.
      Returns:
      a list of JsonObject instances.
      Throws:
      JsonParsingException - if the json object identified by the given property is not an array of JSON objects.
    • getKeyValueMap

      Map<String,String> getKeyValueMap()
      Returns a key-value map of the JSON properties.

      Example:

       {
              @code
              String vcapServices = System.getenv(CFConstants.VCAP_SERVICES);
              JsonObject serviceJsonObject = new DefaultJsonObject(vcapServices).getJsonObjects(Service.XSUAA.getCFName())
                              .get(0);
              Map<String, String> xsuaaConfigMap = serviceJsonObject.getKeyValueMap();
              Map<String, String> credentialsMap = serviceJsonObject.getJsonObject(CFConstants.CREDENTIALS)
                              .getKeyValueMap();
       }
       
      Returns:
      the json properties as key-value map
    • asJsonString

      String asJsonString()
      Returns the json object as a json string.
      Returns:
      the json object in string representation.