json_pure 2.3.0 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +55 -0
  3. data/README.md +16 -0
  4. data/VERSION +1 -1
  5. data/json_pure.gemspec +49 -26
  6. data/lib/json.rb +549 -29
  7. data/lib/json/add/complex.rb +0 -1
  8. data/lib/json/add/rational.rb +0 -1
  9. data/lib/json/common.rb +370 -123
  10. data/lib/json/pure/generator.rb +29 -9
  11. data/lib/json/pure/parser.rb +22 -4
  12. data/lib/json/version.rb +1 -1
  13. data/tests/fixtures/fail29.json +1 -0
  14. data/tests/fixtures/fail30.json +1 -0
  15. data/tests/fixtures/fail31.json +1 -0
  16. data/tests/fixtures/fail32.json +1 -0
  17. data/tests/json_addition_test.rb +0 -4
  18. data/tests/json_common_interface_test.rb +43 -0
  19. data/tests/json_fixtures_test.rb +9 -1
  20. data/tests/json_generator_test.rb +16 -38
  21. data/tests/json_parser_test.rb +25 -0
  22. data/tests/lib/core_assertions.rb +763 -0
  23. data/tests/lib/envutil.rb +365 -0
  24. data/tests/lib/find_executable.rb +22 -0
  25. data/tests/lib/helper.rb +4 -0
  26. data/tests/ractor_test.rb +30 -0
  27. data/tests/test_helper.rb +3 -3
  28. metadata +27 -48
  29. data/.gitignore +0 -18
  30. data/.travis.yml +0 -24
  31. data/README-json-jruby.md +0 -33
  32. data/Rakefile +0 -413
  33. data/diagrams/.keep +0 -0
  34. data/ext/json/ext/fbuffer/fbuffer.h +0 -187
  35. data/ext/json/ext/generator/depend +0 -1
  36. data/ext/json/ext/generator/extconf.rb +0 -4
  37. data/ext/json/ext/generator/generator.c +0 -1499
  38. data/ext/json/ext/generator/generator.h +0 -171
  39. data/ext/json/ext/parser/depend +0 -1
  40. data/ext/json/ext/parser/extconf.rb +0 -6
  41. data/ext/json/ext/parser/parser.c +0 -2136
  42. data/ext/json/ext/parser/parser.h +0 -91
  43. data/ext/json/ext/parser/parser.rl +0 -896
  44. data/ext/json/extconf.rb +0 -2
  45. data/install.rb +0 -23
  46. data/java/src/json/ext/ByteListTranscoder.java +0 -166
  47. data/java/src/json/ext/Generator.java +0 -466
  48. data/java/src/json/ext/GeneratorMethods.java +0 -231
  49. data/java/src/json/ext/GeneratorService.java +0 -42
  50. data/java/src/json/ext/GeneratorState.java +0 -490
  51. data/java/src/json/ext/OptionsReader.java +0 -113
  52. data/java/src/json/ext/Parser.java +0 -2362
  53. data/java/src/json/ext/Parser.rl +0 -893
  54. data/java/src/json/ext/ParserService.java +0 -34
  55. data/java/src/json/ext/RuntimeInfo.java +0 -116
  56. data/java/src/json/ext/StringDecoder.java +0 -166
  57. data/java/src/json/ext/StringEncoder.java +0 -111
  58. data/java/src/json/ext/Utils.java +0 -88
  59. data/json-java.gemspec +0 -37
  60. data/json.gemspec +0 -0
  61. data/references/rfc7159.txt +0 -899
  62. data/tools/diff.sh +0 -18
  63. data/tools/fuzz.rb +0 -131
  64. data/tools/server.rb +0 -62
