Class JsonDescriptor

java.lang.Object
com.tccc.kos.commons.util.json.JsonDescriptor

public class JsonDescriptor extends Object
Encapsulation of a descriptor file used by various resources such as KAB files and assemblies. This is a generic way to load a json structure into an internal representation which can then be accessed using dotted notation. This supports fetching individual values using dotted notation as well as deserializing entire sections of the graph using dotted notation and a class.

This object will serialize to json as the a raw json graph.

Since:
1.0
Version:
2022-09-28
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new descriptor with the root node.
    Create a new descriptor from a file.
    Create a new descriptor from an input stream.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    getBean(String path, JavaType type)
    Finds the specified node in the descriptor using dotted notation, and then either converts to the specified type or returns null.
    <T> T
    getBean(String path, Class<T> clazz)
    Finds the specified node in the descriptor using dotted notation, and then either converts to the specified type or returns null.
    boolean
    Return the boolean value of the specified node.
    boolean
    getBoolean(String path, boolean def)
    Return the boolean value of the specified node and return the default value if not found or null.
    double
    Return the double value of the specified node.
    double
    getDouble(String path, double def)
    Return the long value of the specified node and return the default value if not found or null.
    int
    getInt(String path)
    Return the int value of the specified node.
    int
    getInt(String path, int def)
    Return the int value of the specified node and return the default value if not found or null.
    long
    Return the long value of the specified node.
    long
    getLong(String path, long def)
    Return the long value of the specified node and return the default value if not found or null.
    Finds the specified node in the descriptor using dotted notation.
    <T> T
    getNonNullBean(String path, Class<T> clazz)
    Finds the specified node in the descriptor using dotted notation, and then either converts to the specified type or returns a new instance of the class.
     
    Return the string value of the specified node.
    getString(String path, String def)
    Return the string value of the specified node and return the default value if not found or null.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JsonDescriptor

      public JsonDescriptor(File file) throws IOException
      Create a new descriptor from a file.
      Parameters:
      file - the file containing the descriptor json
      Throws:
      IOException
    • JsonDescriptor

      public JsonDescriptor(InputStream is) throws IOException
      Create a new descriptor from an input stream.
      Parameters:
      is - input stream containing the descriptor json
      Throws:
      IOException
    • JsonDescriptor

      public JsonDescriptor(JsonNode root)
      Create a new descriptor with the root node.
      Parameters:
      root - the root node of the descriptor json
  • Method Details

    • getNode

      public JsonNode getNode(String path)
      Finds the specified node in the descriptor using dotted notation.
      Parameters:
      path - dotted notation of fields
    • getString

      public String getString(String path)
      Return the string value of the specified node.
      Parameters:
      path - dotted notation of the fields
    • getString

      public String getString(String path, String def)
      Return the string value of the specified node and return the default value if not found or null.
      Parameters:
      path - dotted notation of the fields
      def - the default value
    • getInt

      public int getInt(String path)
      Return the int value of the specified node.
      Parameters:
      path - dotted notation of the fields
      Returns:
      int value of the node or 0
    • getInt

      public int getInt(String path, int def)
      Return the int value of the specified node and return the default value if not found or null.
      Parameters:
      path - dotted notation of the fields
      def - the default value
    • getLong

      public long getLong(String path)
      Return the long value of the specified node.
      Parameters:
      path - dotted notation of the fields
      Returns:
      long value of the node or 0
    • getLong

      public long getLong(String path, long def)
      Return the long value of the specified node and return the default value if not found or null.
      Parameters:
      path - dotted notation of the fields
      def - the default value
    • getDouble

      public double getDouble(String path)
      Return the double value of the specified node.
      Parameters:
      path - dotted notation of the fields
      Returns:
      double value of the node or 0
    • getDouble

      public double getDouble(String path, double def)
      Return the long value of the specified node and return the default value if not found or null.
      Parameters:
      path - dotted notation of the fields
      def - the default value
    • getBoolean

      public boolean getBoolean(String path)
      Return the boolean value of the specified node.
      Parameters:
      path - dotted notation of the fields
      Returns:
      boolean value of the node or false
    • getBoolean

      public boolean getBoolean(String path, boolean def)
      Return the boolean value of the specified node and return the default value if not found or null.
      Parameters:
      path - dotted notation of the fields
      def - the default value
    • getBean

      public <T> T getBean(String path, Class<T> clazz)
      Finds the specified node in the descriptor using dotted notation, and then either converts to the specified type or returns null.
      Parameters:
      path - dotted notation of fields
      clazz - the type to convert to
    • getNonNullBean

      public <T> T getNonNullBean(String path, Class<T> clazz)
      Finds the specified node in the descriptor using dotted notation, and then either converts to the specified type or returns a new instance of the class.
      Parameters:
      path - dotted notation of fields
      clazz - the type to convert to
    • getBean

      public <T> T getBean(String path, JavaType type)
      Finds the specified node in the descriptor using dotted notation, and then either converts to the specified type or returns null.
      Parameters:
      path - dotted notation of fields
      type - the type to convert to
    • getRoot

      public JsonNode getRoot()