json 2.3.1 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +22 -0
  3. data/LICENSE +56 -0
  4. data/VERSION +1 -1
  5. data/ext/json/ext/generator/generator.c +60 -11
  6. data/ext/json/ext/generator/generator.h +5 -2
  7. data/ext/json/ext/parser/extconf.rb +25 -0
  8. data/ext/json/ext/parser/parser.c +110 -68
  9. data/ext/json/ext/parser/parser.h +1 -0
  10. data/ext/json/ext/parser/parser.rl +67 -25
  11. data/ext/json/extconf.rb +1 -0
  12. data/json.gemspec +11 -77
  13. data/lib/json.rb +171 -0
  14. data/lib/json/add/complex.rb +0 -1
  15. data/lib/json/add/rational.rb +0 -1
  16. data/lib/json/common.rb +240 -228
  17. data/lib/json/pure/generator.rb +28 -8
  18. data/lib/json/pure/parser.rb +20 -2
  19. data/lib/json/version.rb +1 -1
  20. data/tests/fixtures/fail29.json +1 -0
  21. data/tests/fixtures/fail30.json +1 -0
  22. data/tests/fixtures/fail31.json +1 -0
  23. data/tests/fixtures/fail32.json +1 -0
  24. data/tests/json_addition_test.rb +0 -4
  25. data/tests/json_common_interface_test.rb +43 -0
  26. data/tests/json_fixtures_test.rb +3 -0
  27. data/tests/json_generator_test.rb +16 -38
  28. data/tests/json_parser_test.rb +25 -0
  29. data/tests/lib/core_assertions.rb +763 -0
  30. data/tests/lib/envutil.rb +365 -0
  31. data/tests/lib/find_executable.rb +22 -0
  32. data/tests/lib/helper.rb +4 -0
  33. data/tests/ractor_test.rb +30 -0
  34. data/tests/test_helper.rb +3 -3
  35. metadata +16 -37
  36. data/.gitignore +0 -18
  37. data/.travis.yml +0 -26
  38. data/README-json-jruby.md +0 -33
  39. data/Rakefile +0 -334
  40. data/diagrams/.keep +0 -0
  41. data/install.rb +0 -23
  42. data/java/src/json/ext/ByteListTranscoder.java +0 -166
  43. data/java/src/json/ext/Generator.java +0 -466
  44. data/java/src/json/ext/GeneratorMethods.java +0 -231
  45. data/java/src/json/ext/GeneratorService.java +0 -42
  46. data/java/src/json/ext/GeneratorState.java +0 -490
  47. data/java/src/json/ext/OptionsReader.java +0 -113
  48. data/java/src/json/ext/Parser.java +0 -2362
  49. data/java/src/json/ext/Parser.rl +0 -893
  50. data/java/src/json/ext/ParserService.java +0 -34
  51. data/java/src/json/ext/RuntimeInfo.java +0 -116
  52. data/java/src/json/ext/StringDecoder.java +0 -166
  53. data/java/src/json/ext/StringEncoder.java +0 -111
  54. data/java/src/json/ext/Utils.java +0 -88
  55. data/json-java.gemspec +0 -37
  56. data/json_pure.gemspec +0 -33
  57. data/references/rfc7159.txt +0 -899
  58. data/tools/diff.sh +0 -18
  59. data/tools/fuzz.rb +0 -131
  60. data/tools/server.rb +0 -62