@@ -1,2 +0,0 @@
1
- require 'mkmf'
2
- create_makefile('json')
data/install.rb DELETED
@@ -1,23 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'fileutils'
4
- include FileUtils::Verbose
5
- require 'rbconfig'
6
- include\
7
- begin
8
- RbConfig
9
- rescue NameError
10
- Config
11
- end
12
-
13
- sitelibdir = CONFIG["sitelibdir"]
14
- cd 'lib' do
15
- install('json.rb', sitelibdir)
16
- mkdir_p File.join(sitelibdir, 'json')
17
- for file in Dir['json/**/*}']
18
- d = File.join(sitelibdir, file)
19
- mkdir_p File.dirname(d)
20
- install(file, d)
21
- end
22
- end
23
- warn " *** Installed PURE ruby library."
@@ -1,166 +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.exceptions.RaiseException;
9
- import org.jruby.runtime.ThreadContext;
10
- import org.jruby.util.ByteList;
11
-
12
- /**
13
- * A class specialized in transcoding a certain String format into another,
14
- * using UTF-8 ByteLists as both input and output.
15
- */
16
- abstract class ByteListTranscoder {
17
- protected final ThreadContext context;
18
-
19
- protected ByteList src;
20
- protected int srcEnd;
21
- /** Position where the last read character started */
22
- protected int charStart;
23
- /** Position of the next character to read */
24
- protected int pos;
25
-
26
- private ByteList out;
27
- /**
28
- * When a character that can be copied straight into the output is found,
29
- * its index is stored on this variable, and copying is delayed until
30
- * the sequence of characters that can be copied ends.
31
- *
32
- * <p>The variable stores -1 when not in a plain sequence.
33
- */
34
- private int quoteStart = -1;
35
-
36
- protected ByteListTranscoder(ThreadContext context) {
37
- this.context = context;
38
- }
39
-
40
- protected void init(ByteList src, ByteList out) {
41
- this.init(src, 0, src.length(), out);
42
- }
43
-
44
- protected void init(ByteList src, int start, int end, ByteList out) {
45
- this.src = src;
46
- this.pos = start;
47
- this.charStart = start;
48
- this.srcEnd = end;
49
- this.out = out;
50
- }
51
-
52
- /**
53
- * Returns whether there are any characters left to be read.
54
- */
55
- protected boolean hasNext() {
56
- return pos < srcEnd;
57
- }
58
-
59
- /**
60
- * Returns the next character in the buffer.
61
- */
62
- private char next() {
63
- return src.charAt(pos++);
64
- }
65
-
66
- /**
67
- * Reads an UTF-8 character from the input and returns its code point,
68
- * while advancing the input position.
69
- *
70
- * <p>Raises an {@link #invalidUtf8()} exception if an invalid byte
71
- * is found.
72
- */
73
- protected int readUtf8Char() {
74
- charStart = pos;
75
- char head = next();
76
- if (head <= 0x7f) { // 0b0xxxxxxx (ASCII)
77
- return head;
78
- }
79
- if (head <= 0xbf) { // 0b10xxxxxx
80
- throw invalidUtf8(); // tail byte with no head
81
- }
82
- if (head <= 0xdf) { // 0b110xxxxx
83
- ensureMin(1);
84
- int cp = ((head & 0x1f) << 6)
85
- | nextPart();
86
- if (cp < 0x0080) throw invalidUtf8();
87
- return cp;
88
- }
89
- if (head <= 0xef) { // 0b1110xxxx
90
- ensureMin(2);
91
- int cp = ((head & 0x0f) << 12)
92
- | (nextPart() << 6)
93
- | nextPart();
94
- if (cp < 0x0800) throw invalidUtf8();
95
- return cp;
96
- }
97
- if (head <= 0xf7) { // 0b11110xxx
98
- ensureMin(3);
99
- int cp = ((head & 0x07) << 18)
100
- | (nextPart() << 12)
101
- | (nextPart() << 6)
102
- | nextPart();
103
- if (!Character.isValidCodePoint(cp)) throw invalidUtf8();
104
- return cp;
105
- }
106
- // 0b11111xxx?
107
- throw invalidUtf8();
108
- }
109
-
110
- /**
111
- * Throws a GeneratorError if the input list doesn't have at least this
112
- * many bytes left.
113
- */
114
- protected void ensureMin(int n) {
115
- if (pos + n > srcEnd) throw incompleteUtf8();
116
- }
117
-
118
- /**
119
- * Reads the next byte of a multi-byte UTF-8 character and returns its
120
- * contents (lower 6 bits).
121
- *
122
- * <p>Throws a GeneratorError if the byte is not a valid tail.
123
- */
124
- private int nextPart() {
125
- char c = next();
126
- // tail bytes must be 0b10xxxxxx
127
- if ((c & 0xc0) != 0x80) throw invalidUtf8();
128
- return c & 0x3f;
129
- }
130
-
131
-
132
- protected void quoteStart() {
133
- if (quoteStart == -1) quoteStart = charStart;
134
- }
135
-
136
- /**
137
- * When in a sequence of characters that can be copied directly,
138
- * interrupts the sequence and copies it to the output buffer.
139
- *
140
- * @param endPos The offset until which the direct character quoting should
141
- * occur. You may pass {@link #pos} to quote until the most
142
- * recently read character, or {@link #charStart} to quote
143
- * until the character before it.
144
- */
145
- protected void quoteStop(int endPos) {
146
- if (quoteStart != -1) {
147
- out.append(src, quoteStart, endPos - quoteStart);
148
- quoteStart = -1;
149
- }
150
- }
151
-
152
- protected void append(int b) {
153
- out.append(b);
154
- }
155
-
156
- protected void append(byte[] origin, int start, int length) {
157
- out.append(origin, start, length);
158
- }
159
-
160
-
161
- protected abstract RaiseException invalidUtf8();
162
-
163
- protected RaiseException incompleteUtf8() {
164
- return invalidUtf8();
165
- }
166
- }
@@ -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
- }