forked from stleary/JSON-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONBase.java
More file actions
126 lines (117 loc) · 3.63 KB
/
JSONBase.java
File metadata and controls
126 lines (117 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package org.json;
/**
* Common base class for JSON objects. Provides primitives for querying
* an object tree.
*
* @author stella
*
*/
public abstract class JSONBase {
/**
* Performs a cast from a source object to a target type.
* Here are the rules. If from is null or undefined, null
* is returned.
* @param <T>
* @param from
* @param to
* @return the cast value or null if the cast could not be performed
*/
@SuppressWarnings("unchecked")
public static <T> T cast(Object from, Class<T> to) {
try {
if (from==null || from==JSONObject.NULL) return null;
if (to==String.class) {
return (T)from.toString();
} else if (to==Integer.class) {
if (from instanceof Number) return (T)Integer.valueOf(((Number)from).intValue());
return (T)Integer.valueOf(Integer.parseInt(from.toString()));
} else if (to==Long.class) {
if (from instanceof Number) return (T)Long.valueOf(((Number)from).longValue());
return (T)Long.valueOf(Long.parseLong(from.toString()));
} else if (to==Double.class) {
if (from instanceof Number) return (T)Double.valueOf(((Number)from).doubleValue());
return (T)Double.valueOf(Double.parseDouble(from.toString()));
} else if (to==Float.class) {
if (from instanceof Number) return (T)Float.valueOf(((Number)from).floatValue());
return (T)Float.valueOf(Float.parseFloat(from.toString()));
} else if (to==Boolean.class) {
if (from instanceof Boolean) return (T)from;
if (from instanceof String) {
if ("true".equalsIgnoreCase((String)from)) return (T)Boolean.TRUE;
if ("false".equalsIgnoreCase((String)from)) return (T)Boolean.FALSE;
}
return null;
} else {
return null;
}
} catch (NumberFormatException e) {
return null;
}
}
/**
* Lookup a property by name. For objects, this just looks up the
* field with the given name. For arrays, the special member "length"
* is supported and returns an Integer length. Array indices are
* interpreted as well.
*
* @param name
* @return the member or null if not defined
*/
public abstract Object lookupProperty(String name);
/**
* Parse a path and evaluate it.
* @param expressionString the path to evaluate
* @return null if any part of expression is not found. otherwise the result
* of the last part of the expression
*/
public Object lookup(String expressionString) {
JSONExpression expr=new JSONExpression(expressionString);
return lookup(expr);
}
/**
* Evaluate a path against a pre-parsed expression
* @param expr
* @return result or null if any part is not found
*/
public Object lookup(JSONExpression expr) {
Object context=this;
for (int i=0; i<expr.length(); i++) {
String component=expr.at(i);
if (context instanceof JSONBase) {
JSONBase json=(JSONBase)context;
context=json.lookupProperty(component);
if (context==null) return null;
} else {
// Not an indexable - punt
return null;
}
}
return context;
}
/**
* Lookup and do a type cast
* @param <T>
* @param expr
* @param ofType
* @return cast lookup value
*/
public <T> T lookup(JSONExpression expr, Class<T> ofType) {
return cast(lookup(expr), ofType);
}
public <T> T lookup(String expr, Class<T> ofType) {
return lookup(new JSONExpression(expr), ofType);
}
@SuppressWarnings("unchecked")
public <T> T lookup(JSONExpression expr, T defaultValue) {
if (defaultValue==null) {
return (T)lookup(expr);
} else {
T ret=(T)lookup(expr, defaultValue.getClass());
if (ret==null) return defaultValue;
else return ret;
}
}
public <T> T lookup(String expr, T defaultValue) {
return lookup(new JSONExpression(expr), defaultValue);
}
}