This repository has been archived by the owner on Feb 5, 2018. It is now read-only.
/
GeneratorMethodsLoader.java
627 lines (564 loc) · 24 KB
/
GeneratorMethodsLoader.java
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
* This code is copyrighted work by Daniel Luz <dev at mernen dot com>.
*
* Distributed under the Ruby and GPLv2 licenses; see COPYING and GPL files
* for details.
*/
package json.ext;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyFixnum;
import org.jruby.RubyFloat;
import org.jruby.RubyHash;
import org.jruby.RubyModule;
import org.jruby.RubyNumeric;
import org.jruby.RubyString;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.callback.Callback;
import org.jruby.util.ByteList;
/**
* A class that populates the <code>Json::Ext::Generator::GeneratorMethods</code>
* module.
*
* @author mernen
*/
class GeneratorMethodsLoader {
private final RubyModule parentModule;
private abstract static class OptionalArgsCallback implements Callback {
public Arity getArity() {
return Arity.OPTIONAL;
}
}
/**
* <code>{@link RubyHash Hash}#to_json(state = nil, depth = 0)</code>
*
* <p>Returns a JSON string containing a JSON object, that is unparsed from
* this Hash instance.
* <p><code>state</code> is a {@link GeneratorState JSON::State} object,
* that can also be used to configure the produced JSON string output further.
* <p><code>depth</code> is used to find the nesting depth, to indent accordingly.
*/
private static Callback HASH_TO_JSON = new OptionalArgsCallback() {
public IRubyObject execute(IRubyObject vSelf, IRubyObject[] args, Block block) {
RubyHash self = vSelf.convertToHash();
Ruby runtime = self.getRuntime();
args = Arity.scanArgs(runtime, args, 0, 2);
IRubyObject vState = args[0];
if (vState.isNil()) {
return simpleTransform(self);
}
else {
GeneratorState state = Utils.ensureState(vState);
int depth;
RubyString result;
if (args[1].isNil()) {
depth = 0;
}
else {
depth = RubyNumeric.fix2int(args[1]);
state.checkMaxNesting(depth + 1);
}
if (state.checkCircular()) {
if (state.hasSeen(self)) {
throw Utils.newException(runtime, Utils.M_CIRCULAR_DATA_STRUCTURE,
"circular data structures not supported!");
}
state.remember(self);
result = transform(self, state, depth);
state.forget(self);
}
else {
result = transform(self, state, depth);
}
return result;
}
}
/**
* Performs a simple Hash-to-JSON conversion, with no customization.
* @param hash The Hash to process
* @return The JSON representation of the Hash
*/
private RubyString simpleTransform(RubyHash self) {
final int preSize = 2 + Math.max(self.size() * 12, 0);
final RubyString result =
self.getRuntime().newString(new ByteList(preSize));
result.cat((byte)'{');
self.visitAll(new RubyHash.Visitor() {
private boolean firstPair = true;
@Override
public void visit(IRubyObject key, IRubyObject value) {
// XXX key == Qundef???
if (firstPair) {
firstPair = false;
}
else {
result.cat((byte)',');
}
RubyString jsonKey = Utils.toJson(key.asString());
result.cat(jsonKey.getByteList());
result.infectBy(jsonKey);
result.cat((byte)':');
RubyString jsonValue = Utils.toJson(value);
result.cat(jsonValue.getByteList());
result.infectBy(jsonValue);
}
});
result.cat((byte)'}');
return result;
}
private RubyString transform(RubyHash self, final GeneratorState state, int depth) {
final Ruby runtime = self.getRuntime();
final ByteList objectNl = state.object_nl_get().getByteList();
final byte[] indent = Utils.repeat(state.indent_get().getByteList(), depth + 1);
final ByteList spaceBefore = state.space_before_get().getByteList();
final ByteList space = state.space_get().getByteList();
final RubyFixnum subDepth = runtime.newFixnum(depth + 1);
final int preSize = 2 + self.size() * (12 + indent.length + spaceBefore.length() + space.length());
final RubyString result = runtime.newString(new ByteList(preSize));
result.cat((byte)'{');
result.cat(objectNl);
self.visitAll(new RubyHash.Visitor() {
private boolean firstPair = true;
@Override
public void visit(IRubyObject key, IRubyObject value) {
// XXX key == Qundef???
if (firstPair) {
firstPair = false;
}
else {
result.cat((byte)',');
result.cat(objectNl);
}
if (objectNl.length() != 0) result.cat(indent);
RubyString keyJson = Utils.toJson(key.asString(), state, subDepth);
result.cat(keyJson.getByteList());
result.infectBy(keyJson);
result.cat(spaceBefore);
result.cat((byte)':');
result.cat(space);
RubyString valueJson = Utils.toJson(value, state, subDepth);
result.cat(valueJson.getByteList());
result.infectBy(valueJson);
}
});
if (objectNl.length() != 0) {
result.cat(objectNl);
if (indent.length != 0) {
for (int i = 0; i < depth; i++) {
result.cat(indent);
}
}
}
result.cat((byte)'}');
return result;
}
};
/**
* <code>{@link RubyArray Array}#to_json(state = nil, depth = 0)</code>
*
* <p>Returns a JSON string containing a JSON array, that is unparsed from
* this Array instance.
* <p><code>state</code> is a {@link GeneratorState JSON::State} object,
* that can also be used to configure the produced JSON string output further.
* <p><code>depth</code> is used to find the nesting depth, to indent accordingly.
*/
private static Callback ARRAY_TO_JSON = new OptionalArgsCallback() {
public IRubyObject execute(IRubyObject vSelf, IRubyObject[] args, Block block) {
RubyArray self = Utils.ensureArray(vSelf);
Ruby runtime = self.getRuntime();
args = Arity.scanArgs(runtime, args, 0, 2);
IRubyObject state = args[0];
IRubyObject vDepth = args[1];
RubyString result;
if (state.isNil()) {
int preSize = 2 + Math.max(self.size() * 4, 0);
result = runtime.newString(new ByteList(preSize));
result.cat((byte)'[');
result.infectBy(vSelf);
for (int i = 0, t = self.getLength(); i < t; i++) {
IRubyObject element = self.eltInternal(i);
result.infectBy(element);
if (i > 0) {
result.cat((byte)',');
}
RubyString elementStr = Utils.toJson(element);
result.append(elementStr);
}
result.cat((byte)']');
}
else {
int depth = vDepth.isNil() ? 0 : RubyNumeric.fix2int(vDepth);
result = transform(self, Utils.ensureState(state), depth);
}
result.infectBy(vSelf);
return result;
}
private RubyString transform(RubyArray self, GeneratorState state, int depth) {
final Ruby runtime = self.getRuntime();
final int preSize = 2 + Math.max(self.size() * 4, 0);
final RubyString result = runtime.newString(new ByteList(preSize));
ByteList indentUnit = state.indent_get().getByteList();
byte[] shift = Utils.repeat(indentUnit, depth + 1);
result.infectBy(self);
ByteList arrayNl = state.array_nl_get().getByteList();
byte[] delim = new byte[1 + arrayNl.length()];
delim[0] = ',';
System.arraycopy(arrayNl.unsafeBytes(), arrayNl.begin(), delim, 1, arrayNl.length());
state.checkMaxNesting(depth + 1);
if (state.checkCircular()) {
state.remember(self);
result.cat((byte)'[');
result.cat(arrayNl);
boolean firstItem = true;
for (int i = 0, t = self.getLength(); i < t; i++) {
IRubyObject element = self.eltInternal(i);
if (state.hasSeen(element)) {
throw Utils.newException(runtime, Utils.M_CIRCULAR_DATA_STRUCTURE,
"circular data structures not supported!");
}
result.infectBy(element);
if (firstItem) {
firstItem = false;
}
else {
result.cat(delim);
}
result.cat(shift);
RubyString elemJson = Utils.toJson(element, state, RubyNumeric.int2fix(runtime, depth + 1));
result.cat(elemJson.getByteList());
}
if (arrayNl.length() != 0) {
result.cat(arrayNl);
result.cat(shift, 0, depth * indentUnit.length());
}
result.cat((byte)']');
state.forget(self);
}
else {
result.cat((byte)'[');
result.cat(arrayNl);
boolean firstItem = true;
for (int i = 0, t = self.getLength(); i < t; i++) {
IRubyObject element = self.eltInternal(i);
result.infectBy(element);
if (firstItem) {
firstItem = false;
}
else {
result.cat(delim);
}
result.cat(shift);
RubyString elemJson = Utils.toJson(element, state, RubyNumeric.int2fix(runtime, depth + 1));
result.cat(elemJson.getByteList());
}
if (arrayNl.length() != 0) {
result.cat(arrayNl);
result.cat(shift, 0, depth * indentUnit.length());
}
result.cat((byte)']');
}
return result;
}
};
/**
* <code>{@link RubyInteger Integer}#to_json(*)</code>
*
* <p>Returns a JSON string representation for this Integer number.
*/
private static Callback INTEGER_TO_JSON = new OptionalArgsCallback() {
public IRubyObject execute(IRubyObject recv, IRubyObject[] args, Block block) {
return recv.callMethod(recv.getRuntime().getCurrentContext(), "to_s");
}
};
/**
* <code>{@link RubyFloat Float}#to_json(state = nil, *)</code>
*
* <p>Returns a JSON string representation for this Float number.
* <p><code>state</code> is a {@link GeneratorState JSON::State} object,
* that can also be used to configure the produced JSON string output further.
*/
private static Callback FLOAT_TO_JSON = new OptionalArgsCallback() {
public IRubyObject execute(IRubyObject vSelf, IRubyObject[] args, Block block) {
double value = RubyFloat.num2dbl(vSelf);
if (Double.isInfinite(value) || Double.isNaN(value)) {
GeneratorState state = args.length > 0 ? Utils.ensureState(args[0]) : null;
if (state == null || state.allowNaN()) {
return vSelf.asString();
}
else {
throw Utils.newException(vSelf.getRuntime(), Utils.M_GENERATOR_ERROR,
vSelf + " not allowed in JSON");
}
}
else {
return vSelf.asString();
}
}
};
/**
* <code>{@link RubyString String}#to_json(*)</code>
*
* <p>Returns a JSON string representation for this String.
* <p>The string must be encoded in UTF-8. All non-ASCII characters will be
* escaped as <code>\\u????</code> escape sequences. Characters outside the
* Basic Multilingual Plane range are encoded as a pair of surrogates.
*/
private static Callback STRING_TO_JSON = new OptionalArgsCallback() {
public IRubyObject execute(IRubyObject vSelf, IRubyObject[] args, Block block) {
// using convertToString as a safety guard measure
RubyString self = vSelf.convertToString();
char[] chars = decodeString(self);
int preSize = 2 + self.getByteList().length();
RubyString result = self.getRuntime().newString(new ByteList(preSize));
result.cat((byte)'"');
final byte[] escapeSequence = new byte[] { '\\', 0 };
for (char c : chars) {
switch (c) {
case '"':
case '\\':
escapeSequence[1] = (byte)c;
break;
case '\n':
escapeSequence[1] = 'n';
break;
case '\r':
escapeSequence[1] = 'r';
break;
case '\t':
escapeSequence[1] = 't';
break;
case '\f':
escapeSequence[1] = 'f';
break;
case '\b':
escapeSequence[1] = 'b';
break;
default:
if (c >= 0x20 && c <= 0x7f) {
result.cat((byte)c);
}
else {
result.cat(Utils.escapeUnicode(c));
}
continue;
}
result.cat(escapeSequence);
}
result.cat((byte)'"');
return result;
}
private char[] decodeString(RubyString string) {
ByteList byteList = string.getByteList();
try { // attempt to interpret string as UTF-8
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
ByteBuffer byteBuffer = ByteBuffer.wrap(byteList.unsafeBytes(), byteList.begin(), byteList.length());
CharBuffer buffer = decoder.decode(byteBuffer);
char[] result = new char[buffer.length()];
System.arraycopy(buffer.array(), buffer.position(), result, 0, result.length);
return result;
} catch (CharacterCodingException e) {
// XXX DISABLED: Florian's library strictly only interprets UTF-8
/*
// a very naïve decoder, which just maps bytes
// XXX is this *really* equivalent to the ISO-8859-1 decoder?
char[] chars = new char[bytes.length];
for (int i = 0; i < bytes.length; i++) {
chars[i] = (char)(bytes[i] & 0xff);
}
return chars;
*/
throw Utils.newException(string.getRuntime(), Utils.M_GENERATOR_ERROR,
"source sequence is illegal/malformed");
}
}
};
/**
* <code>{@link RubyString String}#to_json_raw(*)</code>
*
* <p>This method creates a JSON text from the result of a call to
* {@link STRING_TO_JSON_RAW_OBJECT to_json_raw_object} of this String.
*/
private static Callback STRING_TO_JSON_RAW = new OptionalArgsCallback() {
public IRubyObject execute(IRubyObject vSelf, IRubyObject[] args, Block block) {
IRubyObject obj = STRING_TO_JSON_RAW_OBJECT.execute(vSelf, args, block);
return HASH_TO_JSON.execute(obj, args, block);
}
};
/**
* <code>{@link RubyString String}#to_json_raw_object(*)</code>
*
* <p>This method creates a raw object Hash, that can be nested into other
* data structures and will be unparsed as a raw string. This method should
* be used if you want to convert raw strings to JSON instead of UTF-8
* strings, e.g. binary data.
*/
private static Callback STRING_TO_JSON_RAW_OBJECT = new OptionalArgsCallback() {
public IRubyObject execute(IRubyObject vSelf, IRubyObject[] args, Block block) {
RubyString self = vSelf.convertToString();
Ruby runtime = self.getRuntime();
RubyHash result = RubyHash.newHash(runtime);
IRubyObject createId =
runtime.getModule("JSON").callMethod(runtime.getCurrentContext(), "create_id");
result.op_aset(createId, vSelf.getMetaClass().to_s());
ByteList bl = self.getByteList();
byte[] uBytes = bl.unsafeBytes();
RubyArray array = runtime.newArray(bl.length());
for (int i = bl.begin(), t = bl.begin() + bl.length(); i < t; i++) {
array.store(i, runtime.newFixnum(uBytes[i] & 0xff));
}
result.op_aset(runtime.newString("raw"), array);
return result;
}
};
/**
* <code>{@link RubyString String}#json_create(o)</code>
*
* <p>Raw Strings are JSON Objects (the raw bytes are stored in an array for
* the key "raw"). The Ruby String can be created by this module method.
*/
private static Callback stringExtendJsonCreate = new Callback() {
public Arity getArity() {
return Arity.ONE_ARGUMENT;
}
public IRubyObject execute(IRubyObject vSelf, IRubyObject[] args, Block block) {
Ruby runtime = vSelf.getRuntime();
RubyHash o = args[0].convertToHash();
IRubyObject rawData = o.fastARef(runtime.newString("raw"));
if (rawData == null) {
throw runtime.newArgumentError("\"raw\" value not defined for encoded String");
}
RubyArray ary = Utils.ensureArray(rawData);
byte[] bytes = new byte[ary.getLength()];
for (int i = 0, t = ary.getLength(); i < t; i++) {
IRubyObject element = ary.eltInternal(i);
if (element instanceof RubyFixnum) {
bytes[i] = (byte)RubyNumeric.fix2long(element);
}
else {
throw runtime.newTypeError(element, runtime.getFixnum());
}
}
return runtime.newString(new ByteList(bytes, false));
}
};
/**
* A general converter for keyword values
* (<code>true</code>, <code>false</code>, <code>null</code>).
* @author mernen
*/
private static class KeywordJsonConverter extends OptionalArgsCallback {
// Store keyword as a shared ByteList for performance.
private final ByteList keyword;
private KeywordJsonConverter(String keyword) {
super();
this.keyword = new ByteList(ByteList.plain(keyword), false);
}
public IRubyObject execute(IRubyObject self, IRubyObject[] args, Block block) {
return RubyString.newStringShared(self.getRuntime(), keyword);
}
}
/**
* <code>true.to_json(*)</code>
*
* <p>Returns a JSON string for <code>true</code>: <code>"true"</code>.
*/
private static Callback trueToJson = new KeywordJsonConverter("true");
/**
* <code>false.to_json(*)</code>
*
* <p>Returns a JSON string for <code>false</code>: <code>"false"</code>.
*/
private static Callback falseToJson = new KeywordJsonConverter("false");
/**
* <code>nil.to_json(*)</code>
*
* <p>Returns a JSON string for <code>nil</code>: <code>"null"</code>.
*/
private static Callback nilToJson = new KeywordJsonConverter("null");
/**
* <code>{@link RubyObject Object}#to_json(*)</code>
*
* <p>Converts this object to a string (calling <code>#to_s</code>),
* converts it to a JSON string, and returns the result.
* This is a fallback, if no special method <code>#to_json</code> was
* defined for some object.
*/
private static Callback OBJECT_TO_JSON = new OptionalArgsCallback() {
public IRubyObject execute(IRubyObject recv, IRubyObject[] args, Block block) {
return STRING_TO_JSON.execute(recv.asString(), args, block);
}
};
/**
* Instantiates the RubyModule element.
* @param generatorMethodsModule The module to populate
* (normally <code>JSON::Generator::GeneratorMethods</code>)
*/
GeneratorMethodsLoader(RubyModule generatorMethodsModule) {
this.parentModule = generatorMethodsModule;
}
/**
* Performs the generation of all submodules and methods.
*/
void load() {
defineToJson("Object", OBJECT_TO_JSON);
defineToJson("Hash", HASH_TO_JSON);
defineToJson("Array", ARRAY_TO_JSON);
defineToJson("Integer", INTEGER_TO_JSON);
defineToJson("Float", FLOAT_TO_JSON);
defineToJson("String", STRING_TO_JSON);
defineMethod("String", "to_json_raw", STRING_TO_JSON_RAW);
defineMethod("String", "to_json_raw_object", STRING_TO_JSON_RAW_OBJECT);
RubyModule stringModule = parentModule.defineModuleUnder("String");
final RubyModule stringExtend = stringModule.defineModuleUnder("Extend");
stringModule.defineModuleFunction("included", new Callback() {
public Arity getArity() {
return Arity.ONE_ARGUMENT;
}
public IRubyObject execute(IRubyObject vSelf, IRubyObject[] args, Block block) {
ThreadContext context = vSelf.getRuntime().getCurrentContext();
return args[0].callMethod(context, "extend", stringExtend);
}
});
defineMethod(stringExtend, "json_create", stringExtendJsonCreate);
defineToJson("TrueClass", trueToJson);
defineToJson("FalseClass", falseToJson);
defineToJson("NilClass", nilToJson);
}
/**
* Convenience method for defining "to_json" on a module.
* @param moduleName
* @param method
*/
private void defineToJson(String moduleName, Callback method) {
defineMethod(moduleName, "to_json", method);
}
/**
* Convenience method for defining arbitrary methods on a module (by name).
* @param moduleName
* @param methodName
* @param method
*/
private void defineMethod(String moduleName, String methodName, Callback method) {
defineMethod(parentModule.defineModuleUnder(moduleName), methodName, method);
}
/**
* Convenience methods for defining arbitrary methods on a module (by reference).
* @param module
* @param methodName
* @param method
*/
private void defineMethod(RubyModule module, String methodName, Callback method) {
module.defineMethod(methodName, method);
}
}