Package com.github.tadukoo.parsing.json
Class JSONConverter
java.lang.Object
com.github.tadukoo.parsing.json.JSONConverter
- All Implemented Interfaces:
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 Summary
Modifier and TypeFieldDescriptionstatic final char
Character at the end of an array - closing bracket - ]static final char
Character at the start of an array - opening bracket - [private Matcher
A Matcher forCommonPatterns.booleanFormat
static final char
Character at the end of a class - closing brace - }static final char
Character at the start of a class - opening brace - {static final char
Character that appears between a key and value - colon - :static final char
Character used to signify there's another value after it - comma - ,private Matcher
A Matcher forCommonPatterns.nullFormat
private Matcher
A Matcher forCommonPatterns.numberFormat
private Matcher
A Matcher forCommonPatterns.quotedStringFormat
List of whitespace characters to be ignored - space, tab (\t), form feed (\f), carriage return (\r), and newline (\n)Fields inherited from interface com.github.tadukoo.parsing.CommonPatterns
booleanFormat, nullableBooleanFormat, nullFormat, numberFormat, quotedStringFormat
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionconvertToJSON
(Object obj) Converts the given object into the proper form for use in JSON.
If it's aJSONObject
, then we just callJSONObject.convertToJSON(JSONConverter)
on it.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).Parses the given string into a JSON object (either an array or a class), and returns it.parseJSONArray
(String JSONString, int startIndex) Parses a JSON array, where startIndex is the index of the character AFTER the opening bracket - [parseJSONClass
(String JSONString, int startIndex) Parses a JSON class, where startIndex is the index of the character AFTER the opening brace - {parseJSONFromFile
(File file) Reads the file and parses it into a JSON object (either an array or a class), and returns it.parseJSONFromFile
(String filepath) Reads the file at the given filepath and parses it into a JSON object (either an array or a class), and returns it.parseValue
(String JSONString, int startIndex) Method used to parse a value in a JSON object.void
saveJSONFile
(String filepath, Object obj) Converts the given object to JSON and saves it to a file at the given filepath.private int
skipWhitespace
(String JSONString, int startIndex) Advances the index past any whitespace characters.
-
Field Details
-
arrayStartChar
public static final char arrayStartCharCharacter at the start of an array - opening bracket - [- See Also:
-
arrayEndChar
public static final char arrayEndCharCharacter at the end of an array - closing bracket - ]- See Also:
-
classStartChar
public static final char classStartCharCharacter at the start of a class - opening brace - {- See Also:
-
classEndChar
public static final char classEndCharCharacter at the end of a class - closing brace - }- See Also:
-
keyEndChar
public static final char keyEndCharCharacter that appears between a key and value - colon - :- See Also:
-
nextValueChar
public static final char nextValueCharCharacter used to signify there's another value after it - comma - ,- See Also:
-
whitespaceChars
List of whitespace characters to be ignored - space, tab (\t), form feed (\f), carriage return (\r), and newline (\n) -
nullFormatMatcher
A Matcher forCommonPatterns.nullFormat
-
booleanFormatMatcher
A Matcher forCommonPatterns.booleanFormat
-
numberFormatMatcher
A Matcher forCommonPatterns.numberFormat
-
stringFormatMatcher
A Matcher forCommonPatterns.quotedStringFormat
-
-
Constructor Details
-
JSONConverter
public JSONConverter()
-
-
Method Details
-
parseJSON
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
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
Reads the file and parses it into a JSON object (either an array or a class), and returns it.- Parameters:
file
- TheFile
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 parsedstartIndex
- 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 parsedstartIndex
- 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 parsedstartIndex
- The start index of the value- Returns:
- The parsed Object value and the new character index after the value is over
-
skipWhitespace
Advances the index past any whitespace characters.- Parameters:
JSONString
- The String to look atstartIndex
- The current index- Returns:
- The index after all the whitespace is ignored
-
matchAtStartOrError
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 matchingindex
- The index the match should start at- Returns:
- The matched value (group 1)
-
convertToJSON
Converts the given object into the proper form for use in JSON.
If it's aJSONObject
, then we just callJSONObject.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
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 toobj
- The object to be converted to JSON- Throws:
IOException
- If anything goes wrong in writing the file
-