Package io.toro.martini
Class JsonMethods
- java.lang.Object
-
- io.toro.martini.JsonMethods
-
public final class JsonMethods extends Object
Provides one-liners for creating Gloop objects from JSON text and vice versa.
-
-
Constructor Summary
Constructors Constructor Description JsonMethods()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static GloopCursorgetInputCursorFromFile(String filename, String encoding, List<String> jsonNodeNames, GloopExecutionContext context)Parse a large JSON file.static GloopCursorgetInputCursorFromInputStream(InputStream inputStream, String encoding, List<String> jsonNodeNames, GloopExecutionContext context)Parse a large JSON stream.static StringgloopObjectToJsonString(GloopObject gloopObject)Marshal a Gloop object to a JSON string.static StringgloopObjectToJsonString(GloopObject gloopObject, boolean includeRoot, boolean includeNulls)Marshal a Gloop object to a JSON string.static GloopObjectinputStreamToGloopObject(InputStream jsonInputStream)Unmarshal a stream containing JSON data to a Gloop object.static GloopObjectjsonStringToGloopObject(String jsonString)Unmarshal a JSON string into a Gloop object.static GloopModelopenJsonOutputCursor(boolean prettyPrint, boolean printStartDocumentBrace, boolean printCommaAtStart, boolean printStartArrayBracket, boolean printEndArrayBracket, boolean printEndDocumentBrace, String name, String indent, boolean closeDestination, Object destination, GloopExecutionContext context)Open a cursor for writing large amounts of JSON data.static voidprintJson(IndentPrinter printer, GloopObject gloopObject, boolean includeNulls, boolean includeRoot, boolean pretty)Marshal a Gloop object to a printer.static GloopObjectreaderToGloopObject(Reader jsonReader)Unmarshal a reader containing JSON data to a Gloop object.static voidwriteJson(Writer writer, GloopObject gloopObject, boolean includeNulls, boolean includeRoot, boolean pretty)Marshal a Gloop object to a writer.
-
-
-
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 marshallincludeRoot- ifgloopObjectis 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 documentincludeNulls- iftrue,nullproperties 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 togloopObject- the Gloop object to marshalincludeNulls- iftrue,nullproperties will be marshalledincludeRoot- ifgloopObjectis 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 documentpretty- 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 togloopObject- the Gloop object to marshalincludeNulls- iftrue,nullproperties will be marshalledincludeRoot- ifgloopObjectis 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 documentpretty- 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
jsonNodeNamesargument 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 dataencoding- the encoding used by the JSON data; default value is UTF-8jsonNodeNames- the names of the JSON objects to seek to whilst parsing the datacontext- 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
jsonNodeNamesargument 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 dataencoding- the encoding used by the JSON data; default value is UTF-8jsonNodeNames- the names of the JSON objects to seek to whilst parsing the datacontext- 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 readprintStartDocumentBrace- whether or not to print a start document character{before outputting any cursor dataprintCommaAtStart- whether or not to print comma before outputting any cursor dataprintStartArrayBracket- whether or not to print an open array character [ before outputting any cursor dataprintEndArrayBracket- whether or not to print an end array character ] after outputting the cursor dataprintEndDocumentBrace- whether or not to print an end document character}after outputting the cursor dataname- the name to print before declaring the array (nullif there is no name)indent- the number of spaces used for indentation ifprettyPrintis true; the default is 4 spacescloseDestination- set totrueif the cursor will close the underlying destination after printing closing textdestination- where to write the data to; must be aStringBuffer,Writer,OutputStream,Path, orFilecontext- the Gloop execution context (automatically mapped by Gloop)- Returns:
- an output cursor where objects can be added to
-
-