Skip to content

Commit f1dfc34

Browse files
committed
Remove warnings in JSONParser.kt
1. Mostly added @Suppress("UNCHECKED_CAST") to different casts as they will never fail. (They will never fail as the stack, on which it is based on, has a specific structure and it is being built by the same function that uses it in the background. This means that even though it wasn't explicitly stated (for the compiler) that this cast can be done, it is being implied by the structure of the stack itself) 2. Changing the variable type of the stack from a var to a val (and with that a constant) as the reference that it contains is never being changed. 3. Fix a spelling mistake from "Expeced" to "Expected".
1 parent aa057d2 commit f1dfc34

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/main/kotlin/JSONParser.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fun jsonParse(input: String): Any? {
1919

2020
var token: JSONLexeme
2121

22-
var stack = Stack<Any?>()
22+
val stack = Stack<Any?>()
2323

2424
while (true) {
2525
token = lexer.getNext()
@@ -64,17 +64,20 @@ fun jsonParse(input: String): Any? {
6464
if (stack.peek() is String) {
6565
val name = stack.pop() as String
6666

67+
@Suppress("UNCHECKED_CAST")
6768
(stack.peek() as HashMap<String, Any?>)[name] = jsonObject
6869
continue
6970
}
7071

7172
// Should now be an array
73+
@Suppress("UNCHECKED_CAST")
7274
(stack.peek() as ArrayList<Any?>).add(jsonObject)
7375

7476
state = State.ARRAY
7577
continue
7678
}
7779

80+
@Suppress("UNCHECKED_CAST")
7881
val map = stack.peek() as HashMap<String, Any?>
7982

8083
if(map.isNotEmpty()) {
@@ -94,7 +97,7 @@ fun jsonParse(input: String): Any? {
9497
token = lexer.getNext()
9598

9699
if (token.type != COLON) {
97-
throw JSONParserException("Expeced ':' (COLON), but got ${token.type}") // TODO: Better error reporting
100+
throw JSONParserException("Expected ':' (COLON), but got ${token.type}") // TODO: Better error reporting
98101
}
99102

100103
token = lexer.getNext()
@@ -134,17 +137,20 @@ fun jsonParse(input: String): Any? {
134137
if (stack.peek() is String) {
135138
val name = stack.pop() as String
136139

140+
@Suppress("UNCHECKED_CAST")
137141
(stack.peek() as HashMap<String, Any?>)[name] = array
138142

139143
state = State.OBJECT
140144
continue
141145
}
142146

143147
// Should now be an array
148+
@Suppress("UNCHECKED_CAST")
144149
(stack.peek() as ArrayList<Any?>).add(array)
145150
continue
146151
}
147152

153+
@Suppress("UNCHECKED_CAST")
148154
val array = stack.peek() as ArrayList<Any?>
149155

150156
if (array.isNotEmpty()) {

0 commit comments

Comments
 (0)