JsonUtils .java

net.minecraft.util.JsonUtils (class, 338 lines): members: isJsonPrimitive, getJsonObject, getJsonArray, isJsonArray, isBoolean, hasField, isString, getInt.

net.minecraft.utilclass338 linescommon11.1 KB

API Surface (25)

boolean isString(JsonObject p_151205_0_, String p_151205_1_)
boolean isString(JsonElement p_151211_0_)
boolean isBoolean(JsonObject p_180199_0_, String p_180199_1_)
boolean isJsonArray(JsonObject p_151202_0_, String p_151202_1_)
boolean isJsonPrimitive(JsonObject p_151201_0_, String p_151201_1_)
boolean hasField(JsonObject p_151204_0_, String p_151204_1_)
String getString(JsonElement p_151206_0_, String p_151206_1_)
String getString(JsonObject p_151200_0_, String p_151200_1_)
String getString(JsonObject p_151219_0_, String p_151219_1_, String p_151219_2_)
boolean getBoolean(JsonElement p_151216_0_, String p_151216_1_)
boolean getBoolean(JsonObject p_151212_0_, String p_151212_1_)
boolean getBoolean(JsonObject p_151209_0_, String p_151209_1_, boolean p_151209_2_)
float getFloat(JsonElement p_151220_0_, String p_151220_1_)
float getFloat(JsonObject p_151217_0_, String p_151217_1_)
float getFloat(JsonObject p_151221_0_, String p_151221_1_, float p_151221_2_)
int getInt(JsonElement p_151215_0_, String p_151215_1_)
int getInt(JsonObject p_151203_0_, String p_151203_1_)
int getInt(JsonObject p_151208_0_, String p_151208_1_, int p_151208_2_)
JsonObject getJsonObject(JsonElement p_151210_0_, String p_151210_1_)
JsonObject getJsonObject(JsonObject base, String key)
JsonObject getJsonObject(JsonObject p_151218_0_, String p_151218_1_, JsonObject p_151218_2_)
JsonArray getJsonArray(JsonElement p_151207_0_, String p_151207_1_)
JsonArray getJsonArray(JsonObject p_151214_0_, String p_151214_1_)
JsonArray getJsonArray(JsonObject p_151213_0_, String p_151213_1_, JsonArray p_151213_2_)
String toString(JsonElement p_151222_0_)

String literals (20)

"Expected "
" to be a string, was "
"Missing "
", expected to find a string"
" to be a Boolean, was "
", expected to find a Boolean"
" to be a Float, was "
", expected to find a Float"
" to be a Int, was "
", expected to find a Int"
" to be a JsonObject, was "
", expected to find a JsonObject"
" to be a JsonArray, was "
", expected to find a JsonArray"
"null (missing)"
"null (json)"
"an array ("
"an object ("
"a number ("
"a boolean ("

Full Source

