3
3
import java .util .ArrayList ;
4
4
import java .util .Iterator ;
5
5
import java .util .List ;
6
- import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
7
6
8
7
/**
9
8
* A JSONObject is a custom data structure that represents a JSON object. It supports nested key-value pairs, arrays (as
@@ -57,6 +56,7 @@ public class JSONObject implements Iterable<Object> {
57
56
*/
58
57
private static boolean isValidJsonType (Object value ) {
59
58
return (
59
+ value == null ||
60
60
value instanceof String ||
61
61
value instanceof Number ||
62
62
value instanceof JSONObject ||
@@ -94,10 +94,8 @@ public JSONObject(String key) {
94
94
/**
95
95
* Create a {@code JSONObject} with the given key and value.
96
96
*
97
- * @param key
98
- * A {@code String} key
99
- * @param value
100
- * A valid JSON value
97
+ * @param key Valid {@code String} key for JSON Object
98
+ * @param value Valid JSON value ({@code String}, {@code Number}, {@code JSONObject}, {@code List<>}, {@code Boolean}, {@code Object extends Jsonic})
101
99
*/
102
100
public JSONObject (String key , Object value ) {
103
101
if (!isValidJsonType (value )) {
@@ -107,11 +105,12 @@ public JSONObject(String key, Object value) {
107
105
this .value = value ;
108
106
if (value == null )
109
107
type = null ;
110
- else if (value instanceof List <?>)
108
+ else if (value instanceof List <?>) {
111
109
if (isJSONList (getAsList ()))
112
110
type = JSONObject .class ;
113
111
else
114
112
type = ArrayList .class ;
113
+ }
115
114
else
116
115
type = value .getClass ();
117
116
}
@@ -144,46 +143,51 @@ public Object getValue() {
144
143
/**
145
144
* Return the value as the given class.
146
145
*
147
- * @param clazz
148
- * An existant class to cast this object's value into.
146
+ * @param clazz Existant class to cast this object's value into.
149
147
*
150
- * @return The value as a type of the given class, or null if uncastable.
148
+ * @return Value as a type of the given class.
149
+ * @throws ClassCastException if the value is not null and is not assignable to the type T.
151
150
*/
152
- public <T > T getValueAs (Class <T > clazz ) {
153
- try {
154
- return clazz .cast (value );
155
- } catch (ClassCastException e ) {
156
- e .printStackTrace ();
157
- return null ;
158
- }
151
+ public <T > T getValueAs (Class <T > clazz ) throws ClassCastException {
152
+ return clazz .cast (value );
159
153
}
160
154
155
+ /** Returns the value of this JSONObject as a String, or throws a ClassCastException if unable. */
161
156
public String getAsString () {
162
- return value instanceof String ? (String ) value : null ;
157
+ if (value instanceof String ) return (String ) value ;
158
+ else throw new ClassCastException ("Cannot cast non-String value to String." );
163
159
}
164
160
161
+ /** Returns the value of this JSONObject as a Number, or throws a ClassCastException if unable. */
165
162
public Number getAsNumber () {
166
- return value instanceof Number ? (Number ) value : null ;
163
+ if (value instanceof Number ) return (Number ) value ;
164
+ else throw new ClassCastException ("Cannot cast non-Number value to Number." );
167
165
}
168
166
169
- @ SuppressFBWarnings ( "NP_BOOLEAN_RETURN_NULL" )
167
+ /** Returns the value of this JSONObject as a Boolean, or throws a ClassCastException if unable. */
170
168
public Boolean getAsBoolean () {
171
- return value instanceof Boolean ? (Boolean ) value : null ;
169
+ if (value instanceof Boolean ) return (Boolean ) value ;
170
+ else throw new ClassCastException ("Cannot cast non-Boolean value to Boolean." );
172
171
}
173
172
173
+ /** Returns the value of this JSONObject as another JSONObject, or {@code null} if unable. */
174
174
public JSONObject getAsObject () {
175
175
176
- if ( value instanceof JSONObject ) return ( JSONObject ) value ;
176
+ return this ;
177
177
178
- if (value instanceof List <?> && type .equals (JSONObject .class )) {
179
- return this ;
180
- }
178
+ // if (value instanceof JSONObject) return (JSONObject) value;
181
179
182
- return null ;
180
+ // if (value instanceof List<?> && type.equals(JSONObject.class)) {
181
+ // return this;
182
+ // }
183
+
184
+ // return null;
183
185
}
184
186
187
+ /** Returns the value of this JSONObject as a List, or throws a ClassCastException if unable. */
185
188
public List <?> getAsList () {
186
- return value instanceof List <?> ? (List <?>) value : null ;
189
+ if (value instanceof List <?>) return (List <?>) value ;
190
+ else throw new ClassCastException ("Cannot cast non-List value to List." );
187
191
}
188
192
189
193
public void setValue (Object value ) {
@@ -226,10 +230,9 @@ public Class<? extends Object> getType() {
226
230
* Search the JSONObject tree structure for a JSONObject with the given key, and return the value of that
227
231
* JSONObject.
228
232
*
229
- * @param key
230
- * A String key to search for within the tree structure.
233
+ * @param key String key to search for within the tree structure.
231
234
*
232
- * @return The value of the JSONObject with the given key, or null if unfound.
235
+ * @return Value of the JSONObject with the given key, or null if unfound.
233
236
*/
234
237
public Object get (String key ) {
235
238
if (this .key .equals (key ))
@@ -253,7 +256,7 @@ public Object get(String key) {
253
256
/**
254
257
* Get the native type of the inner value of this JSONObject.
255
258
*
256
- * @return The class of the value (String, Number, JSONObject, List<?>, Boolean, or null)
259
+ * @return Class of the value (String, Number, JSONObject, List<?>, Boolean, or null)
257
260
*/
258
261
public Class <?> getInnerType () {
259
262
return value .getClass ();
@@ -265,8 +268,7 @@ public Class<?> getInnerType() {
265
268
* Traverse the tree structure of this JSONObject inorder and return an indexable list of the objects' values. Start
266
269
* with the leftmost value, then the root value, then the rightmost value, recursively.
267
270
*
268
- * @param result
269
- * A List<Object> to be populated with the values from the inorder traversal.
271
+ * @param result List<Object> to be populated with the values from the inorder traversal.
270
272
*/
271
273
public void inorderTraversal (List <Object > result ) {
272
274
if (value == null ) {
@@ -297,12 +299,10 @@ public Iterator<Object> iterator() {
297
299
// Stringify methods
298
300
299
301
/**
300
- * Turn this JSONObject into a String representation. Should result in a functionally identical String to the JSON
301
- * object which was read to create this JSONObject.
302
- *
303
- * @return The String representation of this JSONObject
302
+ * Turn this JSONObject into a String representation. Will result in a functionally identical String to the theoretical JSON
303
+ * file which was parsed to create this JSONObject.
304
304
*
305
- * @see JSONObject#toString(int)
305
+ * @return String representation of this JSONObject
306
306
*/
307
307
@ Override
308
308
public String toString () {
@@ -317,8 +317,7 @@ public int hashCode() {
317
317
/**
318
318
* Determine whether this JSONObect is equal to the other JSONObject by comparing their String representations.
319
319
*
320
- * @param other
321
- * A JSONObject with which to compare this JSONObject
320
+ * @param other JSONObject with which to compare this JSONObject
322
321
*
323
322
* @return True if the String representations are the same, False otherwise
324
323
*
0 commit comments