Interface JSONNode

All Superinterfaces:
Comparable<JSONNode>, Serializable
All Known Subinterfaces:
JSONArray, JSONListNode, JSONMapNode, JSONObject

public interface JSONNode
extends Serializable, Comparable<JSONNode>
Base interface for JSON entities
Author:
Jim Earley
  • Method Summary

    Modifier and Type Method Description
    default JSONArray asJSONArray()
    Return the JSONNode as a JSONArray
    default JSONObject asJSONObject()
    Return the JSONNode as a JSONObject
    default boolean isArray()
    Interrogate if the JSONNode is a JSONArray
    default boolean isEquivalent​(JSONNode other)
    Compares against another JSONNode for equivalence.
    default boolean isObject()
    Interrogate if the JSONNode is a JSONObject
    String prettyPrint()
    Pretty print the JSON as a string
    String prettyPrint​(int indent)
    Pretty print JSON string with specified indents
    default JSONArray select​(String jsonPath)
    Select a value from the JSONNode using a JSON-Path expression.
    default JSONArray select​(String jsonPath, com.jayway.jsonpath.Criteria criteria, com.jayway.jsonpath.Option... options)
    Select a value from the JSONNode using a JSON-Path expression.
    default JSONArray select​(String jsonPath, com.jayway.jsonpath.Option... options)
    Select a value from the JSONNode using a JSON-Path expression.
    default <T> T selectValue​(String jsonPath)
    Select a single value from a JSONPath expression
    default <T> T selectValue​(String jsonPath, com.jayway.jsonpath.Criteria criteria, com.jayway.jsonpath.Option... options)  
    default <T> T selectValue​(String jsonPath, com.jayway.jsonpath.Option... options)  
    String toJSONString()
    Return as a JSON String
    default void write​(File file)
    Write the current node to a File
    default void write​(File outputFile, boolean prettyPrint)
    Write the current node with optional pretty printing
    default void write​(OutputStream out)
    Write the current node to an OutputStream
    default void write​(OutputStream out, boolean prettyPrint)
    Write the current node with optional pretty printing
    default void write​(Writer writer)
    Write the current node to a Writer
    default void write​(Writer writer, boolean prettyPrint)
    Write the current node with optional pretty printing
    default void write​(Path outputPath)
    Write the current node to a Path
    default void write​(Path outputPath, boolean prettyPrint)
    Write the current node with optional pretty printing

    Methods inherited from interface java.lang.Comparable

    compareTo
  • Method Details

    • toJSONString

      String toJSONString()
      Return as a JSON String
      Returns:
      String representation of the JSONNode. Results are serialized as a non-formatted string
    • prettyPrint

      String prettyPrint()
      Pretty print the JSON as a string
      Returns:
      pretty printed JSON string
    • prettyPrint

      String prettyPrint​(int indent)
      Pretty print JSON string with specified indents
      Parameters:
      indent - the number of spaces to indent
      Returns:
      the pretty printed JSON string
    • isArray

      default boolean isArray()
      Interrogate if the JSONNode is a JSONArray
      Returns:
      true if it is a JSONArray; false otherwise
    • isObject

      default boolean isObject()
      Interrogate if the JSONNode is a JSONObject
      Returns:
      true if it is a JSONObject; false otherwise
    • asJSONObject

      default JSONObject asJSONObject()
      Return the JSONNode as a JSONObject
      Returns:
      a JSONObject representation of the JSONNode
    • asJSONArray

      default JSONArray asJSONArray()
      Return the JSONNode as a JSONArray
      Returns:
      the node cast as a JSONArray
    • select

      default JSONArray select​(String jsonPath)
      Select a value from the JSONNode using a JSON-Path expression.

      This is a wrapper around the JSONPath API

      Parameters:
      jsonPath - the JSON-Path expression
      Returns:
      A JSONArray of values.
    • select

      default JSONArray select​(String jsonPath, com.jayway.jsonpath.Option... options)
      Select a value from the JSONNode using a JSON-Path expression.

      This is a wrapper around the JSONPath API

      Parameters:
      jsonPath - the JSON-Path expression
      options - One or more options to implement on the JSONPath query
      Returns:
      A JSONArray of values
    • select

      default JSONArray select​(String jsonPath, com.jayway.jsonpath.Criteria criteria, com.jayway.jsonpath.Option... options)
      Select a value from the JSONNode using a JSON-Path expression.

      This is a wrapper around the JSONPath API

      Parameters:
      jsonPath - the JSON-Path expression
      options - One or more options to implement on the JSONPath query
      Returns:
      A JSONArray of values
    • selectValue

      default <T> T selectValue​(String jsonPath)
      Select a single value from a JSONPath expression
      Parameters:
      jsonPath - the JSONPath expression
      Returns:
      the typed value (or null). For primitive Java types, users should take care to understand the data
    • selectValue

      default <T> T selectValue​(String jsonPath, com.jayway.jsonpath.Option... options)
    • selectValue

      default <T> T selectValue​(String jsonPath, com.jayway.jsonpath.Criteria criteria, com.jayway.jsonpath.Option... options)
    • write

      default void write​(OutputStream out)
      Write the current node to an OutputStream
      Parameters:
      out - the OutputStream
    • write

      default void write​(Path outputPath)
      Write the current node to a Path
      Parameters:
      outputPath - the Path
    • write

      default void write​(Writer writer)
      Write the current node to a Writer
      Parameters:
      writer - the Writer
    • write

      default void write​(File file)
      Write the current node to a File
      Parameters:
      file - the File
    • write

      default void write​(OutputStream out, boolean prettyPrint)
      Write the current node with optional pretty printing
      Parameters:
      out - the OutputStream
      prettyPrint - true if pretty printing is enabled; false otherwise
    • write

      default void write​(Path outputPath, boolean prettyPrint)
      Write the current node with optional pretty printing
      Parameters:
      out - the Path
      prettyPrint - true if pretty printing is enabled; false otherwise
    • write

      default void write​(File outputFile, boolean prettyPrint)
      Write the current node with optional pretty printing
      Parameters:
      out - the File
      prettyPrint - true if pretty printing is enabled; false otherwise
    • write

      default void write​(Writer writer, boolean prettyPrint)
      Write the current node with optional pretty printing
      Parameters:
      out - the Writer
      prettyPrint - true if pretty printing is enabled; false otherwise
    • isEquivalent

      default boolean isEquivalent​(JSONNode other)
      Compares against another JSONNode for equivalence. Equivalance is true if:
      1. Each JSONNode is of the same type
      2. For JSONObjects:
        • Must contain the same keys, though not necessarily in any order
        • Each key's value must be equivalent
      3. For JSONArrays:
        • List order does matter
        • Each value in the list must be equivalent
      Parameters:
      other - The other JSONNode to compare against
      Returns:
      true if both JSONNodes are equivalent; false otherwise