@@ -97,7 +97,6 @@ VALUE oj_nanosecond_sym;
9797VALUE oj_object_class_sym ;
9898VALUE oj_quirks_mode_sym ;
9999VALUE oj_safe_sym ;
100- VALUE oj_skip_null_byte_sym ;
101100VALUE oj_symbolize_names_sym ;
102101VALUE oj_trace_sym ;
103102
@@ -136,6 +135,7 @@ static VALUE newline_sym;
136135static VALUE nilnil_sym ;
137136static VALUE null_sym ;
138137static VALUE object_sym ;
138+ static VALUE omit_null_byte_sym ;
139139static VALUE omit_nil_sym ;
140140static VALUE rails_sym ;
141141static VALUE raise_sym ;
@@ -208,7 +208,6 @@ struct _options oj_default_options = {
208208 "%0.15g" , // float_fmt
209209 Qnil , // hash_class
210210 Qnil , // array_class
211- No , // skip_null_byte
212211 {
213212 // dump_opts
214213 false, // use
@@ -224,6 +223,7 @@ struct _options oj_default_options = {
224223 0 , // array_size
225224 AutoNan , // nan_dump
226225 false, // omit_nil
226+ false, // omit_null_byte
227227 MAX_DEPTH , // max_depth
228228 },
229229 {
@@ -293,6 +293,7 @@ struct _options oj_default_options = {
293293 *used
294294 * - *:array_class* [_Class_|_nil_] Class to use instead of Array on load
295295 * - *:omit_nil* [_true_|_false_] if true Hash and Object attributes with nil values are omitted
296+ * - *:omit_null_byte* [_true_|_false_] if true null bytes in strings will be omitted when dumping
296297 * - *:ignore* [_nil_|_Array_] either nil or an Array of classes to ignore when dumping
297298 * - *:ignore_under* [_Boolean_] if true then attributes that start with _ are ignored when dumping in
298299 *object or custom mode.
@@ -301,7 +302,6 @@ struct _options oj_default_options = {
301302 * - *:integer_range* [_Range_] Dump integers outside range as strings.
302303 * - *:trace* [_true,_|_false_] Trace all load and dump calls, default is false (trace is off)
303304 * - *:safe* [_true,_|_false_] Safe mimic breaks JSON mimic to be safer, default is false (safe is
304- * - *:skip_null_byte* [_true_|_false_] if true null bytes in strings will be omitted when dumping
305305 *off)
306306 *
307307 * Return [_Hash_] all current option settings.
@@ -387,11 +387,6 @@ static VALUE get_def_opts(VALUE self) {
387387 opts ,
388388 cache_keys_sym ,
389389 (Yes == oj_default_options .cache_keys ) ? Qtrue : ((No == oj_default_options .cache_keys ) ? Qfalse : Qnil ));
390- rb_hash_aset (opts ,
391- oj_skip_null_byte_sym ,
392- (Yes == oj_default_options .skip_null_byte )
393- ? Qtrue
394- : ((No == oj_default_options .skip_null_byte ) ? Qfalse : Qnil ));
395390
396391 switch (oj_default_options .mode ) {
397392 case StrictMode : rb_hash_aset (opts , mode_sym , strict_sym ); break ;
@@ -468,6 +463,7 @@ static VALUE get_def_opts(VALUE self) {
468463 default : rb_hash_aset (opts , nan_sym , auto_sym ); break ;
469464 }
470465 rb_hash_aset (opts , omit_nil_sym , oj_default_options .dump_opts .omit_nil ? Qtrue : Qfalse );
466+ rb_hash_aset (opts , omit_null_byte_sym , oj_default_options .dump_opts .omit_null_byte ? Qtrue : Qfalse );
471467 rb_hash_aset (opts , oj_hash_class_sym , oj_default_options .hash_class );
472468 rb_hash_aset (opts , oj_array_class_sym , oj_default_options .array_class );
473469
@@ -594,7 +590,6 @@ bool set_yesno_options(VALUE key, VALUE value, Options copts) {
594590 {ignore_under_sym , & copts -> ignore_under },
595591 {oj_create_additions_sym , & copts -> create_ok },
596592 {cache_keys_sym , & copts -> cache_keys },
597- {oj_skip_null_byte_sym , & copts -> skip_null_byte },
598593 {Qnil , 0 }};
599594 YesNoOpt o ;
600595
@@ -875,6 +870,17 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
875870 } else {
876871 rb_raise (rb_eArgError , ":omit_nil must be true or false." );
877872 }
873+ } else if (omit_null_byte_sym == k ) {
874+ if (Qnil == v ) {
875+ return ST_CONTINUE ;
876+ }
877+ if (Qtrue == v ) {
878+ copts -> dump_opts .omit_null_byte = true;
879+ } else if (Qfalse == v ) {
880+ copts -> dump_opts .omit_null_byte = false;
881+ } else {
882+ rb_raise (rb_eArgError , ":omit_null_byte must be true or false." );
883+ }
878884 } else if (oj_ascii_only_sym == k ) {
879885 // This is here only for backwards compatibility with the original Oj.
880886 if (Qtrue == v ) {
@@ -1289,6 +1295,7 @@ static VALUE dump(int argc, VALUE *argv, VALUE self) {
12891295 oj_out_init (arg .out );
12901296
12911297 arg .out -> omit_nil = copts .dump_opts .omit_nil ;
1298+ arg .out -> omit_null_byte = copts .dump_opts .omit_null_byte ;
12921299 arg .out -> caller = CALLER_DUMP ;
12931300
12941301 return rb_ensure (dump_body , (VALUE )& arg , dump_ensure , (VALUE )& arg );
@@ -1337,6 +1344,7 @@ static VALUE to_json(int argc, VALUE *argv, VALUE self) {
13371344 oj_out_init (& out );
13381345
13391346 out .omit_nil = copts .dump_opts .omit_nil ;
1347+ out .omit_null_byte = copts .dump_opts .omit_null_byte ;
13401348 // For obj.to_json or generate nan is not allowed but if called from dump
13411349 // it is.
13421350 oj_dump_obj_to_json_using_params (* argv , & copts , & out , argc - 1 , argv + 1 );
@@ -1977,8 +1985,8 @@ void Init_oj(void) {
19771985 rb_gc_register_address (& oj_quirks_mode_sym );
19781986 oj_safe_sym = ID2SYM (rb_intern ("safe" ));
19791987 rb_gc_register_address (& oj_safe_sym );
1980- oj_skip_null_byte_sym = ID2SYM (rb_intern ("skip_null_byte " ));
1981- rb_gc_register_address (& oj_skip_null_byte_sym );
1988+ omit_null_byte_sym = ID2SYM (rb_intern ("omit_null_byte " ));
1989+ rb_gc_register_address (& omit_null_byte_sym );
19821990 oj_space_before_sym = ID2SYM (rb_intern ("space_before" ));
19831991 rb_gc_register_address (& oj_space_before_sym );
19841992 oj_space_sym = ID2SYM (rb_intern ("space" ));
0 commit comments