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 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 corresponds to a JSON value.
      Parameters:
      node - the node of interest.
      Returns:
      true if node corresponds to a JSON value; false otherwise.
    • isSimpleJsonValue

      public static boolean isSimpleJsonValue(AbstractNode node)
      Returns true 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 if node corresponds to a JSON null, Boolean, number or string; false otherwise.
    • isCompoundJsonValue

      public static boolean isCompoundJsonValue(AbstractNode node)
      Returns true if the specified node corresponds to a compound JSON value (ie, an array or object).
      Parameters:
      node - the node of interest.
      Returns:
      true if node corresponds to a JSON array or object; false otherwise.
    • isJsonNumber

      public static boolean isJsonNumber(AbstractNode node)
      Returns true if the specified node corresponds to a JSON number.
      Parameters:
      node - the node of interest.
      Returns:
      true if node corresponds to a JSON number; false otherwise.
    • isJsonContainer

      @Deprecated public static boolean isJsonContainer(AbstractNode node)
      Deprecated.
      This method has been replaced by isCompoundJsonValue(uk.blankaspect.common.basictree.AbstractNode) and will eventually be removed.
      Returns true if the specified node corresponds to a JSON container (ie, an array or object).
      Parameters:
      node - the node of interest.
      Returns:
      true if node corresponds to 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 occurs 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 occurs 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 occurs when reading the file.
      JsonParser.ParseException - if an error occurs 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 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 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 occurs 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 occurs 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.