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 booleancontainsText(Reader inputStream, CharSequence target) Returnstrueif the specified sequence of characters is found in the specified character stream.static booleancontainsText(Path file, CharSequence target) Returnstrueif the specified sequence of characters is found in the specified file, which is expected to contain text with the UTF-8 character encoding.static booleanReturnstrueif the specified node corresponds to a compound JSON value (ie, an array or object).static booleanisJsonContainer(AbstractNode node) Deprecated.static booleanisJsonNumber(AbstractNode node) Returnstrueif the specified node corresponds to a JSON number.static booleanisJsonValue(AbstractNode node) Returnstrueif the specified node corresponds to a JSON value.static booleanReturnstrueif the specified node corresponds to a simple JSON value (ie, a null, a Boolean, a number or a string).static AbstractNodeOpens 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 PathtempLocation(Path location) Returns a temporary file-system location that has the same parent directory as the specified location.static voidwriteFile(Path file, AbstractNode value) Writes the specified JSON value to the specified file as JSON text.static voidwriteFile(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 voidWrites 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
Returnstrueif the specified node corresponds to a JSON value.- Parameters:
node- the node of interest.- Returns:
trueifnodecorresponds to a JSON value;falseotherwise.
-
isSimpleJsonValue
Returnstrueif 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:
trueifnodecorresponds to a JSON null, Boolean, number or string;falseotherwise.
-
isCompoundJsonValue
Returnstrueif the specified node corresponds to a compound JSON value (ie, an array or object).- Parameters:
node- the node of interest.- Returns:
trueifnodecorresponds to a JSON array or object;falseotherwise.
-
isJsonNumber
Returnstrueif the specified node corresponds to a JSON number.- Parameters:
node- the node of interest.- Returns:
trueifnodecorresponds to a JSON number;falseotherwise.
-
isJsonContainer
Deprecated.This method has been replaced byisCompoundJsonValue(uk.blankaspect.common.basictree.AbstractNode)and will eventually be removed.Returnstrueif the specified node corresponds to a JSON container (ie, an array or object).- Parameters:
node- the node of interest.- Returns:
trueifnodecorresponds to a JSON array or object;falseotherwise.
-
containsText
Returnstrueif 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:
trueiffilecontainstarget.- Throws:
IOException- if an error occurs when reading the file.
-
containsText
Returnstrueif 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:
trueiftargetis 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 ofJsonGeneratorthat 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 ofvaluewill 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 ofvaluewill 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 whichtextwill 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.