@@ -83,6 +83,12 @@ public class GeneratorState extends RubyObject {
83
83
*/
84
84
private boolean quirksMode = DEFAULT_QUIRKS_MODE ;
85
85
static final boolean DEFAULT_QUIRKS_MODE = false ;
86
+ /**
87
+ * If set to <code>true</code> the forward slash will be escaped in
88
+ * json output.
89
+ */
90
+ private boolean escapeSlash = DEFAULT_ESCAPE_SLASH ;
91
+ static final boolean DEFAULT_ESCAPE_SLASH = false ;
86
92
/**
87
93
* The initial buffer length of this state. (This isn't really used on all
88
94
* non-C implementations.)
@@ -172,6 +178,9 @@ static GeneratorState fromState(ThreadContext context, RuntimeInfo info,
172
178
* <code>-Infinity</code> should be generated, otherwise an exception is
173
179
* thrown if these values are encountered.
174
180
* This options defaults to <code>false</code>.
181
+ * <dt><code>:escape_slash</code>
182
+ * <dd>set to <code>true</code> if the forward slashes should be escaped
183
+ * in the json output (default: <code>false</code>)
175
184
*/
176
185
@ JRubyMethod (optional =1 , visibility =Visibility .PRIVATE )
177
186
public IRubyObject initialize (ThreadContext context , IRubyObject [] args ) {
@@ -195,6 +204,7 @@ public IRubyObject initialize_copy(ThreadContext context, IRubyObject vOrig) {
195
204
this .allowNaN = orig .allowNaN ;
196
205
this .asciiOnly = orig .asciiOnly ;
197
206
this .quirksMode = orig .quirksMode ;
207
+ this .escapeSlash = orig .escapeSlash ;
198
208
this .bufferInitialLength = orig .bufferInitialLength ;
199
209
this .depth = orig .depth ;
200
210
return this ;
@@ -381,6 +391,24 @@ public IRubyObject max_nesting_set(IRubyObject max_nesting) {
381
391
return max_nesting ;
382
392
}
383
393
394
+ /**
395
+ * Returns true if forward slashes are escaped in the json output.
396
+ */
397
+ public int getEscapeSlash () {
398
+ return escapeSlash ;
399
+ }
400
+
401
+ @ JRubyMethod (name ="escape_slash" )
402
+ public RubyBoolean escape_slash_get (ThreadContext context ) {
403
+ return context .getRuntime ().newBoolean (escapeSlash );
404
+ }
405
+
406
+ @ JRubyMethod (name ="escape_slash=" )
407
+ public IRubyObject escape_slash_set (IRubyObject escape_slash ) {
408
+ escapeSlash = escape_slash .isTrue ();
409
+ return escape_slash .getRuntime ().newBoolean (escapeSlash );
410
+ }
411
+
384
412
public boolean allowNaN () {
385
413
return allowNaN ;
386
414
}
@@ -482,6 +510,7 @@ public IRubyObject configure(ThreadContext context, IRubyObject vOpts) {
482
510
allowNaN = opts .getBool ("allow_nan" , DEFAULT_ALLOW_NAN );
483
511
asciiOnly = opts .getBool ("ascii_only" , DEFAULT_ASCII_ONLY );
484
512
quirksMode = opts .getBool ("quirks_mode" , DEFAULT_QUIRKS_MODE );
513
+ escapeSlash = opts .getBool ("escape_slash" , DEFAULT_ESCAPE_SLASH );
485
514
bufferInitialLength = opts .getInt ("buffer_initial_length" , DEFAULT_BUFFER_INITIAL_LENGTH );
486
515
487
516
depth = opts .getInt ("depth" , 0 );
@@ -510,6 +539,7 @@ public RubyHash to_h(ThreadContext context) {
510
539
result .op_aset (context , runtime .newSymbol ("ascii_only" ), ascii_only_p (context ));
511
540
result .op_aset (context , runtime .newSymbol ("quirks_mode" ), quirks_mode_p (context ));
512
541
result .op_aset (context , runtime .newSymbol ("max_nesting" ), max_nesting_get (context ));
542
+ result .op_aset (context , runtime .newSymbol ("escape_slash" ), escape_slash_get (context ));
513
543
result .op_aset (context , runtime .newSymbol ("depth" ), depth_get (context ));
514
544
result .op_aset (context , runtime .newSymbol ("buffer_initial_length" ), buffer_initial_length_get (context ));
515
545
for (String name : getInstanceVariableNameList ()) {
0 commit comments