Class JsonUtil


  • public class JsonUtil
    extends Object
    Utilities to make it easier to access JSON data and convert between JSON encodings and plain java encodings.
    Author:
    Dave Reynolds
    • Constructor Summary

      Constructors 
      Constructor Description
      JsonUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.apache.jena.atlas.json.JsonValue asJson​(Object obj)
      Convert java objects to JSON objects.
      static org.apache.jena.atlas.json.JsonObject emptyObject()
      Return a new singleton empty object
      static Object fromJson​(org.apache.jena.atlas.json.JsonValue jv)
      Convert a JSON value to a corresponding plain Java value.
      static boolean getBooleanValue​(org.apache.jena.atlas.json.JsonObject jo, String field, boolean dflt)  
      static int getIntValue​(org.apache.jena.atlas.json.JsonObject jo, String field, int dflt)  
      static long getLongValue​(org.apache.jena.atlas.json.JsonObject jo, String field, long dflt)  
      static <T> T getPath​(org.apache.jena.atlas.json.JsonValue root, Class<T> target, Object... selectors)
      Return an element of a JSON structure by following a sequence of object field selectors (strings) or array indexes (integers) and return the java-ized version of the leaf.
      static Object getPath​(org.apache.jena.atlas.json.JsonValue root, Object... selectors)
      Return an element of a JSON structure by following a sequence of object field selectors (strings) or array indexes (integers) and return the java-ized version of the leaf.
      static String getStringValue​(org.apache.jena.atlas.json.JsonObject jo, String field)  
      static String getStringValue​(org.apache.jena.atlas.json.JsonObject jo, String field, String dflt)  
      static org.apache.jena.atlas.json.JsonValue loadFromFile​(String filename)  
      static org.apache.jena.atlas.json.JsonObject makeJson​(Object... args)
      Create a simple Json Object.
      static org.apache.jena.atlas.json.JsonObject makeJson​(org.apache.jena.atlas.json.JsonObject base, Object... args)
      Create a JSON object as a copy of an existing object, with new/replacement key values taken from the args
      static org.apache.jena.atlas.json.JsonObject merge​(org.apache.jena.atlas.json.JsonObject base, org.apache.jena.atlas.json.JsonObject extend)
      Create a new json object by cloning one object and then asserting all values from the second.
      static org.apache.jena.atlas.json.JsonObject mergeInto​(org.apache.jena.atlas.json.JsonObject base, org.apache.jena.atlas.json.JsonObject extend)
      Merge all the extend values into the base object returning the base object, no copy.
      static org.apache.jena.atlas.json.JsonArray readArray​(String filename)
      Parse a JSON or YAML file to an array of json objects, supports multi-document yaml files
      static org.apache.jena.atlas.json.JsonArray readArray​(String filename, InputStream is)
      Parse a JSON or YAML file to an array of json objects, supports multi-document yaml files
      static org.apache.jena.atlas.json.JsonObject readObject​(String filename)
      Parse a JSON or YAML file to a json object.
      static org.apache.jena.atlas.json.JsonObject readObject​(String filename, InputStream is)
      Parse a JSON or YAML file to a json object.
    • Constructor Detail

      • JsonUtil

        public JsonUtil()
    • Method Detail

      • loadFromFile

        public static org.apache.jena.atlas.json.JsonValue loadFromFile​(String filename)
      • getStringValue

        public static String getStringValue​(org.apache.jena.atlas.json.JsonObject jo,
                                            String field)
      • getStringValue

        public static String getStringValue​(org.apache.jena.atlas.json.JsonObject jo,
                                            String field,
                                            String dflt)
      • getIntValue

        public static int getIntValue​(org.apache.jena.atlas.json.JsonObject jo,
                                      String field,
                                      int dflt)
      • getLongValue

        public static long getLongValue​(org.apache.jena.atlas.json.JsonObject jo,
                                        String field,
                                        long dflt)
      • getBooleanValue

        public static boolean getBooleanValue​(org.apache.jena.atlas.json.JsonObject jo,
                                              String field,
                                              boolean dflt)
      • asJson

        public static org.apache.jena.atlas.json.JsonValue asJson​(Object obj)
        Convert java objects to JSON objects. Handles strings, numbers, booleans, maps and lists and simple nests thereof. Throws runtime exception if this is not possible.
      • makeJson

        public static org.apache.jena.atlas.json.JsonObject makeJson​(Object... args)
        Create a simple Json Object.
        Parameters:
        args - alternating parameter names and parameter values
      • makeJson

        public static org.apache.jena.atlas.json.JsonObject makeJson​(org.apache.jena.atlas.json.JsonObject base,
                                                                     Object... args)
        Create a JSON object as a copy of an existing object, with new/replacement key values taken from the args
        Parameters:
        base - object to copy, can be null
        args - alternating parameter names and parameter values
      • fromJson

        public static Object fromJson​(org.apache.jena.atlas.json.JsonValue jv)
        Convert a JSON value to a corresponding plain Java value. A near inverse of asJson except that number types might be changed.
      • getPath

        public static Object getPath​(org.apache.jena.atlas.json.JsonValue root,
                                     Object... selectors)
        Return an element of a JSON structure by following a sequence of object field selectors (strings) or array indexes (integers) and return the java-ized version of the leaf. Throws runtime exception if the structure does not match.
      • getPath

        public static <T> T getPath​(org.apache.jena.atlas.json.JsonValue root,
                                    Class<T> target,
                                    Object... selectors)
        Return an element of a JSON structure by following a sequence of object field selectors (strings) or array indexes (integers) and return the java-ized version of the leaf. Returns the value as the requested target class if possible. Throws runtime exception if the structure does not match.
      • emptyObject

        public static org.apache.jena.atlas.json.JsonObject emptyObject()
        Return a new singleton empty object
      • merge

        public static org.apache.jena.atlas.json.JsonObject merge​(org.apache.jena.atlas.json.JsonObject base,
                                                                  org.apache.jena.atlas.json.JsonObject extend)
        Create a new json object by cloning one object and then asserting all values from the second. Shallow copy only.
      • mergeInto

        public static org.apache.jena.atlas.json.JsonObject mergeInto​(org.apache.jena.atlas.json.JsonObject base,
                                                                      org.apache.jena.atlas.json.JsonObject extend)
        Merge all the extend values into the base object returning the base object, no copy.
      • readArray

        public static org.apache.jena.atlas.json.JsonArray readArray​(String filename,
                                                                     InputStream is)
        Parse a JSON or YAML file to an array of json objects, supports multi-document yaml files
      • readArray

        public static org.apache.jena.atlas.json.JsonArray readArray​(String filename)
                                                              throws FileNotFoundException
        Parse a JSON or YAML file to an array of json objects, supports multi-document yaml files
        Throws:
        FileNotFoundException
      • readObject

        public static org.apache.jena.atlas.json.JsonObject readObject​(String filename,
                                                                       InputStream is)
        Parse a JSON or YAML file to a json object.