1
|
package eu.dnetlib.utils;
|
2
|
|
3
|
import java.text.FieldPosition;
|
4
|
import java.text.Format;
|
5
|
import java.text.NumberFormat;
|
6
|
import java.text.ParsePosition;
|
7
|
import java.util.ArrayList;
|
8
|
import java.util.HashMap;
|
9
|
import java.util.List;
|
10
|
import java.util.Map;
|
11
|
|
12
|
public class JsonFormat extends Format {
|
13
|
private class JsonPair {
|
14
|
private final String name;
|
15
|
private final Object value;
|
16
|
|
17
|
public JsonPair(final String name, final Object value) {
|
18
|
this.name = name;
|
19
|
this.value = value;
|
20
|
}
|
21
|
|
22
|
public String getName() {
|
23
|
return name;
|
24
|
}
|
25
|
|
26
|
public Object getValue() {
|
27
|
return value;
|
28
|
}
|
29
|
}
|
30
|
|
31
|
private static final long serialVersionUID = 1L;
|
32
|
|
33
|
@Override
|
34
|
@SuppressWarnings("unchecked")
|
35
|
public StringBuffer format(final Object object, final StringBuffer toAppendTo, final FieldPosition position) {
|
36
|
if (object == null)
|
37
|
toAppendTo.append("null");
|
38
|
else if (object instanceof Boolean)
|
39
|
toAppendTo.append(object);
|
40
|
else if (object instanceof List<?>)
|
41
|
formatJsonArray((List<Object>) object, toAppendTo);
|
42
|
else if (object instanceof Number)
|
43
|
formatJsonNumber((Number) object, toAppendTo);
|
44
|
else if (object instanceof Map<?, ?>)
|
45
|
formatJsonObject((Map<String, Object>) object, toAppendTo);
|
46
|
else if (object instanceof String)
|
47
|
formatJsonString((String) object, toAppendTo);
|
48
|
else
|
49
|
throw new IllegalArgumentException();
|
50
|
return toAppendTo;
|
51
|
}
|
52
|
|
53
|
@Override
|
54
|
public Object parseObject(final String source, final ParsePosition position) {
|
55
|
if (position.getIndex() >= source.length()) { // premature end of source
|
56
|
position.setErrorIndex(position.getIndex());
|
57
|
return null;
|
58
|
}
|
59
|
if (source.startsWith("null", position.getIndex())) {
|
60
|
position.setIndex(position.getIndex() + 4);
|
61
|
return null;
|
62
|
}
|
63
|
if (source.startsWith("true", position.getIndex())) {
|
64
|
position.setIndex(position.getIndex() + 4);
|
65
|
return Boolean.TRUE;
|
66
|
}
|
67
|
if (source.startsWith("false", position.getIndex())) {
|
68
|
position.setIndex(position.getIndex() + 5);
|
69
|
return Boolean.FALSE;
|
70
|
}
|
71
|
final int beginIndex = position.getIndex();
|
72
|
Object value = null;
|
73
|
switch (source.charAt(position.getIndex())) {
|
74
|
case '[':
|
75
|
value = parseJsonArray(source, position);
|
76
|
break;
|
77
|
case '{':
|
78
|
value = parseJsonObject(source, position);
|
79
|
break;
|
80
|
case '"':
|
81
|
value = parseJsonString(source, position);
|
82
|
break;
|
83
|
default:
|
84
|
value = parseJsonNumber(source, position);
|
85
|
}
|
86
|
if (position.getErrorIndex() != -1)
|
87
|
position.setIndex(beginIndex);
|
88
|
return value;
|
89
|
}
|
90
|
|
91
|
private void formatJsonArray(final List<Object> array, final StringBuffer toAppendTo) {
|
92
|
toAppendTo.append('[');
|
93
|
for (Object object : array) {
|
94
|
format(object, toAppendTo, null);
|
95
|
toAppendTo.append(',');
|
96
|
}
|
97
|
if (array.size() > 0)
|
98
|
toAppendTo.setLength(toAppendTo.length() - 1);
|
99
|
toAppendTo.append(']');
|
100
|
}
|
101
|
|
102
|
private void formatJsonNumber(final Number number, final StringBuffer toAppendTo) {
|
103
|
toAppendTo.append(number);
|
104
|
}
|
105
|
|
106
|
private void formatJsonObject(final Map<String, Object> object, final StringBuffer toAppendTo) {
|
107
|
toAppendTo.append('{');
|
108
|
for (Map.Entry<?, ?> pair : object.entrySet()) {
|
109
|
if (pair.getKey() instanceof String)
|
110
|
formatJsonString((String) pair.getKey(), toAppendTo);
|
111
|
else
|
112
|
throw new IllegalArgumentException();
|
113
|
toAppendTo.append(':');
|
114
|
format(pair.getValue(), toAppendTo, null);
|
115
|
toAppendTo.append(',');
|
116
|
}
|
117
|
if (object.size() > 0)
|
118
|
toAppendTo.setLength(toAppendTo.length() - 1);
|
119
|
toAppendTo.append('}');
|
120
|
}
|
121
|
|
122
|
private void formatJsonString(final String string, final StringBuffer toAppendTo) {
|
123
|
toAppendTo.append('"');
|
124
|
for (int i = 0; i < string.length(); i++) {
|
125
|
switch (string.charAt(i)) {
|
126
|
case '"':
|
127
|
toAppendTo.append("\\\"");
|
128
|
break;
|
129
|
case '\\':
|
130
|
toAppendTo.append("\\\\");
|
131
|
break;
|
132
|
case '\b':
|
133
|
toAppendTo.append("\\b");
|
134
|
break;
|
135
|
case '\f':
|
136
|
toAppendTo.append("\\f");
|
137
|
break;
|
138
|
case '\n':
|
139
|
toAppendTo.append("\\n");
|
140
|
break;
|
141
|
case '\r':
|
142
|
toAppendTo.append("\\r");
|
143
|
break;
|
144
|
case '\t':
|
145
|
toAppendTo.append("\\t");
|
146
|
break;
|
147
|
default:
|
148
|
toAppendTo.append(string.charAt(i));
|
149
|
}
|
150
|
}
|
151
|
toAppendTo.append('"');
|
152
|
}
|
153
|
|
154
|
private List<Object> parseJsonArray(final String source, final ParsePosition position) {
|
155
|
if ((position.getIndex() >= source.length()) || (source.charAt(position.getIndex()) != '[')) { // premature end of source or no opening bracket found
|
156
|
position.setErrorIndex(position.getIndex());
|
157
|
return null;
|
158
|
}
|
159
|
position.setIndex(position.getIndex() + 1);
|
160
|
final List<Object> array = new ArrayList<Object>();
|
161
|
if ((position.getIndex() < source.length()) && (source.charAt(position.getIndex()) != ']')) { // next character is not closing bracket; elements exist
|
162
|
parseJsonElements(source, position, array);
|
163
|
if (position.getErrorIndex() != -1)
|
164
|
return null;
|
165
|
}
|
166
|
if ((position.getIndex() >= source.length()) || (source.charAt(position.getIndex()) != ']')) { // premature end of source or no closing bracket found
|
167
|
position.setErrorIndex(position.getIndex());
|
168
|
return null;
|
169
|
}
|
170
|
position.setIndex(position.getIndex() + 1);
|
171
|
return array;
|
172
|
}
|
173
|
|
174
|
private void parseJsonElements(final String source, final ParsePosition position, final List<Object> elements) {
|
175
|
final Object value = parseObject(source, position);
|
176
|
if (position.getErrorIndex() != -1)
|
177
|
return;
|
178
|
elements.add(value);
|
179
|
if ((position.getIndex() < source.length()) && (source.charAt(position.getIndex()) == ',')) { // next character is a comma; more elements
|
180
|
position.setIndex(position.getIndex() + 1);
|
181
|
parseJsonElements(source, position, elements);
|
182
|
if (position.getErrorIndex() != -1) // no more elements found
|
183
|
return;
|
184
|
}
|
185
|
}
|
186
|
|
187
|
private Number parseJsonNumber(final String source, final ParsePosition position) {
|
188
|
if (position.getIndex() >= source.length()) { // premature end of input
|
189
|
position.setErrorIndex(position.getIndex());
|
190
|
return null;
|
191
|
}
|
192
|
return NumberFormat.getInstance().parse(source, position);
|
193
|
}
|
194
|
|
195
|
private Map<String, Object> parseJsonObject(final String source, final ParsePosition position) {
|
196
|
if ((position.getIndex() >= source.length()) || (source.charAt(position.getIndex()) != '{')) { // premature end of source or no opening brace found
|
197
|
position.setErrorIndex(position.getIndex());
|
198
|
return null;
|
199
|
}
|
200
|
position.setIndex(position.getIndex() + 1);
|
201
|
final Map<String, Object> object = new HashMap<String, Object>();
|
202
|
if ((position.getIndex() < source.length()) && (source.charAt(position.getIndex()) != '}')) { // next character is not closing brace; members exist
|
203
|
parseJsonMembers(source, position, object);
|
204
|
if (position.getErrorIndex() != -1)
|
205
|
return null;
|
206
|
}
|
207
|
if ((position.getIndex() >= source.length()) || (source.charAt(position.getIndex()) != '}')) { // premature end of source or no closing brace found
|
208
|
position.setErrorIndex(position.getIndex());
|
209
|
return null;
|
210
|
}
|
211
|
position.setIndex(position.getIndex() + 1);
|
212
|
return object;
|
213
|
}
|
214
|
|
215
|
private void parseJsonMembers(final String source, final ParsePosition position, final Map<String, Object> members) {
|
216
|
final JsonPair pair = parseJsonPair(source, position);
|
217
|
if (position.getErrorIndex() != -1) // no pair found
|
218
|
return;
|
219
|
members.put(pair.getName(), pair.getValue());
|
220
|
if ((position.getIndex() < source.length()) && (source.charAt(position.getIndex()) == ',')) { // next character is a comma; more members follow
|
221
|
position.setIndex(position.getIndex() + 1);
|
222
|
parseJsonMembers(source, position, members);
|
223
|
if (position.getErrorIndex() != -1) // no more members found
|
224
|
return;
|
225
|
}
|
226
|
}
|
227
|
|
228
|
private JsonPair parseJsonPair(final String source, final ParsePosition position) {
|
229
|
final String name = parseJsonString(source, position);
|
230
|
if (position.getErrorIndex() != -1) // no name found
|
231
|
return null;
|
232
|
if ((position.getIndex() >= source.length()) || (source.charAt(position.getIndex()) != ':')) { // premature end of input or no colon found
|
233
|
position.setErrorIndex(position.getIndex());
|
234
|
return null;
|
235
|
}
|
236
|
position.setIndex(position.getIndex() + 1);
|
237
|
final Object value = parseObject(source, position);
|
238
|
if (position.getErrorIndex() != -1) // no value found
|
239
|
return null;
|
240
|
return new JsonPair(name, value);
|
241
|
}
|
242
|
|
243
|
private String parseJsonString(final String source, final ParsePosition position) {
|
244
|
if ((position.getIndex() >= source.length()) || (source.charAt(position.getIndex()) != '"')) { // premature end of source or no opening quote found
|
245
|
position.setErrorIndex(position.getIndex());
|
246
|
return null;
|
247
|
}
|
248
|
final StringBuilder string = new StringBuilder();
|
249
|
for (int i = position.getIndex() + 1; i < source.length(); i++) {
|
250
|
switch (source.charAt(i)) {
|
251
|
case '"': // closing quote found; JSON string ended
|
252
|
position.setIndex(i + 1);
|
253
|
return string.toString();
|
254
|
case '\\': // escape character
|
255
|
if (++i == source.length()) { // no next character found
|
256
|
position.setErrorIndex(i);
|
257
|
return null;
|
258
|
}
|
259
|
switch(source.charAt(i)) {
|
260
|
case '"': // quote
|
261
|
string.append('"');
|
262
|
break;
|
263
|
case '\\': // backslash
|
264
|
string.append('\\');
|
265
|
break;
|
266
|
case 'b': // backspace
|
267
|
string.append('\b');
|
268
|
break;
|
269
|
case 'f': // form feed
|
270
|
string.append('\f');
|
271
|
break;
|
272
|
case 'n': // newline
|
273
|
string.append('\n');
|
274
|
break;
|
275
|
case 'r': // carriage return
|
276
|
string.append('\n');
|
277
|
break;
|
278
|
case 't': // horizontal tab
|
279
|
string.append('\t');
|
280
|
break;
|
281
|
default: // invalid escape character
|
282
|
position.setErrorIndex(i);
|
283
|
return null;
|
284
|
}
|
285
|
break;
|
286
|
default:
|
287
|
string.append(source.charAt(i));
|
288
|
}
|
289
|
} // source ended without finding closing quote
|
290
|
position.setErrorIndex(source.length());
|
291
|
return null;
|
292
|
}
|
293
|
}
|