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 GloopCursor
getInputCursorFromFile(String filename, String encoding, List<String> jsonNodeNames, GloopExecutionContext context)
Parse a large JSON file.static GloopCursor
getInputCursorFromInputStream(InputStream inputStream, String encoding, List<String> jsonNodeNames, GloopExecutionContext context)
Parse a large JSON stream.static String
gloopObjectToJsonString(GloopObject gloopObject)
Marshal a Gloop object to a JSON string.static String
gloopObjectToJsonString(GloopObject gloopObject, boolean includeRoot, boolean includeNulls)
Marshal a Gloop object to a JSON string.static GloopObject
inputStreamToGloopObject(InputStream jsonInputStream)
Unmarshal a stream containing JSON data to a Gloop object.static GloopObject
jsonStringToGloopObject(String jsonString)
Unmarshal a JSON string into a Gloop object.static GloopModel
openJsonOutputCursor(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 void
printJson(IndentPrinter printer, GloopObject gloopObject, boolean includeNulls, boolean includeRoot, boolean pretty)
Marshal a Gloop object to a printer.static GloopObject
readerToGloopObject(Reader jsonReader)
Unmarshal a reader containing JSON data to a Gloop object.static void
writeJson(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
- ifgloopObject
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 documentincludeNulls
- iftrue
,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 togloopObject
- the Gloop object to marshalincludeNulls
- iftrue
,null
properties will be marshalledincludeRoot
- ifgloopObject
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 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
,null
properties will be marshalledincludeRoot
- ifgloopObject
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 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
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 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
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 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 (null
if there is no name)indent
- the number of spaces used for indentation ifprettyPrint
is true; the default is 4 spacescloseDestination
- set totrue
if the cursor will close the underlying destination after printing closing textdestination
- where to write the data to; must be aStringBuffer
,Writer
,OutputStream
,Path
, orFile
context
- the Gloop execution context (automatically mapped by Gloop)- Returns:
- an output cursor where objects can be added to
-
-