@@ -1,466 +0,0 @@
1
- /*
2
- * This code is copyrighted work by Daniel Luz <dev at mernen dot com>.
3
- *
4
- * Distributed under the Ruby license: https://www.ruby-lang.org/en/about/license.txt
5
- */
6
- package json.ext;
7
-
8
- import org.jruby.Ruby;
9
- import org.jruby.RubyArray;
10
- import org.jruby.RubyBasicObject;
11
- import org.jruby.RubyBignum;
12
- import org.jruby.RubyBoolean;
13
- import org.jruby.RubyClass;
14
- import org.jruby.RubyFixnum;
15
- import org.jruby.RubyFloat;
16
- import org.jruby.RubyHash;
17
- import org.jruby.RubyNumeric;
18
- import org.jruby.RubyString;
19
- import org.jruby.runtime.ClassIndex;
20
- import org.jruby.runtime.ThreadContext;
21
- import org.jruby.runtime.builtin.IRubyObject;
22
- import org.jruby.util.ByteList;
23
-
24
- public final class Generator {
25
- private Generator() {
26
- throw new RuntimeException();
27
- }
28
-
29
- /**
30
- * Encodes the given object as a JSON string, using the given handler.
31
- */
32
- static <T extends IRubyObject> RubyString
33
- generateJson(ThreadContext context, T object,
34
- Handler<? super T> handler, IRubyObject[] args) {
35
- Session session = new Session(context, args.length > 0 ? args[0]
36
- : null);
37
- return session.infect(handler.generateNew(session, object));
38
- }
39
-
40
- /**
41
- * Encodes the given object as a JSON string, detecting the appropriate handler
42
- * for the given object.
43
- */
44
- static <T extends IRubyObject> RubyString
45
- generateJson(ThreadContext context, T object, IRubyObject[] args) {
46
- Handler<? super T> handler = getHandlerFor(context.getRuntime(), object);
47
- return generateJson(context, object, handler, args);
48
- }
49
-
50
- /**
51
- * Encodes the given object as a JSON string, using the appropriate
52
- * handler if one is found or calling #to_json if not.
53
- */
54
- public static <T extends IRubyObject> RubyString
55
- generateJson(ThreadContext context, T object,
56
- GeneratorState config) {
57
- Session session = new Session(context, config);
58
- Handler<? super T> handler = getHandlerFor(context.getRuntime(), object);
59
- return handler.generateNew(session, object);
60
- }
61
-
62
- // NOTE: drop this once Ruby 1.9.3 support is gone!
63
- private static final int FIXNUM = 1;
64
- private static final int BIGNUM = 2;
65
- private static final int ARRAY = 3;
66
- private static final int STRING = 4;
67
- private static final int NIL = 5;
68
- private static final int TRUE = 6;
69
- private static final int FALSE = 7;
70
- private static final int HASH = 10;
71
- private static final int FLOAT = 11;
72
- // hard-coded due JRuby 1.7 compatibility
73
- // https://github.com/jruby/jruby/blob/1.7.27/core/src/main/java/org/jruby/runtime/ClassIndex.java
74
-
75
- /**
76
- * Returns the best serialization handler for the given object.
77
- */
78
- // Java's generics can't handle this satisfactorily, so I'll just leave
79
- // the best I could get and ignore the warnings
80
- @SuppressWarnings("unchecked")
81
- private static <T extends IRubyObject>
82
- Handler<? super T> getHandlerFor(Ruby runtime, T object) {
83
- switch (((RubyBasicObject) object).getNativeTypeIndex()) {
84
- // can not use getNativeClassIndex due 1.7 compatibility
85
- case NIL : return (Handler) NIL_HANDLER;
86
- case TRUE : return (Handler) TRUE_HANDLER;
87
- case FALSE : return (Handler) FALSE_HANDLER;
88
- case FLOAT : return (Handler) FLOAT_HANDLER;
89
- case FIXNUM : return (Handler) FIXNUM_HANDLER;
90
- case BIGNUM : return (Handler) BIGNUM_HANDLER;
91
- case STRING :
92
- if (((RubyBasicObject) object).getMetaClass() != runtime.getString()) break;
93
- return (Handler) STRING_HANDLER;
94
- case ARRAY :
95
- if (((RubyBasicObject) object).getMetaClass() != runtime.getArray()) break;
96
- return (Handler) ARRAY_HANDLER;
97
- case HASH :
98
- if (((RubyBasicObject) object).getMetaClass() != runtime.getHash()) break;
99
- return (Handler) HASH_HANDLER;
100
- }
101
- return GENERIC_HANDLER;
102
- }
103
-
104
-
105
- /* Generator context */
106
-
107
- /**
108
- * A class that concentrates all the information that is shared by
109
- * generators working on a single session.
110
- *
111
- * <p>A session is defined as the process of serializing a single root
112
- * object; any handler directly called by container handlers (arrays and
113
- * hashes/objects) shares this object with its caller.
114
- *
115
- * <p>Note that anything called indirectly (via {@link GENERIC_HANDLER})
116
- * won't be part of the session.
117
- */
118
- static class Session {
119
- private final ThreadContext context;
120
- private GeneratorState state;
121
- private IRubyObject possibleState;
122
- private RuntimeInfo info;
123
- private StringEncoder stringEncoder;
124
-
125
- private boolean tainted = false;
126
- private boolean untrusted = false;
127
-
128
- Session(ThreadContext context, GeneratorState state) {
129
- this.context = context;
130
- this.state = state;
131
- }
132
-
133
- Session(ThreadContext context, IRubyObject possibleState) {
134
- this.context = context;
135
- this.possibleState = possibleState == null || possibleState.isNil()
136
- ? null : possibleState;
137
- }
138
-
139
- public ThreadContext getContext() {
140
- return context;
141
- }
142
-
143
- public Ruby getRuntime() {
144
- return context.getRuntime();
145
- }
146
-
147
- public GeneratorState getState() {
148
- if (state == null) {
149
- state = GeneratorState.fromState(context, getInfo(), possibleState);
150
- }
151
- return state;
152
- }
153
-
154
- public RuntimeInfo getInfo() {
155
- if (info == null) info = RuntimeInfo.forRuntime(getRuntime());
156
- return info;
157
- }
158
-
159
- public StringEncoder getStringEncoder() {
160
- if (stringEncoder == null) {
161
- stringEncoder = new StringEncoder(context, getState().asciiOnly());
162
- }
163
- return stringEncoder;
164
- }
165
-
166
- public void infectBy(IRubyObject object) {
167
- if (object.isTaint()) tainted = true;
168
- if (object.isUntrusted()) untrusted = true;
169
- }
170
-
171
- public <T extends IRubyObject> T infect(T object) {
172
- if (tainted) object.setTaint(true);
173
- if (untrusted) object.setUntrusted(true);
174
- return object;
175
- }
176
- }
177
-
178
-
179
- /* Handler base classes */
180
-
181
- private static abstract class Handler<T extends IRubyObject> {
182
- /**
183
- * Returns an estimative of how much space the serialization of the
184
- * given object will take. Used for allocating enough buffer space
185
- * before invoking other methods.
186
- */
187
- int guessSize(Session session, T object) {
188
- return 4;
189
- }
190
-
191
- RubyString generateNew(Session session, T object) {
192
- RubyString result;
193
- ByteList buffer = new ByteList(guessSize(session, object));
194
- generate(session, object, buffer);
195
- result = RubyString.newString(session.getRuntime(), buffer);
196
- ThreadContext context = session.getContext();
197
- RuntimeInfo info = session.getInfo();
198
- result.force_encoding(context, info.utf8.get());
199
- return result;
200
- }
201
-
202
- abstract void generate(Session session, T object, ByteList buffer);
203
- }
204
-
205
- /**
206
- * A handler that returns a fixed keyword regardless of the passed object.
207
- */
208
- private static class KeywordHandler<T extends IRubyObject>
209
- extends Handler<T> {
210
- private final ByteList keyword;
211
-
212
- private KeywordHandler(String keyword) {
213
- this.keyword = new ByteList(ByteList.plain(keyword), false);
214
- }
215
-
216
- @Override
217
- int guessSize(Session session, T object) {
218
- return keyword.length();
219
- }
220
-
221
- @Override
222
- RubyString generateNew(Session session, T object) {
223
- return RubyString.newStringShared(session.getRuntime(), keyword);
224
- }
225
-
226
- @Override
227
- void generate(Session session, T object, ByteList buffer) {
228
- buffer.append(keyword);
229
- }
230
- }
231
-
232
-
233
- /* Handlers */
234
-
235
- static final Handler<RubyBignum> BIGNUM_HANDLER =
236
- new Handler<RubyBignum>() {
237
- @Override
238
- void generate(Session session, RubyBignum object, ByteList buffer) {
239
- // JRUBY-4751: RubyBignum.to_s() returns generic object
240
- // representation (fixed in 1.5, but we maintain backwards
241
- // compatibility; call to_s(IRubyObject[]) then
242
- buffer.append(((RubyString)object.to_s(IRubyObject.NULL_ARRAY)).getByteList());
243
- }
244
- };
245
-
246
- static final Handler<RubyFixnum> FIXNUM_HANDLER =
247
- new Handler<RubyFixnum>() {
248
- @Override
249
- void generate(Session session, RubyFixnum object, ByteList buffer) {
250
- buffer.append(object.to_s().getByteList());
251
- }
252
- };
253
-
254
- static final Handler<RubyFloat> FLOAT_HANDLER =
255
- new Handler<RubyFloat>() {
256
- @Override
257
- void generate(Session session, RubyFloat object, ByteList buffer) {
258
- double value = RubyFloat.num2dbl(object);
259
-
260
- if (Double.isInfinite(value) || Double.isNaN(value)) {
261
- if (!session.getState().allowNaN()) {
262
- throw Utils.newException(session.getContext(),
263
- Utils.M_GENERATOR_ERROR,
264
- object + " not allowed in JSON");
265
- }
266
- }
267
- buffer.append(((RubyString)object.to_s()).getByteList());
268
- }
269
- };
270
-
271
- static final Handler<RubyArray> ARRAY_HANDLER =
272
- new Handler<RubyArray>() {
273
- @Override
274
- int guessSize(Session session, RubyArray object) {
275
- GeneratorState state = session.getState();
276
- int depth = state.getDepth();
277
- int perItem =
278
- 4 // prealloc
279
- + (depth + 1) * state.getIndent().length() // indent
280
- + 1 + state.getArrayNl().length(); // ',' arrayNl
281
- return 2 + object.size() * perItem;
282
- }
283
-
284
- @Override
285
- void generate(Session session, RubyArray object, ByteList buffer) {
286
- ThreadContext context = session.getContext();
287
- Ruby runtime = context.getRuntime();
288
- GeneratorState state = session.getState();
289
- int depth = state.increaseDepth();
290
-
291
- ByteList indentUnit = state.getIndent();
292
- byte[] shift = Utils.repeat(indentUnit, depth);
293
-
294
- ByteList arrayNl = state.getArrayNl();
295
- byte[] delim = new byte[1 + arrayNl.length()];
296
- delim[0] = ',';
297
- System.arraycopy(arrayNl.unsafeBytes(), arrayNl.begin(), delim, 1,
298
- arrayNl.length());
299
-
300
- session.infectBy(object);
301
-
302
- buffer.append((byte)'[');
303
- buffer.append(arrayNl);
304
- boolean firstItem = true;
305
- for (int i = 0, t = object.getLength(); i < t; i++) {
306
- IRubyObject element = object.eltInternal(i);
307
- session.infectBy(element);
308
- if (firstItem) {
309
- firstItem = false;
310
- } else {
311
- buffer.append(delim);
312
- }
313
- buffer.append(shift);
314
- Handler<IRubyObject> handler = getHandlerFor(runtime, element);
315
- handler.generate(session, element, buffer);
316
- }
317
-
318
- state.decreaseDepth();
319
- if (arrayNl.length() != 0) {
320
- buffer.append(arrayNl);
321
- buffer.append(shift, 0, state.getDepth() * indentUnit.length());
322
- }
323
-
324
- buffer.append((byte)']');
325
- }
326
- };
327
-
328
- static final Handler<RubyHash> HASH_HANDLER =
329
- new Handler<RubyHash>() {
330
- @Override
331
- int guessSize(Session session, RubyHash object) {
332
- GeneratorState state = session.getState();
333
- int perItem =
334
- 12 // key, colon, comma
335
- + (state.getDepth() + 1) * state.getIndent().length()
336
- + state.getSpaceBefore().length()
337
- + state.getSpace().length();
338
- return 2 + object.size() * perItem;
339
- }
340
-
341
- @Override
342
- void generate(final Session session, RubyHash object,
343
- final ByteList buffer) {
344
- ThreadContext context = session.getContext();
345
- final Ruby runtime = context.getRuntime();
346
- final GeneratorState state = session.getState();
347
- final int depth = state.increaseDepth();
348
-
349
- final ByteList objectNl = state.getObjectNl();
350
- final byte[] indent = Utils.repeat(state.getIndent(), depth);
351
- final ByteList spaceBefore = state.getSpaceBefore();
352
- final ByteList space = state.getSpace();
353
-
354
- buffer.append((byte)'{');
355
- buffer.append(objectNl);
356
- object.visitAll(new RubyHash.Visitor() {
357
- private boolean firstPair = true;
358
-
359
- @Override
360
- public void visit(IRubyObject key, IRubyObject value) {
361
- if (firstPair) {
362
- firstPair = false;
363
- } else {
364
- buffer.append((byte)',');
365
- buffer.append(objectNl);
366
- }
367
- if (objectNl.length() != 0) buffer.append(indent);
368
-
369
- STRING_HANDLER.generate(session, key.asString(), buffer);
370
- session.infectBy(key);
371
-
372
- buffer.append(spaceBefore);
373
- buffer.append((byte)':');
374
- buffer.append(space);
375
-
376
- Handler<IRubyObject> valueHandler = getHandlerFor(runtime, value);
377
- valueHandler.generate(session, value, buffer);
378
- session.infectBy(value);
379
- }
380
- });
381
- state.decreaseDepth();
382
- if (objectNl.length() != 0) {
383
- buffer.append(objectNl);
384
- buffer.append(Utils.repeat(state.getIndent(), state.getDepth()));
385
- }
386
- buffer.append((byte)'}');
387
- }
388
- };
389
-
390
- static final Handler<RubyString> STRING_HANDLER =
391
- new Handler<RubyString>() {
392
- @Override
393
- int guessSize(Session session, RubyString object) {
394
- // for most applications, most strings will be just a set of
395
- // printable ASCII characters without any escaping, so let's
396
- // just allocate enough space for that + the quotes
397
- return 2 + object.getByteList().length();
398
- }
399
-
400
- @Override
401
- void generate(Session session, RubyString object, ByteList buffer) {
402
- RuntimeInfo info = session.getInfo();
403
- RubyString src;
404
-
405
- if (object.encoding(session.getContext()) != info.utf8.get()) {
406
- src = (RubyString)object.encode(session.getContext(),
407
- info.utf8.get());
408
- } else {
409
- src = object;
410
- }
411
-
412
- session.getStringEncoder().encode(src.getByteList(), buffer);
413
- }
414
- };
415
-
416
- static final Handler<RubyBoolean> TRUE_HANDLER =
417
- new KeywordHandler<RubyBoolean>("true");
418
- static final Handler<RubyBoolean> FALSE_HANDLER =
419
- new KeywordHandler<RubyBoolean>("false");
420
- static final Handler<IRubyObject> NIL_HANDLER =
421
- new KeywordHandler<IRubyObject>("null");
422
-
423
- /**
424
- * The default handler (<code>Object#to_json</code>): coerces the object
425
- * to string using <code>#to_s</code>, and serializes that string.
426
- */
427
- static final Handler<IRubyObject> OBJECT_HANDLER =
428
- new Handler<IRubyObject>() {
429
- @Override
430
- RubyString generateNew(Session session, IRubyObject object) {
431
- RubyString str = object.asString();
432
- return STRING_HANDLER.generateNew(session, str);
433
- }
434
-
435
- @Override
436
- void generate(Session session, IRubyObject object, ByteList buffer) {
437
- RubyString str = object.asString();
438
- STRING_HANDLER.generate(session, str, buffer);
439
- }
440
- };
441
-
442
- /**
443
- * A handler that simply calls <code>#to_json(state)</code> on the
444
- * given object.
445
- */
446
- static final Handler<IRubyObject> GENERIC_HANDLER =
447
- new Handler<IRubyObject>() {
448
- @Override
449
- RubyString generateNew(Session session, IRubyObject object) {
450
- if (object.respondsTo("to_json")) {
451
- IRubyObject result = object.callMethod(session.getContext(), "to_json",
452
- new IRubyObject[] {session.getState()});
453
- if (result instanceof RubyString) return (RubyString)result;
454
- throw session.getRuntime().newTypeError("to_json must return a String");
455
- } else {
456
- return OBJECT_HANDLER.generateNew(session, object);
457
- }
458
- }
459
-
460
- @Override
461
- void generate(Session session, IRubyObject object, ByteList buffer) {
462
- RubyString result = generateNew(session, object);
463
- buffer.append(result.getByteList());
464
- }
465
- };
466
- }