Class JsonMethods


  • public final class JsonMethods
    extends Object
    Provides one-liners for creating Gloop objects from JSON text and vice versa.
    • Constructor Detail

      • JsonMethods

        JsonMethods()
    • Method Detail

      • gloopObjectToJsonString

        @GloopParameter(name="gloopJson")
        public static String gloopObjectToJsonString​(GloopObject gloopObject)
        Marshal a Gloop object to a JSON string.
        Parameters:
        gloopObject - the Gloop object to marshall
        Returns:
        a JSON representation of the provided Gloop object
      • gloopObjectToJsonString

        public static String gloopObjectToJsonString​(@GloopParameter(allowNull=false)
                                                     GloopObject gloopObject,
                                                     @GloopParameter(defaultValue="false")
                                                     boolean includeRoot,
                                                     @GloopParameter(defaultValue="false")
                                                     boolean includeNulls)
        Marshal a Gloop object to a JSON string.
        Parameters:
        gloopObject - the Gloop object to marshall
        includeRoot - if gloopObject is a model, this flag is used to determine whether or not to include the name of the Gloop model in the root node of the JSON document
        includeNulls - if true, null properties will be marshalled
        Returns:
        a JSON representation of the provided Gloop object
      • jsonStringToGloopObject

        public static GloopObject jsonStringToGloopObject​(String jsonString)
        Unmarshal a JSON string into a Gloop object.
        Parameters:
        jsonString - the JSON string to unmarshal
        Returns:
        a Gloop object equivalent of the provided JSON string
      • inputStreamToGloopObject

        public static GloopObject inputStreamToGloopObject​(InputStream jsonInputStream)
        Unmarshal a stream containing JSON data to a Gloop object.
        Parameters:
        jsonInputStream - the stream containing the JSON string to unmarshal
        Returns:
        a Gloop object equivalent of the provided JSON string
      • readerToGloopObject

        public static GloopObject readerToGloopObject​(Reader jsonReader)
        Unmarshal a reader containing JSON data to a Gloop object.
        Parameters:
        jsonReader - the reader containing the JSON string to unmarshal
        Returns:
        a Gloop object equivalent of the provided JSON string
      • writeJson

        public static void writeJson​(@GloopParameter(allowNull=false)
                                     Writer writer,
                                     @GloopParameter(allowNull=false)
                                     GloopObject gloopObject,
                                     @GloopParameter(defaultValue="false")
                                     boolean includeNulls,
                                     @GloopParameter(defaultValue="false")
                                     boolean includeRoot,
                                     @GloopParameter(defaultValue="true")
                                     boolean pretty)
        Marshal a Gloop object to a writer.
        Parameters:
        writer - the writer where data will be written to
        gloopObject - the Gloop object to marshal
        includeNulls - if true, null properties will be marshalled
        includeRoot - if gloopObject is a model, this flag is used to determine whether or not to include the name of the Gloop model in the root node of the JSON document
        pretty - flag determining whether or not to write the JSON with indentation and new lines to make it easier to read
      • printJson

        public static void printJson​(@GloopParameter(allowNull=false)
                                     IndentPrinter printer,
                                     @GloopParameter(allowNull=false)
                                     GloopObject gloopObject,
                                     @GloopParameter(defaultValue="false")
                                     boolean includeNulls,
                                     @GloopParameter(defaultValue="false")
                                     boolean includeRoot,
                                     @GloopParameter(defaultValue="true")
                                     boolean pretty)
        Marshal a Gloop object to a printer.
        Parameters:
        printer - the printer where data will be written to
        gloopObject - the Gloop object to marshal
        includeNulls - if true, null properties will be marshalled
        includeRoot - if gloopObject is a model, this flag is used to determine whether or not to include the name of the Gloop model in the root node of the JSON document
        pretty - flag determining whether or not to write the JSON with indentation and new lines to make it easier to read
      • getInputCursorFromFile

        @GloopObjectParameter("gloopJsonCursor{\n  gloopJsonCursor[]{\n    nodeName\n    cursorRecord:object\n }\n}")
        public static GloopCursor getInputCursorFromFile​(String filename,
                                                         @GloopParameter(defaultValue="UTF-8")
                                                         String encoding,
                                                         List<String> jsonNodeNames,
                                                         GloopExecutionContext context)
                                                  throws IOException

        Parse a large JSON file.

        Use the jsonNodeNames argument to specify which nodes you want to extract from the JSON file. Use $jsonRoot for root arrays.

        This one-liner will return a Gloop cursor. Every record in the cursor will point to a JSON object that has the same name as any of the properties specified via jsonNodeNames.

        Parameters:
        filename - the name of the file containing the JSON data
        encoding - the encoding used by the JSON data; default value is UTF-8
        jsonNodeNames - the names of the JSON objects to seek to whilst parsing the data
        context - the Gloop execution context (automatically mapped by Gloop)
        Returns:
        a cursor that can be used to iterate over all the records in the provided JSON data, whose names match the names provided in jsonNodeNames
        Throws:
        IOException
      • getInputCursorFromInputStream

        @GloopObjectParameter("gloopJsonCursor{\n  gloopJsonCursor[]{\n    nodeName\n   cursorRecord:object\n }\n}")
        public static GloopCursor getInputCursorFromInputStream​(InputStream inputStream,
                                                                @GloopParameter(defaultValue="UTF-8")
                                                                String encoding,
                                                                List<String> jsonNodeNames,
                                                                GloopExecutionContext context)
                                                         throws IOException

        Parse a large JSON stream.

        Use the jsonNodeNames argument to specify which nodes you want to extract from the JSON file. Use $jsonRoot for root arrays.

        This one-liner will return a Gloop cursor. Every record in the cursor will point to a JSON object that has the same name as any of the properties specified via jsonNodeNames.

        Parameters:
        inputStream - the stream containing the JSON data
        encoding - the encoding used by the JSON data; default value is UTF-8
        jsonNodeNames - the names of the JSON objects to seek to whilst parsing the data
        context - the Gloop execution context (automatically mapped by Gloop)
        Returns:
        a cursor that can be used to iterate over all the records in the provided JSON data, whose names match the names provided in jsonNodeNames
        Throws:
        IOException
      • openJsonOutputCursor

        @GloopObjectParameter("output{\n    jsonCursorOutput{\n        indentPrinter:object:printer used to write the json out to::::groovy.util.IndentPrinter\n        jsonOutputCursor[]*{\n        }\n    }\n}")
        public static GloopModel openJsonOutputCursor​(@GloopParameter(defaultValue="true")
                                                      boolean prettyPrint,
                                                      @GloopParameter(defaultValue="true")
                                                      boolean printStartDocumentBrace,
                                                      @GloopParameter(defaultValue="true")
                                                      boolean printCommaAtStart,
                                                      @GloopParameter(defaultValue="true")
                                                      boolean printStartArrayBracket,
                                                      @GloopParameter(defaultValue="true")
                                                      boolean printEndArrayBracket,
                                                      @GloopParameter(defaultValue="true")
                                                      boolean printEndDocumentBrace,
                                                      String name,
                                                      @GloopParameter(allowNull=false,defaultValue="    ")
                                                      String indent,
                                                      @GloopParameter(defaultValue="true")
                                                      boolean closeDestination,
                                                      Object destination,
                                                      GloopExecutionContext context)
        Open a cursor for writing large amounts of JSON data.
        Parameters:
        prettyPrint - whether or not to write the JSON with indentation and new lines to make it easier to read
        printStartDocumentBrace - whether or not to print a start document character { before outputting any cursor data
        printCommaAtStart - whether or not to print comma before outputting any cursor data
        printStartArrayBracket - whether or not to print an open array character [ before outputting any cursor data
        printEndArrayBracket - whether or not to print an end array character ] after outputting the cursor data
        printEndDocumentBrace - whether or not to print an end document character } after outputting the cursor data
        name - the name to print before declaring the array (null if there is no name)
        indent - the number of spaces used for indentation if prettyPrint is true; the default is 4 spaces
        closeDestination - set to true if the cursor will close the underlying destination after printing closing text
        destination - where to write the data to; must be a StringBuffer, Writer, OutputStream, Path, or File
        context - the Gloop execution context (automatically mapped by Gloop)
        Returns:
        an output cursor where objects can be added to