Skip to content

Commit ce3dc95

Browse files
committed
Add KDoc documentation to the JSONParser
1 parent 899043b commit ce3dc95

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/main/kotlin/JSONParser.kt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,33 @@ private enum class State {
1414
ARRAY,
1515
}
1616

17+
/**
18+
* Parses JSON tokens from a [JSONLexer] into a Kotlin Object.
19+
* The JSON will be parsed according to:
20+
* [RFC 8259 - The JavaScript Object Notation (JSON) Data Interchange Format](https://www.rfc-editor.org/rfc/rfc8259).
21+
*
22+
* ## Resulting value
23+
* The resulting value can be (depending on the JSON input) recursively:
24+
* - Boolean
25+
* - Null
26+
* - Number (as a [BigDecimal])
27+
* - String
28+
* - Array (as an [ArrayList] with [Any])
29+
* - Object (as a [HashMap] with [String] to [Any])
30+
*
31+
* This value is a single object and can be used however wanted without any other abstractions.
32+
*
33+
* ## Error handling
34+
* If an error happens at the parsing stage, a [JSONParserException] is being thrown.
35+
*
36+
* If the lexer is experiencing an error the [JSONLexerException] will be thrown by the lexer
37+
* and with that this function as well.
38+
*
39+
* Both exception types are based upon the [JSONParseException] and with that can both be caught at the same time.
40+
*
41+
* @param lexer The lexer which is being used for the tokens
42+
* @return The resulting JSON value
43+
*/
1744
@Throws(JSONParseException::class)
1845
fun jsonParse(lexer: JSONLexer): Any? {
1946
var state = State.START;
@@ -211,6 +238,36 @@ fun jsonParse(lexer: JSONLexer): Any? {
211238
return result
212239
}
213240

241+
/**
242+
* Parses a JSON string into a Kotlin Object.
243+
* The JSON will be parsed according to:
244+
* [RFC 8259 - The JavaScript Object Notation (JSON) Data Interchange Format](https://www.rfc-editor.org/rfc/rfc8259).
245+
*
246+
* This function is an abstraction of the [jsonParse] function and uses a [StringJSONLexer] which is initialized with
247+
* the `input` string and passed to the actual function.
248+
*
249+
* ## Resulting value
250+
* The resulting value can be (depending on the JSON input) recursively:
251+
* - Boolean
252+
* - Null
253+
* - Number (as a [BigDecimal])
254+
* - String
255+
* - Array (as an [ArrayList] with [Any])
256+
* - Object (as a [HashMap] with [String] to [Any])
257+
*
258+
* This value is a single object and can be used however wanted without any other abstractions.
259+
*
260+
* ## Error handling
261+
* If an error happens at the parsing stage, a [JSONParserException] is being thrown.
262+
*
263+
* If the lexer is experiencing an error the [JSONLexerException] will be thrown by the lexer
264+
* and with that this function as well.
265+
*
266+
* Both exception types are based upon the [JSONParseException] and with that can both be caught at the same time.
267+
*
268+
* @param input The input string which is being parsed
269+
* @return The resulting JSON value
270+
*/
214271
@Throws(JSONParseException::class)
215272
fun jsonParse(input: String): Any? {
216273
return jsonParse(StringJSONLexer(input))

0 commit comments

Comments
 (0)