Class JsonUtils

java.lang.Object
uk.blankaspect.common.json.JsonUtils

public class JsonUtils extends Object
This class contains utility methods that relate to JSON.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The filename extension of a temporary file.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    containsText(Reader inputStream, CharSequence target)
    Returns true if the specified sequence of characters is found in the specified character stream.
    static boolean
    Returns true 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
    Returns true if the specified node represents a JSON container (ie, an array or object).
    static boolean
    Returns true if the specified node represents a JSON value.
    static boolean
    Returns true if the specified node represents a simple JSON value (ie, a null, a Boolean, a number or a string).
    readFile(Path file)
    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 of JsonGenerator.
    static void
    writeText(Path file, String text)
    Writes the specified text to the specified file with the UTF-8 character encoding.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • TEMPORARY_FILENAME_EXTENSION

      public static final String TEMPORARY_FILENAME_EXTENSION
      The filename extension of a temporary file.
      See Also:
  • Method Details

    • isJsonValue

      public static boolean isJsonValue(AbstractNode node)
      Returns true if the specified node represents a JSON value.
      Parameters:
      node - the node of interest.
      Returns:
      true if node represents a JSON value; false otherwise.
    • isSimpleJsonValue

      public static boolean isSimpleJsonValue(AbstractNode node)
      Returns true if the specified node represents a simple JSON value (ie, a null, a Boolean, a number or a string).
      Parameters:
      node - the node of interest.
      Returns:
      true if node represents a JSON null, Boolean, number or string; false otherwise.
    • isJsonContainer

      public static boolean isJsonContainer(AbstractNode node)
      Returns true if the specified node represents a JSON container (ie, an array or object).
      Parameters:
      node - the node of interest.
      Returns:
      true if node represents a JSON array or object; false otherwise.
    • containsText

      public static boolean containsText(Path file, CharSequence target) throws IOException
      Returns true 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 in file.
      Returns:
      true if file contains target.
      Throws:
      IOException - if an error occurred when reading the file.
    • containsText

      public static boolean containsText(Reader inputStream, CharSequence target) throws IOException
      Returns true 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 in inputSteam.
      Returns:
      true if target is found in inputStream.
      Throws:
      IOException - if an error occurred when reading from the input stream.
    • readFile

      public static AbstractNode readFile(Path file) throws IOException, JsonParser.ParseException
      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 occurred when reading the file.
      JsonParser.ParseException - if an error occurred when parsing the content of the file.
    • writeFile

      public static void writeFile(Path file, AbstractNode value) throws IOException
      Writes the specified JSON value to the specified file as JSON text. The text is generated by a new instance of JsonGenerator 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 of value will be written.
      value - the JSON value whose JSON text will be written to file.
      Throws:
      IOException - if an error occurred 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 of JsonGenerator. 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 of value will be written.
      value - the JSON value whose JSON text will be written to file.
      generator - the object that will generate the JSON text for value.
      Throws:
      IOException - if an error occurred when writing the file.
    • writeText

      public static void writeText(Path file, String text) throws IOException
      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 which text will be written.
      text - the text that will be written to file.
      Throws:
      IOException - if an error occurred when reading permissions of an existing file or when writing the file.
    • tempLocation

      public static Path tempLocation(Path location)
      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.