1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.johnzon.core; |
20 | |
|
21 | |
import java.io.Serializable; |
22 | |
import java.util.AbstractMap; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.Map; |
25 | |
import java.util.Set; |
26 | |
|
27 | |
import javax.json.JsonArray; |
28 | |
import javax.json.JsonNumber; |
29 | |
import javax.json.JsonObject; |
30 | |
import javax.json.JsonString; |
31 | |
import javax.json.JsonValue; |
32 | |
|
33 | |
|
34 | |
final class JsonObjectImpl extends AbstractMap<String, JsonValue> implements JsonObject, Serializable { |
35 | 4519 | private Integer hashCode = null; |
36 | |
private final Map<String, JsonValue> unmodifieableBackingMap; |
37 | |
|
38 | |
private <T> T value(final String name, final Class<T> clazz) { |
39 | 83 | final Object v = unmodifieableBackingMap.get(name); |
40 | 83 | if (v != null) { |
41 | 82 | return clazz.cast(v); |
42 | |
} |
43 | 1 | throw new NullPointerException("no mapping for " + name); |
44 | |
} |
45 | |
|
46 | |
JsonObjectImpl(final Map<String, JsonValue> backingMap) { |
47 | 4519 | super(); |
48 | 4519 | this.unmodifieableBackingMap = backingMap; |
49 | 4519 | } |
50 | |
|
51 | |
@Override |
52 | |
public JsonArray getJsonArray(final String name) { |
53 | 6 | return value(name, JsonArray.class); |
54 | |
} |
55 | |
|
56 | |
@Override |
57 | |
public JsonObject getJsonObject(final String name) { |
58 | 2 | return value(name, JsonObject.class); |
59 | |
} |
60 | |
|
61 | |
@Override |
62 | |
public JsonNumber getJsonNumber(final String name) { |
63 | 11 | return value(name, JsonNumber.class); |
64 | |
} |
65 | |
|
66 | |
@Override |
67 | |
public JsonString getJsonString(final String name) { |
68 | 63 | return value(name, JsonString.class); |
69 | |
} |
70 | |
|
71 | |
@Override |
72 | |
public String getString(final String name) { |
73 | 63 | return getJsonString(name).getString(); |
74 | |
} |
75 | |
|
76 | |
@Override |
77 | |
public String getString(final String name, final String defaultValue) { |
78 | 0 | final Object v = unmodifieableBackingMap.get(name); |
79 | 0 | if (v != null) { |
80 | 0 | if (v instanceof JsonString) { |
81 | 0 | return JsonString.class.cast(v).getString(); |
82 | |
} else { |
83 | 0 | return defaultValue; |
84 | |
} |
85 | |
} else { |
86 | 0 | return defaultValue; |
87 | |
} |
88 | |
|
89 | |
} |
90 | |
|
91 | |
@Override |
92 | |
public int getInt(final String name) { |
93 | 7 | return getJsonNumber(name).intValue(); |
94 | |
} |
95 | |
|
96 | |
@Override |
97 | |
public int getInt(final String name, final int defaultValue) { |
98 | 0 | final Object v = unmodifieableBackingMap.get(name); |
99 | 0 | if (v != null) { |
100 | 0 | if (v instanceof JsonNumber) { |
101 | 0 | return JsonNumber.class.cast(v).intValue(); |
102 | |
} else { |
103 | 0 | return defaultValue; |
104 | |
} |
105 | |
} else { |
106 | 0 | return defaultValue; |
107 | |
} |
108 | |
} |
109 | |
|
110 | |
@Override |
111 | |
public boolean getBoolean(final String name) { |
112 | 0 | return value(name, JsonValue.class) == JsonValue.TRUE; |
113 | |
} |
114 | |
|
115 | |
@Override |
116 | |
public boolean getBoolean(final String name, final boolean defaultValue) { |
117 | 0 | final Object v = unmodifieableBackingMap.get(name); |
118 | 0 | if (v != null) { |
119 | 0 | if (v == JsonValue.TRUE) { |
120 | 0 | return true; |
121 | 0 | } else if (v == JsonValue.FALSE) { |
122 | 0 | return false; |
123 | |
} else { |
124 | 0 | return defaultValue; |
125 | |
} |
126 | |
} else { |
127 | 0 | return defaultValue; |
128 | |
} |
129 | |
} |
130 | |
|
131 | |
@Override |
132 | |
public boolean isNull(final String name) { |
133 | 1 | return value(name, JsonValue.class) == JsonValue.NULL; |
134 | |
} |
135 | |
|
136 | |
@Override |
137 | |
public ValueType getValueType() { |
138 | 2 | return ValueType.OBJECT; |
139 | |
} |
140 | |
|
141 | |
@Override |
142 | |
public String toString() { |
143 | 3 | final StringBuilder builder = new StringBuilder("{"); |
144 | 3 | final Iterator<Map.Entry<String, JsonValue>> it = unmodifieableBackingMap.entrySet().iterator(); |
145 | 3 | boolean hasNext = it.hasNext(); |
146 | 6 | while (hasNext) { |
147 | 3 | final Map.Entry<String, JsonValue> entry = it.next(); |
148 | |
|
149 | 3 | builder.append('"').append(entry.getKey()).append("\":"); |
150 | |
|
151 | 3 | final JsonValue value = entry.getValue(); |
152 | 3 | if (JsonString.class.isInstance(value)) { |
153 | 3 | builder.append(value.toString()); |
154 | |
} else { |
155 | 0 | builder.append(value != JsonValue.NULL ? value.toString() : JsonChars.NULL); |
156 | |
} |
157 | |
|
158 | 3 | hasNext = it.hasNext(); |
159 | 3 | if (hasNext) { |
160 | 0 | builder.append(","); |
161 | |
} |
162 | 3 | } |
163 | 3 | return builder.append('}').toString(); |
164 | |
} |
165 | |
|
166 | |
@Override |
167 | |
public boolean equals(final Object obj) { |
168 | 0 | return JsonObjectImpl.class.isInstance(obj) |
169 | |
&& unmodifieableBackingMap.equals(JsonObjectImpl.class.cast(obj).unmodifieableBackingMap); |
170 | |
} |
171 | |
|
172 | |
@Override |
173 | |
public int hashCode() { |
174 | 0 | Integer h = hashCode; |
175 | 0 | if (h == null) { |
176 | 0 | h = unmodifieableBackingMap.hashCode(); |
177 | 0 | hashCode = h; |
178 | |
} |
179 | 0 | return h; |
180 | |
} |
181 | |
|
182 | |
@Override |
183 | |
public Set<java.util.Map.Entry<String, JsonValue>> entrySet() { |
184 | 60 | return unmodifieableBackingMap.entrySet(); |
185 | |
} |
186 | |
} |