JsonUtils.java338 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.util;
2
3
import com.google.gson.JsonArray;
4
import com.google.gson.JsonElement;
5
import com.google.gson.JsonObject;
6
import com.google.gson.JsonPrimitive;
7
import com.google.gson.JsonSyntaxException;
8
9
public class JsonUtils
10
{
11
    /**
12
     * Does the given JsonObject contain a string field with the given name?
13
     */
14
    public static boolean isString(JsonObject p_151205_0_, String p_151205_1_)
15
    {
16
        return !isJsonPrimitive(p_151205_0_, p_151205_1_) ? false : p_151205_0_.getAsJsonPrimitive(p_151205_1_).isString();
17
    }
18
19
    /**
20
     * Is the given JsonElement a string?
21
     */
22
    public static boolean isString(JsonElement p_151211_0_)
23
    {
24
        return !p_151211_0_.isJsonPrimitive() ? false : p_151211_0_.getAsJsonPrimitive().isString();
25
    }
26
27
    public static boolean isBoolean(JsonObject p_180199_0_, String p_180199_1_)
28
    {
29
        return !isJsonPrimitive(p_180199_0_, p_180199_1_) ? false : p_180199_0_.getAsJsonPrimitive(p_180199_1_).isBoolean();
30
    }
31
32
    /**
33
     * Does the given JsonObject contain an array field with the given name?
34
     */
35
    public static boolean isJsonArray(JsonObject p_151202_0_, String p_151202_1_)
36
    {
37
        return !hasField(p_151202_0_, p_151202_1_) ? false : p_151202_0_.get(p_151202_1_).isJsonArray();
38
    }
39
40
    /**
41
     * Does the given JsonObject contain a field with the given name whose type is primitive (String, Java primitive, or
42
     * Java primitive wrapper)?
43
     */
44
    public static boolean isJsonPrimitive(JsonObject p_151201_0_, String p_151201_1_)
45
    {
46
        return !hasField(p_151201_0_, p_151201_1_) ? false : p_151201_0_.get(p_151201_1_).isJsonPrimitive();
47
    }
48
49
    /**
50
     * Does the given JsonObject contain a field with the given name?
51
     */
52
    public static boolean hasField(JsonObject p_151204_0_, String p_151204_1_)
53
    {
54
        return p_151204_0_ == null ? false : p_151204_0_.get(p_151204_1_) != null;
55
    }
56
57
    /**
58
     * Gets the string value of the given JsonElement.  Expects the second parameter to be the name of the element's
59
     * field if an error message needs to be thrown.
60
     */
61
    public static String getString(JsonElement p_151206_0_, String p_151206_1_)
62
    {
63
        if (p_151206_0_.isJsonPrimitive())
64
        {
65
            return p_151206_0_.getAsString();
66
        }
67
        else
68
        {
69
            throw new JsonSyntaxException("Expected " + p_151206_1_ + " to be a string, was " + toString(p_151206_0_));
70
        }
71
    }
72
73
    /**
74
     * Gets the string value of the field on the JsonObject with the given name.
75
     */
76
    public static String getString(JsonObject p_151200_0_, String p_151200_1_)
77
    {
78
        if (p_151200_0_.has(p_151200_1_))
79
        {
80
            return getString(p_151200_0_.get(p_151200_1_), p_151200_1_);
81
        }
82
        else
83
        {
84
            throw new JsonSyntaxException("Missing " + p_151200_1_ + ", expected to find a string");
85
        }
86
    }
87
88
    /**
89
     * Gets the string value of the field on the JsonObject with the given name, or the given default value if the field
90
     * is missing.
91
     */
92
    public static String getString(JsonObject p_151219_0_, String p_151219_1_, String p_151219_2_)
93
    {
94
        return p_151219_0_.has(p_151219_1_) ? getString(p_151219_0_.get(p_151219_1_), p_151219_1_) : p_151219_2_;
95
    }
96
97
    /**
98
     * Gets the boolean value of the given JsonElement.  Expects the second parameter to be the name of the element's
99
     * field if an error message needs to be thrown.
100
     */
101
    public static boolean getBoolean(JsonElement p_151216_0_, String p_151216_1_)
102
    {
103
        if (p_151216_0_.isJsonPrimitive())
104
        {
105
            return p_151216_0_.getAsBoolean();
106
        }
107
        else
108
        {
109
            throw new JsonSyntaxException("Expected " + p_151216_1_ + " to be a Boolean, was " + toString(p_151216_0_));
110
        }
111
    }
112
113
    /**
114
     * Gets the boolean value of the field on the JsonObject with the given name.
115
     */
116
    public static boolean getBoolean(JsonObject p_151212_0_, String p_151212_1_)
117
    {
118
        if (p_151212_0_.has(p_151212_1_))
119
        {
120
            return getBoolean(p_151212_0_.get(p_151212_1_), p_151212_1_);
121
        }
122
        else
123
        {
124
            throw new JsonSyntaxException("Missing " + p_151212_1_ + ", expected to find a Boolean");
125
        }
126
    }
127
128
    /**
129
     * Gets the boolean value of the field on the JsonObject with the given name, or the given default value if the
130
     * field is missing.
131
     */
132
    public static boolean getBoolean(JsonObject p_151209_0_, String p_151209_1_, boolean p_151209_2_)
133
    {
134
        return p_151209_0_.has(p_151209_1_) ? getBoolean(p_151209_0_.get(p_151209_1_), p_151209_1_) : p_151209_2_;
135
    }
136
137
    /**
138
     * Gets the float value of the given JsonElement.  Expects the second parameter to be the name of the element's
139
     * field if an error message needs to be thrown.
140
     */
141
    public static float getFloat(JsonElement p_151220_0_, String p_151220_1_)
142
    {
143
        if (p_151220_0_.isJsonPrimitive() && p_151220_0_.getAsJsonPrimitive().isNumber())
144
        {
145
            return p_151220_0_.getAsFloat();
146
        }
147
        else
148
        {
149
            throw new JsonSyntaxException("Expected " + p_151220_1_ + " to be a Float, was " + toString(p_151220_0_));
150
        }
151
    }
152
153
    /**
154
     * Gets the float value of the field on the JsonObject with the given name.
155
     */
156
    public static float getFloat(JsonObject p_151217_0_, String p_151217_1_)
157
    {
158
        if (p_151217_0_.has(p_151217_1_))
159
        {
160
            return getFloat(p_151217_0_.get(p_151217_1_), p_151217_1_);
161
        }
162
        else
163
        {
164
            throw new JsonSyntaxException("Missing " + p_151217_1_ + ", expected to find a Float");
165
        }
166
    }
167
168
    /**
169
     * Gets the float value of the field on the JsonObject with the given name, or the given default value if the field
170
     * is missing.
171
     */
172
    public static float getFloat(JsonObject p_151221_0_, String p_151221_1_, float p_151221_2_)
173
    {
174
        return p_151221_0_.has(p_151221_1_) ? getFloat(p_151221_0_.get(p_151221_1_), p_151221_1_) : p_151221_2_;
175
    }
176
177
    /**
178
     * Gets the integer value of the given JsonElement.  Expects the second parameter to be the name of the element's
179
     * field if an error message needs to be thrown.
180
     */
181
    public static int getInt(JsonElement p_151215_0_, String p_151215_1_)
182
    {
183
        if (p_151215_0_.isJsonPrimitive() && p_151215_0_.getAsJsonPrimitive().isNumber())
184
        {
185
            return p_151215_0_.getAsInt();
186
        }
187
        else
188
        {
189
            throw new JsonSyntaxException("Expected " + p_151215_1_ + " to be a Int, was " + toString(p_151215_0_));
190
        }
191
    }
192
193
    /**
194
     * Gets the integer value of the field on the JsonObject with the given name.
195
     */
196
    public static int getInt(JsonObject p_151203_0_, String p_151203_1_)
197
    {
198
        if (p_151203_0_.has(p_151203_1_))
199
        {
200
            return getInt(p_151203_0_.get(p_151203_1_), p_151203_1_);
201
        }
202
        else
203
        {
204
            throw new JsonSyntaxException("Missing " + p_151203_1_ + ", expected to find a Int");
205
        }
206
    }
207
208
    /**
209
     * Gets the integer value of the field on the JsonObject with the given name, or the given default value if the
210
     * field is missing.
211
     */
212
    public static int getInt(JsonObject p_151208_0_, String p_151208_1_, int p_151208_2_)
213
    {
214
        return p_151208_0_.has(p_151208_1_) ? getInt(p_151208_0_.get(p_151208_1_), p_151208_1_) : p_151208_2_;
215
    }
216
217
    /**
218
     * Gets the given JsonElement as a JsonObject.  Expects the second parameter to be the name of the element's field
219
     * if an error message needs to be thrown.
220
     */
221
    public static JsonObject getJsonObject(JsonElement p_151210_0_, String p_151210_1_)
222
    {
223
        if (p_151210_0_.isJsonObject())
224
        {
225
            return p_151210_0_.getAsJsonObject();
226
        }
227
        else
228
        {
229
            throw new JsonSyntaxException("Expected " + p_151210_1_ + " to be a JsonObject, was " + toString(p_151210_0_));
230
        }
231
    }
232
233
    public static JsonObject getJsonObject(JsonObject base, String key)
234
    {
235
        if (base.has(key))
236
        {
237
            return getJsonObject(base.get(key), key);
238
        }
239
        else
240
        {
241
            throw new JsonSyntaxException("Missing " + key + ", expected to find a JsonObject");
242
        }
243
    }
244
245
    /**
246
     * Gets the JsonObject field on the JsonObject with the given name, or the given default value if the field is
247
     * missing.
248
     */
249
    public static JsonObject getJsonObject(JsonObject p_151218_0_, String p_151218_1_, JsonObject p_151218_2_)
250
    {
251
        return p_151218_0_.has(p_151218_1_) ? getJsonObject(p_151218_0_.get(p_151218_1_), p_151218_1_) : p_151218_2_;
252
    }
253
254
    /**
255
     * Gets the given JsonElement as a JsonArray.  Expects the second parameter to be the name of the element's field if
256
     * an error message needs to be thrown.
257
     */
258
    public static JsonArray getJsonArray(JsonElement p_151207_0_, String p_151207_1_)
259
    {
260
        if (p_151207_0_.isJsonArray())
261
        {
262
            return p_151207_0_.getAsJsonArray();
263
        }
264
        else
265
        {
266
            throw new JsonSyntaxException("Expected " + p_151207_1_ + " to be a JsonArray, was " + toString(p_151207_0_));
267
        }
268
    }
269
270
    /**
271
     * Gets the JsonArray field on the JsonObject with the given name.
272
     */
273
    public static JsonArray getJsonArray(JsonObject p_151214_0_, String p_151214_1_)
274
    {
275
        if (p_151214_0_.has(p_151214_1_))
276
        {
277
            return getJsonArray(p_151214_0_.get(p_151214_1_), p_151214_1_);
278
        }
279
        else
280
        {
281
            throw new JsonSyntaxException("Missing " + p_151214_1_ + ", expected to find a JsonArray");
282
        }
283
    }
284
285
    /**
286
     * Gets the JsonArray field on the JsonObject with the given name, or the given default value if the field is
287
     * missing.
288
     */
289
    public static JsonArray getJsonArray(JsonObject p_151213_0_, String p_151213_1_, JsonArray p_151213_2_)
290
    {
291
        return p_151213_0_.has(p_151213_1_) ? getJsonArray(p_151213_0_.get(p_151213_1_), p_151213_1_) : p_151213_2_;
292
    }
293
294
    /**
295
     * Gets a human-readable description of the given JsonElement's type.  For example: "a number (4)"
296
     */
297
    public static String toString(JsonElement p_151222_0_)
298
    {
299
        String s = org.apache.commons.lang3.StringUtils.abbreviateMiddle(String.valueOf((Object)p_151222_0_), "...", 10);
300
301
        if (p_151222_0_ == null)
302
        {
303
            return "null (missing)";
304
        }
305
        else if (p_151222_0_.isJsonNull())
306
        {
307
            return "null (json)";
308
        }
309
        else if (p_151222_0_.isJsonArray())
310
        {
311
            return "an array (" + s + ")";
312
        }
313
        else if (p_151222_0_.isJsonObject())
314
        {
315
            return "an object (" + s + ")";
316
        }
317
        else
318
        {
319
            if (p_151222_0_.isJsonPrimitive())
320
            {
321
                JsonPrimitive jsonprimitive = p_151222_0_.getAsJsonPrimitive();
322
323
                if (jsonprimitive.isNumber())
324
                {
325
                    return "a number (" + s + ")";
326
                }
327
328
                if (jsonprimitive.isBoolean())
329
                {
330
                    return "a boolean (" + s + ")";
331
                }
332
            }
333
334
            return s;
335
        }
336
    }
337
}
338

Deobfuscated MCP 1.8.9 source of JsonUtils from Marcelektro/MavenMCP-1.8.9 (net/minecraft/util/JsonUtils.java), stored verbatim.