summaryrefslogtreecommitdiff
path: root/platform/script-debugger/protocol/protocol-reader-runtime/src/org/jetbrains/jsonProtocol/JsonReaders.java
blob: aa1d19f1bad2299ef58c2537a4e7fd3bb18890a5 (plain)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package org.jetbrains.jsonProtocol;

import com.google.gson.stream.JsonToken;
import com.intellij.util.ArrayUtilRt;
import gnu.trove.TDoubleArrayList;
import gnu.trove.THashMap;
import gnu.trove.TIntArrayList;
import gnu.trove.TLongArrayList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.io.JsonReaderEx;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class JsonReaders {
  private JsonReaders() {
  }

  private static void checkIsNull(JsonReaderEx reader, String fieldName) {
    if (reader.peek() == JsonToken.NULL) {
      throw new RuntimeException("Field is not nullable" + (fieldName == null ? "" : (": " + fieldName)));
    }
  }

  public static String readString(JsonReaderEx reader, String fieldName) {
    checkIsNull(reader, fieldName);
    return reader.nextString();
  }

  public static String readRawString(JsonReaderEx reader) {
    return reader.nextString(true);
  }

  public static Object readRawStringOrMap(JsonReaderEx reader) {
    if (reader.peek() == JsonToken.BEGIN_OBJECT) {
      return readMap(reader, null);
    }
    else {
      return reader.nextString(true);
    }
  }

  public static String readNullableString(JsonReaderEx reader) {
    if (reader.peek() == JsonToken.NULL) {
      reader.skipValue();
      return null;
    }
    return reader.nextString();
  }

  public static boolean readBoolean(JsonReaderEx reader, String fieldName) {
    checkIsNull(reader, fieldName);
    return reader.nextBoolean();
  }

  public static boolean readNullableBoolean(JsonReaderEx reader) {
    if (reader.peek() == JsonToken.NULL) {
      reader.skipValue();
      return false;
    }
    return reader.nextBoolean();
  }

  public static int readInt(JsonReaderEx reader, String fieldName) {
    checkIsNull(reader, fieldName);
    return reader.nextInt();
  }

  public static int readNullableInt(JsonReaderEx reader) {
    if (reader.peek() == JsonToken.NULL) {
      reader.skipValue();
      return -1;
    }
    return reader.nextInt();
  }

  public static long readLong(JsonReaderEx reader, String fieldName) {
    checkIsNull(reader, fieldName);
    return reader.nextLong();
  }

  public static double readDouble(JsonReaderEx reader, String fieldName) {
    checkIsNull(reader, fieldName);
    return reader.nextDouble();
  }

  public static long readNullableLong(JsonReaderEx reader) {
    if (reader.peek() == JsonToken.NULL) {
      reader.skipValue();
      return -1;
    }
    return reader.nextLong();
  }

  public static <T extends Enum<T>> T readEnum(JsonReaderEx reader, String fieldName, Class<T> enumClass) {
    checkIsNull(reader, fieldName);
    try {
      return Enum.valueOf(enumClass, readEnumName(reader));
    }
    catch (IllegalArgumentException ignored) {
      return Enum.valueOf(enumClass, "NO_ENUM_CONST");
    }
  }

  public static String convertRawEnumName(@NotNull String enumValue) {
    StringBuilder builder = new StringBuilder(enumValue.length() + 4);
    boolean prevIsLowerCase = false;
    for (int i = 0; i < enumValue.length(); i++) {
      char c = enumValue.charAt(i);
      if (c == '-' || c == ' ') {
        builder.append('_');
        continue;
      }

      if (Character.isUpperCase(c)) {
        // second check handle "CSPViolation" (transform to CSP_VIOLATION)
        if (prevIsLowerCase || ((i + 1) < enumValue.length() && Character.isLowerCase(enumValue.charAt(i + 1)))) {
          builder.append('_');
        }
        builder.append(c);
      }
      else {
        builder.append(Character.toUpperCase(c));
        prevIsLowerCase = true;
      }
    }
    return builder.toString();
  }

  private static String readEnumName(JsonReaderEx reader) {
    return convertRawEnumName(reader.nextString());
  }

  public static <T extends Enum<T>> T readNullableEnum(JsonReaderEx reader, Class<T> enumClass) {
    if (reader.peek() == JsonToken.NULL) {
      reader.skipValue();
      return null;
    }
    return Enum.valueOf(enumClass, readEnumName(reader));
  }

  public static <T> List<T> readObjectArray(JsonReaderEx reader, String fieldName, ObjectFactory<T> factory, boolean nullable) {
    if (reader.peek() == JsonToken.NULL) {
      if (nullable) {
        reader.skipValue();
        return null;
      }
      else {
        checkIsNull(reader, fieldName);
      }
    }

    reader.beginArray();
    if (!reader.hasNext()) {
      reader.endArray();
      if (nullable) {
        return null;
      }
      else {
        return Collections.emptyList();
      }
    }

    List<T> result = new ArrayList<T>();
    do {
      result.add(factory.read(reader));
    }
    while (reader.hasNext());
    reader.endArray();
    return result;
  }

  public static Map<?, ?> readMap(JsonReaderEx reader, String fieldName) {
    checkIsNull(reader, fieldName);
    reader.beginObject();
    if (!reader.hasNext()) {
      reader.endObject();
      return Collections.emptyMap();
    }
    return nextObject(reader);
  }

  public static Object read(JsonReaderEx reader) {
    switch (reader.peek()) {
      case BEGIN_ARRAY:
        return nextList(reader);

      case BEGIN_OBJECT:
        reader.beginObject();
        return nextObject(reader);

      case STRING:
        return reader.nextString();

      case NUMBER:
        return reader.nextDouble();

      case BOOLEAN:
        return reader.nextBoolean();

      case NULL:
        reader.nextNull();
        return null;

      default: throw new IllegalStateException();
    }
  }

  public static Map<String, Object> nextObject(JsonReaderEx reader) {
    Map<String, Object> map = new THashMap<String, Object>();
    while (reader.hasNext()) {
      map.put(reader.nextName(), read(reader));
    }
    reader.endObject();
    return map;
  }

  public static <T> List<T> nextList(JsonReaderEx reader) {
    reader.beginArray();
    if (!reader.hasNext()) {
      reader.endArray();
      return Collections.emptyList();
    }

    List<T> list = new ArrayList<T>();
    do {
      //noinspection unchecked
      list.add((T)read(reader));
    }
    while (reader.hasNext());
    reader.endArray();
    return list;
  }

  public static List<String> readListOfPrimitive(JsonReaderEx reader) {
    reader.beginArray();
    if (!reader.hasNext()) {
      reader.endArray();
      return Collections.emptyList();
    }

    List<String> list = new ArrayList<String>();
    do {
      //noinspection unchecked
      list.add(reader.nextString(true));
    }
    while (reader.hasNext());
    reader.endArray();
    return list;
  }

  public static long[] readLongArray(JsonReaderEx reader) {
    checkIsNull(reader, null);
    reader.beginArray();
    if (!reader.hasNext()) {
      reader.endArray();
      return ArrayUtilRt.EMPTY_LONG_ARRAY;
    }

    TLongArrayList result = new TLongArrayList();
    do {
      result.add(reader.nextLong());
    }
    while (reader.hasNext());
    reader.endArray();
    return result.toNativeArray();
  }

  public static double[] readDoubleArray(JsonReaderEx reader) {
    checkIsNull(reader, null);
    reader.beginArray();
    if (!reader.hasNext()) {
      reader.endArray();
      return new double[]{0};
    }

    TDoubleArrayList result = new TDoubleArrayList();
    do {
      result.add(reader.nextDouble());
    }
    while (reader.hasNext());
    reader.endArray();
    return result.toNativeArray();
  }

  public static int[] readIntArray(JsonReaderEx reader) {
    checkIsNull(reader, null);
    reader.beginArray();
    if (!reader.hasNext()) {
      reader.endArray();
      return ArrayUtilRt.EMPTY_INT_ARRAY;
    }

    TIntArrayList result = new TIntArrayList();
    do {
      result.add(reader.nextInt());
    }
    while (reader.hasNext());
    reader.endArray();
    return result.toNativeArray();
  }

  public static List<StringIntPair> readIntStringPairs(JsonReaderEx reader) {
    checkIsNull(reader, null);
    reader.beginArray();
    if (!reader.hasNext()) {
      reader.endArray();
      return Collections.emptyList();
    }

    List<StringIntPair> result = new ArrayList<StringIntPair>();
    do {
      reader.beginArray();
      result.add(new StringIntPair(reader.nextInt(), reader.nextString()));
      reader.endArray();
    }
    while (reader.hasNext());
    reader.endArray();
    return result;
  }

  public static JsonReaderEx createReader(CharSequence string) {
    return new JsonReaderEx(string);
  }

  public static boolean findBooleanField(String name, JsonReaderEx reader) {
    reader.beginObject();
    while (reader.hasNext()) {
      if (reader.nextName().equals(name)) {
        return reader.nextBoolean();
      }
      else {
        reader.skipValue();
      }
    }
    return false;
  }
}