Class JSONToToken

  • All Implemented Interfaces:
    java.lang.Cloneable, Actor, Executable, FiringsRecordable, Initializable, TypedActor, Changeable, Debuggable, DebugListener, Derivable, Instantiable, ModelErrorHandler, MoMLExportable, Moveable, Nameable

    public class JSONToToken
    extends Converter
    An actor that converts a string containing JSON-formatted data into a Token.

    Depending on the top level structure found in the JSON string, it produces either a RecordToken or an ArrayToken on its output port. Nested structures in the JSON data will translate to correspondingly nested structures in the Token.

    The JSONObject parser processes values as follows: Delimited values are always parsed as a String. Values that are not delimited are tested in the order noted below. The first test that succeeds determines the type.

    • 'true' | 'false' => Boolean (case insensitive)
    • 'null' => JSONObject.NULL (case insensitive)
    • '0x..' => Integer (hexadecimal)
    • x'.'y | exponent encoded => Double
    • x => Long, or Integer if value remains the same after conversion

    If non of the above apply, the value is interpreted as a String.

    Note that JSON allows array elements to have different types, whereas the ArrayToken does not. Conversion of such mixed array will result in an ArrayToken of which the types of all elements are cast to the least upper bound of the entire collection.

    http://www.json.org/ - a description of the JSON format.

    Since:
    Ptolemy II 10.0
    Version:
    $Id$
    Author:
    Marten Lohstroh and Edward A. Lee, Contributor: Beth Latronico
    See Also:
    TokenToJSON
    Pt.AcceptedRating:
    Red (chx)
    Pt.ProposedRating:
    Yellow (marten)
    • Method Detail

      • parseJSON

        public static Token parseJSON​(java.lang.String input)
                               throws IllegalActionException
        Parse the input string and return a token representation of the data. A JSON object is converted to a RecordToken, a JSON array to an ArrayToken, a string to a StringToken, "true" and "false" to BooleanToken, and an empty string, "nil", or "null" to a nil token.
        Parameters:
        input - An input string that contains JSON-formatted data
        Returns:
        A Token that represents the JSON-formatted input string
        Throws:
        IllegalActionException - If the given input string cannot be parsed.