Package uk.blankaspect.common.json
Class JsonUtils
java.lang.Object
uk.blankaspect.common.json.JsonUtils
This class contains utility methods that relate to JSON.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
containsText
(Reader inputStream, CharSequence target) Returnstrue
if the specified sequence of characters is found in the specified character stream.static boolean
containsText
(Path file, CharSequence target) Returnstrue
if the specified sequence of characters is found in the specified file, which is expected to contain text with the UTF-8 character encoding.static boolean
Returnstrue
if the specified node corresponds to a compound JSON value (ie, an array or object).static boolean
isJsonContainer
(AbstractNode node) Deprecated.static boolean
isJsonNumber
(AbstractNode node) Returnstrue
if the specified node corresponds to a JSON number.static boolean
isJsonValue
(AbstractNode node) Returnstrue
if the specified node corresponds to a JSON value.static boolean
Returnstrue
if the specified node corresponds to a simple JSON value (ie, a null, a Boolean, a number or a string).static AbstractNode
Opens a character stream on the specified file, which is expected to contain text with the UTF-8 character encoding, parses the character stream as JSON text and returns the resulting JSON value.static Path
tempLocation
(Path location) Returns a temporary file-system location that has the same parent directory as the specified location.static void
writeFile
(Path file, AbstractNode value) Writes the specified JSON value to the specified file as JSON text.static void
writeFile
(Path file, AbstractNode value, JsonGenerator generator) Writes the specified JSON value to the specified file as JSON text that is generated by the specified instance ofJsonGenerator
.static void
Writes the specified text to the specified file with the UTF-8 character encoding.
-
Field Details
-
TEMPORARY_FILENAME_EXTENSION
The filename extension of a temporary file.- See Also:
-
-
Method Details
-
isJsonValue
Returnstrue
if the specified node corresponds to a JSON value.- Parameters:
node
- the node of interest.- Returns:
true
ifnode
corresponds to a JSON value;false
otherwise.
-
isSimpleJsonValue
Returnstrue
if the specified node corresponds to a simple JSON value (ie, a null, a Boolean, a number or a string).- Parameters:
node
- the node of interest.- Returns:
true
ifnode
corresponds to a JSON null, Boolean, number or string;false
otherwise.
-
isCompoundJsonValue
Returnstrue
if the specified node corresponds to a compound JSON value (ie, an array or object).- Parameters:
node
- the node of interest.- Returns:
true
ifnode
corresponds to a JSON array or object;false
otherwise.
-
isJsonNumber
Returnstrue
if the specified node corresponds to a JSON number.- Parameters:
node
- the node of interest.- Returns:
true
ifnode
corresponds to a JSON number;false
otherwise.
-
isJsonContainer
Deprecated.This method has been replaced byisCompoundJsonValue(uk.blankaspect.common.basictree.AbstractNode)
and will eventually be removed.Returnstrue
if the specified node corresponds to a JSON container (ie, an array or object).- Parameters:
node
- the node of interest.- Returns:
true
ifnode
corresponds to a JSON array or object;false
otherwise.
-
containsText
Returnstrue
if the specified sequence of characters is found in the specified file, which is expected to contain text with the UTF-8 character encoding.- Parameters:
file
- the file that will be searched.target
- the sequence of characters that will be searched for infile
.- Returns:
true
iffile
containstarget
.- Throws:
IOException
- if an error occurs when reading the file.
-
containsText
Returnstrue
if the specified sequence of characters is found in the specified character stream.- Parameters:
inputStream
- the character stream that will be searched.target
- the sequence of characters that will be searched for ininputSteam
.- Returns:
true
iftarget
is found ininputStream
.- Throws:
IOException
- if an error occurs when reading from the input stream.
-
readFile
Opens a character stream on the specified file, which is expected to contain text with the UTF-8 character encoding, parses the character stream as JSON text and returns the resulting JSON value.- Parameters:
file
- the file whose content will be parsed as JSON text.- Returns:
- the JSON value that results from parsing the content of
file
, if the file contains valid JSON text. - Throws:
IOException
- if an error occurs when reading the file.JsonParser.ParseException
- if an error occurs when parsing the content of the file.
-
writeFile
Writes the specified JSON value to the specified file as JSON text. The text is generated by a new instance ofJsonGenerator
that has default values for the mode, the opening bracket on the same line flag, the indent increment and the maximum line length. The file is written to a new file in the parent directory of the specified file, and the new file is then renamed to the specified file.- Parameters:
file
- the file to which the JSON text ofvalue
will be written.value
- the JSON value whose JSON text will be written tofile
.- Throws:
IOException
- if an error occurs when writing the file.
-
writeFile
public static void writeFile(Path file, AbstractNode value, JsonGenerator generator) throws IOException Writes the specified JSON value to the specified file as JSON text that is generated by the specified instance ofJsonGenerator
. The file is written to a new file in the parent directory of the specified file, and the new file is then renamed to the specified file.- Parameters:
file
- the file to which the JSON text ofvalue
will be written.value
- the JSON value whose JSON text will be written tofile
.generator
- the object that will generate the JSON text forvalue
.- Throws:
IOException
- if an error occurs when writing the file.
-
writeText
Writes the specified text to the specified file with the UTF-8 character encoding. The file is written to a new file in the parent directory of the specified file, and the new file is then renamed to the specified file.- Parameters:
file
- the file to whichtext
will be written.text
- the text that will be written tofile
.- Throws:
IOException
- if an error occurs when reading permissions of an existing file or when writing the file.
-
tempLocation
Returns a temporary file-system location that has the same parent directory as the specified location.- Parameters:
location
- the file-system location for which a temporary location is sought.- Returns:
- a temporary file-system location that has the same parent directory as
location
.
-
isCompoundJsonValue(uk.blankaspect.common.basictree.AbstractNode)
and will eventually be removed.