@@ -16,7 +16,7 @@ static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
1616 i_object_nl , i_array_nl , i_max_nesting , i_allow_nan , i_ascii_only ,
1717 i_pack , i_unpack , i_create_id , i_extend , i_key_p ,
1818 i_aref , i_send , i_respond_to_p , i_match , i_keys , i_depth ,
19- i_buffer_initial_length , i_dup , i_script_safe , i_escape_slash ;
19+ i_buffer_initial_length , i_dup , i_script_safe , i_escape_slash , i_strict ;
2020
2121/*
2222 * Copyright 2001-2004 Unicode, Inc.
@@ -749,6 +749,8 @@ static VALUE cState_configure(VALUE self, VALUE opts)
749749 tmp = rb_hash_aref (opts , ID2SYM (i_escape_slash ));
750750 state -> script_safe = RTEST (tmp );
751751 }
752+ tmp = rb_hash_aref (opts , ID2SYM (i_strict ));
753+ state -> strict = RTEST (tmp );
752754 return self ;
753755}
754756
@@ -784,6 +786,7 @@ static VALUE cState_to_h(VALUE self)
784786 rb_hash_aset (result , ID2SYM (i_ascii_only ), state -> ascii_only ? Qtrue : Qfalse );
785787 rb_hash_aset (result , ID2SYM (i_max_nesting ), LONG2FIX (state -> max_nesting ));
786788 rb_hash_aset (result , ID2SYM (i_script_safe ), state -> script_safe ? Qtrue : Qfalse );
789+ rb_hash_aset (result , ID2SYM (i_strict ), state -> strict ? Qtrue : Qfalse );
787790 rb_hash_aset (result , ID2SYM (i_depth ), LONG2FIX (state -> depth ));
788791 rb_hash_aset (result , ID2SYM (i_buffer_initial_length ), LONG2FIX (state -> buffer_initial_length ));
789792 return result ;
@@ -1049,6 +1052,8 @@ static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *s
10491052 generate_json_bignum (buffer , Vstate , state , obj );
10501053 } else if (klass == rb_cFloat ) {
10511054 generate_json_float (buffer , Vstate , state , obj );
1055+ } else if (state -> strict ) {
1056+ rb_raise (eGeneratorError , "%" PRIsVALUE " not allowed in JSON" , RB_OBJ_STRING (CLASS_OF (obj )));
10521057 } else if (rb_respond_to (obj , i_to_json )) {
10531058 tmp = rb_funcall (obj , i_to_json , 1 , Vstate );
10541059 Check_Type (tmp , T_STRING );
@@ -1423,7 +1428,7 @@ static VALUE cState_script_safe(VALUE self)
14231428}
14241429
14251430/*
1426- * call-seq: script_safe=(depth )
1431+ * call-seq: script_safe=(enable )
14271432 *
14281433 * This sets whether or not the forward slashes will be escaped in
14291434 * the json output.
@@ -1435,6 +1440,37 @@ static VALUE cState_script_safe_set(VALUE self, VALUE enable)
14351440 return Qnil ;
14361441}
14371442
1443+ /*
1444+ * call-seq: strict
1445+ *
1446+ * If this boolean is false, types unsupported by the JSON format will
1447+ * be serialized as strings.
1448+ * If this boolean is true, types unsupported by the JSON format will
1449+ * raise a JSON::GeneratorError.
1450+ */
1451+ static VALUE cState_strict (VALUE self )
1452+ {
1453+ GET_STATE (self );
1454+ return state -> strict ? Qtrue : Qfalse ;
1455+ }
1456+
1457+ /*
1458+ * call-seq: strict=(enable)
1459+ *
1460+ * This sets whether or not to serialize types unsupported by the
1461+ * JSON format as strings.
1462+ * If this boolean is false, types unsupported by the JSON format will
1463+ * be serialized as strings.
1464+ * If this boolean is true, types unsupported by the JSON format will
1465+ * raise a JSON::GeneratorError.
1466+ */
1467+ static VALUE cState_strict_set (VALUE self , VALUE enable )
1468+ {
1469+ GET_STATE (self );
1470+ state -> strict = RTEST (enable );
1471+ return Qnil ;
1472+ }
1473+
14381474/*
14391475 * call-seq: allow_nan?
14401476 *
@@ -1557,6 +1593,9 @@ void Init_generator(void)
15571593 rb_define_alias (cState , "escape_slash" , "script_safe" );
15581594 rb_define_alias (cState , "escape_slash?" , "script_safe?" );
15591595 rb_define_alias (cState , "escape_slash=" , "script_safe=" );
1596+ rb_define_method (cState , "strict" , cState_strict , 0 );
1597+ rb_define_method (cState , "strict?" , cState_strict , 0 );
1598+ rb_define_method (cState , "strict=" , cState_strict_set , 1 );
15601599 rb_define_method (cState , "check_circular?" , cState_check_circular_p , 0 );
15611600 rb_define_method (cState , "allow_nan?" , cState_allow_nan_p , 0 );
15621601 rb_define_method (cState , "ascii_only?" , cState_ascii_only_p , 0 );
@@ -1615,6 +1654,7 @@ void Init_generator(void)
16151654 i_max_nesting = rb_intern ("max_nesting" );
16161655 i_script_safe = rb_intern ("script_safe" );
16171656 i_escape_slash = rb_intern ("escape_slash" );
1657+ i_strict = rb_intern ("strict" );
16181658 i_allow_nan = rb_intern ("allow_nan" );
16191659 i_ascii_only = rb_intern ("ascii_only" );
16201660 i_depth = rb_intern ("depth" );
0 commit comments