Class Node

    • Method Detail

      • parse

        public static Node parse​(java.lang.String json)
        Attempts to parse the given JSON string and return a Node.
        Parameters:
        json - JSON text to parse.
        Returns:
        Returns the parsed Node on success.
        Throws:
        ModelSyntaxException - if the JSON text is invalid.
      • parse

        public static Node parse​(java.lang.String json,
                                 java.lang.String file)
        Attempts to parse the given JSON string and File Name and return a Node.
        Parameters:
        json - JSON text to parse.
        file - Filename corresponding to json text
        Returns:
        Returns the parsed Node on success.
        Throws:
        ModelSyntaxException - if the JSON text is invalid.
      • parse

        public static Node parse​(java.io.InputStream json)
        Attempts to parse the given JSON input stream and returns a Node.
        Parameters:
        json - JSON input stream to parse. The input stream is closed automatically when the content is fully parsed.
        Returns:
        Returns the parsed Node on success.
        Throws:
        ModelSyntaxException - if the JSON text is invalid.
      • parse

        public static Node parse​(java.io.InputStream json,
                                 java.lang.String file)
        Attempts to parse the given JSON input stream and returns a Node.
        Parameters:
        json - JSON input stream to parse. The input stream is closed automatically when the content is fully parsed.
        file - Filename corresponding to json text
        Returns:
        Returns the parsed Node on success.
        Throws:
        ModelSyntaxException - if the JSON text is invalid.
      • parseJsonWithComments

        public static Node parseJsonWithComments​(java.lang.String json,
                                                 java.lang.String file)
        Attempts to parse the given JSON string and File Name and return a Node.

        This parser allows for comments in the JSON.

        Parameters:
        json - JSON text to parse.
        file - Filename corresponding to json text
        Returns:
        Returns the parsed Node on success.
        Throws:
        ModelSyntaxException - if the JSON text is invalid.
      • parseJsonWithComments

        public static Node parseJsonWithComments​(java.lang.String json)
        Attempts to parse the given JSON string and return a Node.

        This parser allows for comments in the JSON.

        Parameters:
        json - JSON text to parse.
        Returns:
        Returns the parsed Node on success.
        Throws:
        ModelSyntaxException - if the JSON text is invalid.
      • prettyPrintJson

        public static java.lang.String prettyPrintJson​(Node node)
        Writes the contents of a Node to a pretty-printed JSON string.
        Parameters:
        node - Node to write.
        Returns:
        Returns the serialized Node.
      • prettyPrintJson

        public static java.lang.String prettyPrintJson​(Node node,
                                                       java.lang.String indentString)
        Writes the contents of a Node to a pretty-printed JSON string.
        Parameters:
        node - Node to write.
        indentString - String to use for indention.
        Returns:
        Returns the serialized Node.
      • printJson

        public static java.lang.String printJson​(Node node)
        Writes the contents of a Node to a non-pretty-printed JSON string.
        Parameters:
        node - Node to write.
        Returns:
        Returns the serialized Node.
      • from

        public static StringNode from​(java.lang.String value)
        Create a StringNode from a String value.
        Parameters:
        value - Value to create a node from.
        Returns:
        Returns the created StringNode.
      • from

        public static NumberNode from​(java.lang.Number number)
        Create a NumberNode from a Number value.
        Parameters:
        number - Value to create a node from.
        Returns:
        Returns the created NumberNode.
      • from

        public static BooleanNode from​(boolean value)
        Create a BooleanNode from a boolean value.
        Parameters:
        value - Value to create a node from.
        Returns:
        Returns the created BooleanNode.
      • from

        public static Node from​(ToNode value)
        Create a Node from a potentially null ToNode value.
        Parameters:
        value - Value to create a node from.
        Returns:
        Returns the created Node.
      • fromNodes

        public static ArrayNode fromNodes​(java.util.List<? extends Node> values)
        Creates an ArrayNode from a Collection of Node values.
        Parameters:
        values - String values to add to the ArrayNode.
        Returns:
        Returns the created ArrayNode.
      • fromNodes

        public static ArrayNode fromNodes​(Node... values)
        Creates an ArrayNode from a variadic list of Node values.
        Parameters:
        values - String values to add to the ArrayNode.
        Returns:
        Returns the created ArrayNode.
      • fromStrings

        public static ArrayNode fromStrings​(java.util.Collection<java.lang.String> values)
        Creates an ArrayNode from a Collection of String values.
        Parameters:
        values - String values to add to the ArrayNode.
        Returns:
        Returns the created ArrayNode.
      • fromStrings

        public static ArrayNode fromStrings​(java.lang.String... values)
        Creates an ArrayNode from a variadic list of String values.
        Parameters:
        values - String values to add to the ArrayNode.
        Returns:
        Returns the created ArrayNode.
      • objectNode

        public static ObjectNode objectNode()
        Creates an empty ObjectNode.
        Returns:
        Returns the ObjectNode.
      • objectNode

        public static ObjectNode objectNode​(java.util.Map<StringNode,​Node> values)
        Creates an ObjectNode from the given map of Nodes.
        Parameters:
        values - Values to add to the object node.
        Returns:
        Returns the created ObjectNode.
      • arrayNode

        public static ArrayNode arrayNode()
        Creates an empty ArrayNode.
        Returns:
        Returns the ArrayNode.
      • arrayNode

        public static ArrayNode arrayNode​(Node... nodes)
        Creates an ArrayNode from a variadic list of Nodes.
        Parameters:
        nodes - Nodes to add to the array.
        Returns:
        Returns the created ArrayNode.
      • nullNode

        public static NullNode nullNode()
        Creates a NullNode.
        Returns:
        Returns the NullNode.
      • loadArrayOfString

        public static java.util.List<java.lang.String> loadArrayOfString​(java.lang.String descriptor,
                                                                         Node node)
        Expects an array of strings and returns the loaded strings.
        Parameters:
        descriptor - Name of the property being loaded.
        node - Node to load.
        Returns:
        Returns the loaded strings.
        Throws:
        SourceException - on error.
      • assertEquals

        public static void assertEquals​(ToNode actual,
                                        ToNode expected)
        Testing helper used to compare two Nodes for equivalence.

        Compares two Node values and throws if they aren't equal. The thrown exception contains a message that shows the differences between the two Nodes as returned by diff(ToNode, ToNode).

        Parameters:
        actual - Node to use as the starting node.
        expected - Node to compare against.
        Throws:
        ExpectationNotMetException - if the nodes are not equivalent.
      • diff

        public static java.util.List<java.lang.String> diff​(ToNode actual,
                                                            ToNode expected)
        Computes the differences between two Nodes as a String.
        Parameters:
        actual - Node to use as the starting node.
        expected - Node to compare against.
        Returns:
        Returns the differences as a String.
      • getType

        public abstract NodeType getType()
        Gets the type of the node.
        Returns:
        Returns the node type.
      • accept

        public abstract <R> R accept​(NodeVisitor<R> visitor)
        Accepts a visitor with the node.
        Type Parameters:
        R - visitor return type.
        Parameters:
        visitor - Visitor to dispatch to.
        Returns:
        Returns the accepted result.
      • isObjectNode

        public final boolean isObjectNode()
        Checks if this node is an object type.
        Returns:
        Returns true if this node is an object type.
      • isArrayNode

        public final boolean isArrayNode()
        Checks if this node is an array type.
        Returns:
        Returns true if this node is an array type.
      • isStringNode

        public final boolean isStringNode()
        Checks if this node is a string type.
        Returns:
        Returns true if this node is a string type.
      • isNumberNode

        public final boolean isNumberNode()
        Checks if this node is a number type.
        Returns:
        Returns true if this node is a number type.
      • isBooleanNode

        public final boolean isBooleanNode()
        Checks if this node is a boolean type.
        Returns:
        Returns true if this node is a boolean type.
      • isNullNode

        public final boolean isNullNode()
        Checks if this node is a null type.
        Returns:
        Returns true if this node is a null type.
      • asObjectNode

        public java.util.Optional<ObjectNode> asObjectNode()
        Gets the node as an ObjectNode if it is an object.
        Returns:
        Returns the optional object node.
      • asArrayNode

        public java.util.Optional<ArrayNode> asArrayNode()
        Gets the node as an ArrayNode if it is an array.
        Returns:
        Returns the optional array node.
      • asStringNode

        public java.util.Optional<StringNode> asStringNode()
        Gets the node as an StringNode if it is an string.
        Returns:
        Returns the optional StringNode.
      • asBooleanNode

        public java.util.Optional<BooleanNode> asBooleanNode()
        Gets the node as an BooleanNode if it is an boolean.
        Returns:
        Returns the optional BooleanNode.
      • asNumberNode

        public java.util.Optional<NumberNode> asNumberNode()
        Gets the node as an NumberNode if it is an number.
        Returns:
        Returns the optional NumberNode.
      • asNullNode

        public java.util.Optional<NullNode> asNullNode()
        Gets the node as an NullNode if it is a null.
        Returns:
        Returns the optional NullNode.
      • expectObjectNode

        public final ObjectNode expectObjectNode()
        Casts the current node to an ObjectNode.
        Returns:
        Returns an object node.
        Throws:
        ExpectationNotMetException - when the node is not an ObjectNode.
      • expectObjectNode

        public ObjectNode expectObjectNode​(java.lang.String message)
        Casts the current node to an ObjectNode, throwing ExpectationNotMetException when the node is the wrong type.
        Parameters:
        message - Error message to use if the node is of the wrong type.
        Returns:
        Returns an object node.
        Throws:
        ExpectationNotMetException - when the node is not an ObjectNode.
      • expectObjectNode

        public ObjectNode expectObjectNode​(java.util.function.Supplier<java.lang.String> message)
        Casts the current node to an ObjectNode, throwing ExpectationNotMetException when the node is the wrong type.
        Parameters:
        message - Error message supplier.
        Returns:
        Returns an object node.
        Throws:
        ExpectationNotMetException - when the node is not an ObjectNode.
      • expectArrayNode

        public final ArrayNode expectArrayNode()
        Casts the current node to an ArrayNode.
        Returns:
        Returns an array node.
        Throws:
        ExpectationNotMetException - when the node is not an ArrayNode.
      • expectArrayNode

        public ArrayNode expectArrayNode​(java.lang.String message)
        Casts the current node to an ArrayNode, throwing ExpectationNotMetException when the node is the wrong type.
        Parameters:
        message - Error message to use if the node is of the wrong type.
        Returns:
        Returns an array node.
        Throws:
        ExpectationNotMetException - when the node is the wrong type.
      • expectArrayNode

        public ArrayNode expectArrayNode​(java.util.function.Supplier<java.lang.String> message)
        Casts the current node to an ArrayNode, throwing ExpectationNotMetException when the node is the wrong type.
        Parameters:
        message - Error message supplier.
        Returns:
        Returns an array node.
        Throws:
        ExpectationNotMetException - when the node is the wrong type.
      • expectStringNode

        public final StringNode expectStringNode()
        Casts the current node to a StringNode.
        Returns:
        Returns a string node.
        Throws:
        ExpectationNotMetException - when the node is the wrong type.
      • expectStringNode

        public StringNode expectStringNode​(java.lang.String message)
        Casts the current node to a StringNode, throwing ExpectationNotMetException when the node is the wrong type.
        Parameters:
        message - Error message to use if the node is of the wrong type.
        Returns:
        Returns a string node.
        Throws:
        ExpectationNotMetException - when the node is the wrong type.
      • expectStringNode

        public StringNode expectStringNode​(java.util.function.Supplier<java.lang.String> message)
        Casts the current node to a StringNode, throwing ExpectationNotMetException when the node is the wrong type.
        Parameters:
        message - Error message supplier.
        Returns:
        Returns a string node.
        Throws:
        ExpectationNotMetException - when the node is the wrong type.
      • expectNumberNode

        public final NumberNode expectNumberNode()
        Casts the current node to a NumberNode.
        Returns:
        Returns a number node.
        Throws:
        ExpectationNotMetException - when the node is the wrong type.
      • expectNumberNode

        public NumberNode expectNumberNode​(java.lang.String message)
        Casts the current node to a NumberNode, throwing ExpectationNotMetException when the node is the wrong type.
        Parameters:
        message - Error message to use if the node is of the wrong type.
        Returns:
        Returns a number node.
        Throws:
        ExpectationNotMetException - when the node is the wrong type.
      • expectNumberNode

        public NumberNode expectNumberNode​(java.util.function.Supplier<java.lang.String> message)
        Casts the current node to a NumberNode, throwing ExpectationNotMetException when the node is the wrong type.
        Parameters:
        message - Error message supplier.
        Returns:
        Returns a number node.
        Throws:
        ExpectationNotMetException - when the node is the wrong type.
      • expectBooleanNode

        public final BooleanNode expectBooleanNode()
        Casts the current node to a BooleanNode.
        Returns:
        Returns a boolean node.
        Throws:
        ExpectationNotMetException - when the node is the wrong type.
      • expectBooleanNode

        public BooleanNode expectBooleanNode​(java.lang.String message)
        Casts the current node to a BooleanNode, throwing ExpectationNotMetException when the node is the wrong type.
        Parameters:
        message - Error message to use if the node is of the wrong type.
        Returns:
        Returns a boolean node.
        Throws:
        ExpectationNotMetException - when the node is the wrong type.
      • expectBooleanNode

        public BooleanNode expectBooleanNode​(java.util.function.Supplier<java.lang.String> message)
        Casts the current node to a BooleanNode, throwing ExpectationNotMetException when the node is the wrong type.
        Parameters:
        message - Error message supplier.
        Returns:
        Returns a boolean node.
        Throws:
        ExpectationNotMetException - when the node is the wrong type.
      • expectNullNode

        public final NullNode expectNullNode()
        Casts the current node to a NullNode.
        Returns:
        Returns a null node.
        Throws:
        ExpectationNotMetException - when the node is the wrong type.
      • expectNullNode

        public NullNode expectNullNode​(java.lang.String message)
        Casts the current node to a NullNode, throwing ExpectationNotMetException when the node is the wrong type.
        Parameters:
        message - Error message to use if the node is of the wrong type.
        Returns:
        Returns a null node.
        Throws:
        ExpectationNotMetException - when the node is the wrong type.
      • expectNullNode

        public NullNode expectNullNode​(java.util.function.Supplier<java.lang.String> message)
        Casts the current node to a NullNode, throwing ExpectationNotMetException when the node is the wrong type.
        Parameters:
        message - Error message supplier.
        Returns:
        Returns a null node.
        Throws:
        ExpectationNotMetException - when the node is the wrong type.
      • toNode

        public final Node toNode()
        Description copied from interface: ToNode
        Converts a value to a Node.
        Specified by:
        toNode in interface ToNode
        Returns:
        Returns the creates Node.
      • withDeepSortedKeys

        public final Node withDeepSortedKeys()
        Returns a node with sorted keys and sorted keys of all nested object nodes.
        Returns:
        Returns the node in which all object nodes have sorted keys.
      • withDeepSortedKeys

        public final Node withDeepSortedKeys​(java.util.Comparator<StringNode> keyComparator)
        Returns a node with sorted keys and sorted keys of all nested object nodes using a custom comparator.
        Parameters:
        keyComparator - Compares keys.
        Returns:
        Returns the node in which all object nodes have sorted keys.