-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONValue.java
More file actions
113 lines (99 loc) · 3.86 KB
/
JSONValue.java
File metadata and controls
113 lines (99 loc) · 3.86 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
package javaxt.json;
//******************************************************************************
//** JSONValue Class
//******************************************************************************
/**
* Used to represent a value associated with a key in a JSONObject or an
* entry in a JSONArray. The value can be converted into a number of Java
* primitives (strings, integers, doubles, booleans, etc).
*
******************************************************************************/
public class JSONValue extends javaxt.utils.Value {
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class.
*/
public JSONValue(Object value){
super(value);
}
//**************************************************************************
//** toString
//**************************************************************************
/** Returns the value as a string. Returns null if the string is null or
* empty or if there was a problem converting the value to a string.
*/
public String toString(){
String str = super.toString();
if (str!=null){
if (str.trim().length()==0) return null;
}
return str;
}
//**************************************************************************
//** toJSONObject
//**************************************************************************
/** Returns the value as a JSONObject. Returns a null if there was a problem
* converting the value to an JSONObject or if the value is null.
*/
public JSONObject toJSONObject(){
Object obj = super.toObject();
if (obj!=null){
if (obj instanceof JSONObject) {
return (JSONObject) obj;
}
else if (obj instanceof javaxt.sql.Model){
return ((javaxt.sql.Model) obj).toJson();
}
}
return null;
}
//**************************************************************************
//** toJSONArray
//**************************************************************************
/** Returns the value as a JSONArray. Returns a null if there was a problem
* converting the value to an JSONArray or if the value is null.
*/
public JSONArray toJSONArray(){
Object obj = super.toObject();
if (obj!=null){
if (obj instanceof JSONArray) {
return (JSONArray) obj;
}
}
return null;
}
//**************************************************************************
//** get
//**************************************************************************
/** Returns the value associated with an index in a JSONArray, assuming the
* underlying object represented by this class is a JSONArray. This is
* shorthand for value.toJSONArray().get(i);
*/
public JSONValue get(int i){
Object obj = super.toObject();
if (obj!=null){
if (obj instanceof JSONArray) {
return ((JSONArray) obj).get(i);
}
}
return new JSONValue(null);
}
//**************************************************************************
//** get
//**************************************************************************
/** Returns the value associated with a key in a JSONObject, assuming the
* underlying object represented by this class is a JSONObject. This is
* shorthand for value.toJSONObject().get(key);
*/
public JSONValue get(String key) {
if (key == null) return new JSONValue(null);
Object obj = super.toObject();
if (obj!=null){
if (obj instanceof JSONObject) {
return ((JSONObject) obj).get(key);
}
}
return new JSONValue(null);
}
}