Class JSONConverter

java.lang.Object
com.github.tadukoo.parsing.json.JSONConverter
All Implemented Interfaces:
CommonPatterns

public class JSONConverter extends Object implements CommonPatterns
A class able to parse JSON into JSON Objects (either a class or array), and to convert classes or objects into JSON strings.
Since:
Alpha v.0.1
Version:
Alpha v.0.3.1
Author:
Logan Ferree (Tadukoo)
  • Field Details

    • arrayStartChar

      public static final char arrayStartChar
      Character at the start of an array - opening bracket - [
      See Also:
    • arrayEndChar

      public static final char arrayEndChar
      Character at the end of an array - closing bracket - ]
      See Also:
    • classStartChar

      public static final char classStartChar
      Character at the start of a class - opening brace - {
      See Also:
    • classEndChar

      public static final char classEndChar
      Character at the end of a class - closing brace - }
      See Also:
    • keyEndChar

      public static final char keyEndChar
      Character that appears between a key and value - colon - :
      See Also:
    • nextValueChar

      public static final char nextValueChar
      Character used to signify there's another value after it - comma - ,
      See Also:
    • whitespaceChars

      private static final List<Character> whitespaceChars
      List of whitespace characters to be ignored - space, tab (\t), form feed (\f), carriage return (\r), and newline (\n)
    • nullFormatMatcher

      private Matcher nullFormatMatcher
    • booleanFormatMatcher

      private Matcher booleanFormatMatcher
    • numberFormatMatcher

      private Matcher numberFormatMatcher
    • stringFormatMatcher

      private Matcher stringFormatMatcher
  • Constructor Details

    • JSONConverter

      public JSONConverter()
  • Method Details

    • parseJSON

      public JSONObject parseJSON(String JSONString)
      Parses the given string into a JSON object (either an array or a class), and returns it.
      Parameters:
      JSONString - The string to be parsed
      Returns:
      A JSONObject (either a JSONClass or JSONArray)
    • parseJSONFromFile

      public JSONObject parseJSONFromFile(String filepath) throws IOException
      Reads the file at the given filepath and parses it into a JSON object (either an array or a class), and returns it.
      Parameters:
      filepath - The path to the file to be read
      Returns:
      A JSONObject (either a JSONClass or JSONArray)
      Throws:
      IOException - if something goes wrong in reading the file
    • parseJSONFromFile

      public JSONObject parseJSONFromFile(File file) throws IOException
      Reads the file and parses it into a JSON object (either an array or a class), and returns it.
      Parameters:
      file - The File to be read
      Returns:
      A JSONObject (either a JSONClass or JSONArray)
      Throws:
      IOException - if something goes wrong in reading the file
    • parseJSONArray

      private com.github.tadukoo.util.tuple.Pair<JSONArray<Object>,Integer> parseJSONArray(String JSONString, int startIndex)
      Parses a JSON array, where startIndex is the index of the character AFTER the opening bracket - [
      Parameters:
      JSONString - The JSON string to be parsed
      startIndex - The index of the character AFTER the opening bracket - [
      Returns:
      The parsed JSONArray, and the index of the first character after the closing bracket
    • parseJSONClass

      private com.github.tadukoo.util.tuple.Pair<JSONClass,Integer> parseJSONClass(String JSONString, int startIndex)
      Parses a JSON class, where startIndex is the index of the character AFTER the opening brace - {
      Parameters:
      JSONString - The JSON string to be parsed
      startIndex - The index of the character AFTER the opening brace - {
      Returns:
      The parsed JSONClass, and the index of the first character after the closing brace
    • parseValue

      private com.github.tadukoo.util.tuple.Pair<Object,Integer> parseValue(String JSONString, int startIndex)
      Method used to parse a value in a JSON object.
      Parameters:
      JSONString - The JSON string being parsed
      startIndex - The start index of the value
      Returns:
      The parsed Object value and the new character index after the value is over
    • skipWhitespace

      private int skipWhitespace(String JSONString, int startIndex)
      Advances the index past any whitespace characters.
      Parameters:
      JSONString - The String to look at
      startIndex - The current index
      Returns:
      The index after all the whitespace is ignored
    • matchAtStartOrError

      private String matchAtStartOrError(Matcher matcher, int index)
      Ensures that a match is found starting at the given index, and returns the match (on group 1, for cases of excluding surrounding info, such as quotes on a String). If there's no match, or the match doesn't start at the given index, an IllegalStateException is thrown
      Parameters:
      matcher - The Matcher to use in matching
      index - The index the match should start at
      Returns:
      The matched value (group 1)
    • convertToJSON

      public String convertToJSON(Object obj)
      Converts the given object into the proper form for use in JSON.
      If it's a JSONObject, then we just call JSONObject.convertToJSON(JSONConverter) on it. null, true, false, strings, and numbers are handled as JSON expects.
      If it doesn't fall into one of those categories, an error is thrown.
      Parameters:
      obj - The object to convert to a JSON string
      Returns:
      The JSON string representing the given object.
    • saveJSONFile

      public void saveJSONFile(String filepath, Object obj) throws IOException
      Converts the given object to JSON and saves it to a file at the given filepath.
      Parameters:
      filepath - The path of the file to save the JSON to
      obj - The object to be converted to JSON
      Throws:
      IOException - If anything goes wrong in writing the file