forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonAccessor.java
More file actions
248 lines (198 loc) · 8.42 KB
/
Copy pathJsonAccessor.java
File metadata and controls
248 lines (198 loc) · 8.42 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.bridge.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.cloud.bridge.service.exception.InternalErrorException;
/**
* JsonAccessor provides the functionality to allow navigating JSON object graph using simple expressions,
* for example, following property access expressions are all valid ones
*
* rootobj.level1obj[1].property
* this[0].level1obj[1].property
*
*/
public class JsonAccessor {
private JsonElement _json;
Pattern _arrayAccessorMatcher = Pattern.compile("(.*)\\[(\\d+)\\]");
public JsonAccessor(JsonElement json) {
assert (json != null);
_json = json;
}
public BigDecimal getAsBigDecimal(String propPath) {
JsonElement jsonElement = eval(propPath);
return jsonElement.getAsBigDecimal();
}
public BigInteger getAsBigInteger(String propPath) {
JsonElement jsonElement = eval(propPath);
return jsonElement.getAsBigInteger();
}
public boolean getAsBoolean(String propPath) {
JsonElement jsonElement = eval(propPath);
return jsonElement.getAsBoolean();
}
public byte getAsByte(String propPath) {
JsonElement jsonElement = eval(propPath);
return jsonElement.getAsByte();
}
public char getAsCharacter(String propPath) {
JsonElement jsonElement = eval(propPath);
return jsonElement.getAsCharacter();
}
public double getAsDouble(String propPath) {
JsonElement jsonElement = eval(propPath);
return jsonElement.getAsDouble();
}
public float getAsFloat(String propPath) {
JsonElement jsonElement = eval(propPath);
return jsonElement.getAsFloat();
}
public int getAsInt(String propPath) {
JsonElement jsonElement = eval(propPath);
return jsonElement.getAsInt();
}
public long getAsLong(String propPath) {
JsonElement jsonElement = eval(propPath);
return jsonElement.getAsLong();
}
public Number getAsNumber(String propPath) {
JsonElement jsonElement = eval(propPath);
return jsonElement.getAsNumber();
}
public short getAsShort(String propPath) {
JsonElement jsonElement = eval(propPath);
return jsonElement.getAsShort();
}
public String getAsString(String propPath) {
JsonElement jsonElement = eval(propPath);
return jsonElement.getAsString();
}
public boolean isBoolean(String propPath) {
JsonElement jsonElement = eval(propPath);
if (jsonElement instanceof JsonPrimitive)
return ((JsonPrimitive)jsonElement).isBoolean();
return false;
}
public boolean isNumber(String propPath) {
JsonElement jsonElement = eval(propPath);
if (jsonElement instanceof JsonPrimitive)
return ((JsonPrimitive)jsonElement).isNumber();
return false;
}
public boolean isString(String propPath) {
JsonElement jsonElement = eval(propPath);
if (jsonElement instanceof JsonPrimitive)
return ((JsonPrimitive)jsonElement).isString();
return false;
}
/*
* Return
* -1 : property expression can not be resolved
* 0 : match to a null JSON object
* 1+ : matched, for array element, the count of the elements inside the array
*/
public int getMatchCount(String propPath) {
JsonElement jsonElement = tryEval(propPath);
if (jsonElement == null)
return -1;
if (jsonElement.isJsonNull())
return 0;
if (jsonElement.isJsonArray())
return ((JsonArray)jsonElement).size();
return 1;
}
public JsonElement eval(String propPath) {
JsonElement jsonElement = tryEval(propPath);
if (jsonElement == null)
throw new InternalErrorException("Property " + propPath + " is resolved to null JSON element on object: " + _json.toString());
return jsonElement;
}
public JsonElement tryEval(String propPath) {
assert (propPath != null);
String[] tokens = propPath.split("\\.");
ArrayList<Resolver> resolverChain = new ArrayList<Resolver>();
for (String token : tokens) {
Matcher matcher = _arrayAccessorMatcher.matcher(token);
if (matcher.find()) {
String propStr = matcher.group(1);
String indexStr = matcher.group(2);
resolverChain.add(new ArrayPropertyResolver(propStr, Integer.parseInt(indexStr)));
} else {
resolverChain.add(new PropertyResolver(token));
}
}
JsonElement jsonElementToResolveAt = _json;
for (Resolver resolver : resolverChain) {
jsonElementToResolveAt = resolver.resolve(jsonElementToResolveAt);
if (jsonElementToResolveAt == null)
break;
}
return jsonElementToResolveAt;
}
//
// Property resolvers
//
private static interface Resolver {
public JsonElement resolve(JsonElement jsonElementToResolveAt);
}
private static class PropertyResolver implements Resolver {
protected String _propName;
public PropertyResolver(String propName) {
_propName = propName;
}
public JsonElement resolve(JsonElement jsonElementToResolveAt) {
if ("this".equals(_propName))
return jsonElementToResolveAt;
if (jsonElementToResolveAt.isJsonObject())
return ((JsonObject)jsonElementToResolveAt).get(_propName);
if (jsonElementToResolveAt.isJsonNull())
throw new NullPointerException(String.format("Property %s points to a null element on object: %s", _propName, jsonElementToResolveAt.toString()));
throw new InternalErrorException("Unable to evaluate JSON accessor property: " + _propName + ", on object: " + jsonElementToResolveAt.toString());
}
}
private static class ArrayPropertyResolver extends PropertyResolver {
protected int _index;
public ArrayPropertyResolver(String propName, int index) {
super(propName);
_index = index;
}
public JsonElement resolve(JsonElement jsonElementToResolveAt) {
if (!"this".equals(_propName)) {
if (jsonElementToResolveAt.isJsonObject()) {
jsonElementToResolveAt = ((JsonObject)jsonElementToResolveAt).get(_propName);
} else {
if (jsonElementToResolveAt.isJsonNull())
throw new NullPointerException(String.format("Property %s points to a null element on object: %s", _propName, jsonElementToResolveAt.toString()));
throw new InternalErrorException("Unable to evaluate JSON accessor property: " + _propName + ", on object: " + jsonElementToResolveAt.toString());
}
}
if (jsonElementToResolveAt instanceof JsonArray) {
return ((JsonArray)jsonElementToResolveAt).get(_index);
}
if (jsonElementToResolveAt.isJsonNull())
throw new NullPointerException(String.format("Property %s points to a null element on object: %s", _propName, jsonElementToResolveAt.toString()));
throw new InternalErrorException("Unable to evaluate JSON accessor property: " + _propName + ", on object: " + jsonElementToResolveAt.toString());
}
}
}