Method: Oj.default_options

Defined in:
ext/oj/oj.c

.default_optionsObject

Returns the default load and dump options as a Hash. The options are

  • :indent [Fixnum|String|nil] number of spaces to indent each element in an JSON document, zero or nil is no newline between JSON elements, negative indicates no newline between top level JSON elements in a stream, a String indicates the string should be used for indentation

  • :circular [Boolean|nil] support circular references while dumping as well as shared references

  • :auto_define [Boolean|nil] automatically define classes if they do not exist

  • :symbol_keys [Boolean|nil] use symbols instead of strings for hash keys

  • :escape_mode [:newline|:json|:slash|:xss_safe|:ascii|:unicode_xss|nil] determines the characters to escape

  • :class_cache [Boolean|nil] cache classes for faster parsing (if dynamically modifying classes or reloading classes then don’t use this)

  • :mode [:object|:strict|:compat|:null|:custom|:rails|:wab] load and dump modes to use for JSON

  • :time_format [:unix|:unix_zone|:xmlschema|:ruby] time format when dumping

  • :bigdecimal_as_decimal [Boolean|nil] dump BigDecimal as a decimal number or as a String

  • :bigdecimal_load [:bigdecimal|:float|:auto|:fast|:ruby] load decimals as BigDecimal instead of as a Float. :auto pick the most precise for the number of digits. :float should be the same as ruby. :fast may require rounding but is must faster.

  • :compat_bigdecimal [true|false] load decimals as BigDecimal instead of as a Float when in compat or rails mode.

  • :create_id [String|nil] create id for json compatible object encoding, default is ‘json_class’

  • :create_additions [Boolean|nil] if true allow creation of instances using create_id on load.

  • :second_precision [Fixnum|nil] number of digits after the decimal when dumping the seconds portion of time

  • :float_precision [Fixnum|nil] number of digits of precision when dumping floats, 0 indicates use Ruby

  • :float_format [String] the C printf format string for printing floats. Default follows the float_precision and will be changed if float_precision is changed. The string can be no more than 6 bytes.

  • :use_to_json [Boolean|nil] call to_json() methods on dump, default is false

  • :use_as_json [Boolean|nil] call as_json() methods on dump, default is false

  • :use_raw_json [Boolean|nil] call raw_json() methods on dump, default is false

  • :nilnil [Boolean|nil] if true a nil input to load will return nil and not raise an Exception

  • :empty_string [Boolean|nil] if true an empty input will not raise an Exception

  • :allow_gc [Boolean|nil] allow or prohibit GC during parsing, default is true (allow)

  • :quirks_mode [true,|false|nil] Allow single JSON values instead of documents, default is true (allow)

  • :allow_invalid_unicode [true,|false|nil] Allow invalid unicode, default is false (don’t allow)

  • :allow_nan [true,|false|nil] Allow Nan, Infinity, and -Infinity to be parsed, default is true (allow)

  • :indent_str [String|nil] String to use for indentation, overriding the indent option is not nil

  • :space [String|nil] String to use for the space after the colon in JSON object fields

  • :space_before [String|nil] String to use before the colon separator in JSON object fields

  • :object_nl [String|nil] String to use after a JSON object field value

  • :array_nl [String|nil] String to use after a JSON array value

  • :nan [:null|:huge|:word|:raise|:auto] how to dump Infinity and NaN. :null places a null, :huge places a huge number, :word places Infinity or NaN, :raise raises and exception, :auto uses default for each mode.

  • :hash_class [Class|nil] Class to use instead of Hash on load, :object_class can also be used

  • :array_class [Class|nil] Class to use instead of Array on load

  • :omit_nil [true|false] if true Hash and Object attributes with nil values are omitted

  • :omit_null_byte [true|false] if true null bytes in strings will be omitted when dumping

  • :ignore [nil|Array] either nil or an Array of classes to ignore when dumping

  • :ignore_under [Boolean] if true then attributes that start with _ are ignored when dumping in object or custom mode.

  • :cache_keys [Boolean] if true then hash keys are cached if less than 35 bytes.

  • :cache_str [Fixnum] maximum string value length to cache (strings less than this are cached)

  • :integer_range [Range] Dump integers outside range as strings.

  • :trace [true,|false] Trace all load and dump calls, default is false (trace is off)

  • :safe [true,|false] Safe mimic breaks JSON mimic to be safer, default is false (safe is off)

Return [Hash] all current option settings.



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
# File 'ext/oj/oj.c', line 320

static VALUE get_def_opts(VALUE self) {
    VALUE opts = rb_hash_new();

    if (0 == oj_default_options.dump_opts.indent_size) {
        rb_hash_aset(opts, oj_indent_sym, INT2FIX(oj_default_options.indent));
    } else {
        rb_hash_aset(opts, oj_indent_sym, rb_str_new2(oj_default_options.dump_opts.indent_str));
    }
    rb_hash_aset(opts, sec_prec_sym, INT2FIX(oj_default_options.sec_prec));
    rb_hash_aset(opts,
                 circular_sym,
                 (Yes == oj_default_options.circular) ? Qtrue : ((No == oj_default_options.circular) ? Qfalse : Qnil));
    rb_hash_aset(
        opts,
        class_cache_sym,
        (Yes == oj_default_options.class_cache) ? Qtrue : ((No == oj_default_options.class_cache) ? Qfalse : Qnil));
    rb_hash_aset(
        opts,
        auto_define_sym,
        (Yes == oj_default_options.auto_define) ? Qtrue : ((No == oj_default_options.auto_define) ? Qfalse : Qnil));
    rb_hash_aset(opts,
                 symbol_keys_sym,
                 (Yes == oj_default_options.sym_key) ? Qtrue : ((No == oj_default_options.sym_key) ? Qfalse : Qnil));
    rb_hash_aset(
        opts,
        bigdecimal_as_decimal_sym,
        (Yes == oj_default_options.bigdec_as_num) ? Qtrue : ((No == oj_default_options.bigdec_as_num) ? Qfalse : Qnil));
    rb_hash_aset(
        opts,
        oj_create_additions_sym,
        (Yes == oj_default_options.create_ok) ? Qtrue : ((No == oj_default_options.create_ok) ? Qfalse : Qnil));
    rb_hash_aset(opts,
                 use_to_json_sym,
                 (Yes == oj_default_options.to_json) ? Qtrue : ((No == oj_default_options.to_json) ? Qfalse : Qnil));
    rb_hash_aset(opts,
                 use_to_hash_sym,
                 (Yes == oj_default_options.to_hash) ? Qtrue : ((No == oj_default_options.to_hash) ? Qfalse : Qnil));
    rb_hash_aset(opts,
                 use_as_json_sym,
                 (Yes == oj_default_options.as_json) ? Qtrue : ((No == oj_default_options.as_json) ? Qfalse : Qnil));
    rb_hash_aset(opts,
                 use_raw_json_sym,
                 (Yes == oj_default_options.raw_json) ? Qtrue : ((No == oj_default_options.raw_json) ? Qfalse : Qnil));
    rb_hash_aset(opts,
                 nilnil_sym,
                 (Yes == oj_default_options.nilnil) ? Qtrue : ((No == oj_default_options.nilnil) ? Qfalse : Qnil));
    rb_hash_aset(
        opts,
        empty_string_sym,
        (Yes == oj_default_options.empty_string) ? Qtrue : ((No == oj_default_options.empty_string) ? Qfalse : Qnil));
    rb_hash_aset(opts,
                 allow_gc_sym,
                 (Yes == oj_default_options.allow_gc) ? Qtrue : ((No == oj_default_options.allow_gc) ? Qfalse : Qnil));
    rb_hash_aset(
        opts,
        oj_quirks_mode_sym,
        (Yes == oj_default_options.quirks_mode) ? Qtrue : ((No == oj_default_options.quirks_mode) ? Qfalse : Qnil));
    rb_hash_aset(
        opts,
        allow_invalid_unicode_sym,
        (Yes == oj_default_options.allow_invalid) ? Qtrue : ((No == oj_default_options.allow_invalid) ? Qfalse : Qnil));
    rb_hash_aset(
        opts,
        oj_allow_nan_sym,
        (Yes == oj_default_options.allow_nan) ? Qtrue : ((No == oj_default_options.allow_nan) ? Qfalse : Qnil));
    rb_hash_aset(opts,
                 oj_trace_sym,
                 (Yes == oj_default_options.trace) ? Qtrue : ((No == oj_default_options.trace) ? Qfalse : Qnil));
    rb_hash_aset(opts,
                 oj_safe_sym,
                 (Yes == oj_default_options.safe) ? Qtrue : ((No == oj_default_options.safe) ? Qfalse : Qnil));
    rb_hash_aset(opts, float_prec_sym, INT2FIX(oj_default_options.float_prec));
    rb_hash_aset(opts, float_format_sym, rb_str_new_cstr(oj_default_options.float_fmt));
    rb_hash_aset(opts, cache_str_sym, INT2FIX(oj_default_options.cache_str));
    rb_hash_aset(
        opts,
        ignore_under_sym,
        (Yes == oj_default_options.ignore_under) ? Qtrue : ((No == oj_default_options.ignore_under) ? Qfalse : Qnil));
    rb_hash_aset(
        opts,
        cache_keys_sym,
        (Yes == oj_default_options.cache_keys) ? Qtrue : ((No == oj_default_options.cache_keys) ? Qfalse : Qnil));

    switch (oj_default_options.mode) {
    case StrictMode: rb_hash_aset(opts, mode_sym, strict_sym); break;
    case CompatMode: rb_hash_aset(opts, mode_sym, compat_sym); break;
    case NullMode: rb_hash_aset(opts, mode_sym, null_sym); break;
    case ObjectMode: rb_hash_aset(opts, mode_sym, object_sym); break;
    case CustomMode: rb_hash_aset(opts, mode_sym, custom_sym); break;
    case RailsMode: rb_hash_aset(opts, mode_sym, rails_sym); break;
    case WabMode: rb_hash_aset(opts, mode_sym, wab_sym); break;
    default: rb_hash_aset(opts, mode_sym, object_sym); break;
    }

    if (oj_default_options.int_range_max != 0 || oj_default_options.int_range_min != 0) {
        VALUE range = rb_obj_alloc(rb_cRange);
        VALUE min   = LONG2FIX(oj_default_options.int_range_min);
        VALUE max   = LONG2FIX(oj_default_options.int_range_max);

        rb_ivar_set(range, oj_begin_id, min);
        rb_ivar_set(range, oj_end_id, max);
        rb_hash_aset(opts, integer_range_sym, range);
    } else {
        rb_hash_aset(opts, integer_range_sym, Qnil);
    }
    switch (oj_default_options.escape_mode) {
    case NLEsc: rb_hash_aset(opts, escape_mode_sym, newline_sym); break;
    case JSONEsc: rb_hash_aset(opts, escape_mode_sym, json_sym); break;
    case SlashEsc: rb_hash_aset(opts, escape_mode_sym, slash_sym); break;
    case XSSEsc: rb_hash_aset(opts, escape_mode_sym, xss_safe_sym); break;
    case ASCIIEsc: rb_hash_aset(opts, escape_mode_sym, ascii_sym); break;
    case JXEsc: rb_hash_aset(opts, escape_mode_sym, unicode_xss_sym); break;
    default: rb_hash_aset(opts, escape_mode_sym, json_sym); break;
    }
    switch (oj_default_options.time_format) {
    case XmlTime: rb_hash_aset(opts, time_format_sym, xmlschema_sym); break;
    case RubyTime: rb_hash_aset(opts, time_format_sym, ruby_sym); break;
    case UnixZTime: rb_hash_aset(opts, time_format_sym, unix_zone_sym); break;
    case UnixTime:
    default: rb_hash_aset(opts, time_format_sym, unix_sym); break;
    }
    switch (oj_default_options.bigdec_load) {
    case BigDec: rb_hash_aset(opts, bigdecimal_load_sym, bigdecimal_sym); break;
    case FloatDec: rb_hash_aset(opts, bigdecimal_load_sym, float_sym); break;
    case FastDec: rb_hash_aset(opts, bigdecimal_load_sym, fast_sym); break;
    case AutoDec:
    default: rb_hash_aset(opts, bigdecimal_load_sym, auto_sym); break;
    }
    rb_hash_aset(opts, compat_bigdecimal_sym, oj_default_options.compat_bigdec ? Qtrue : Qfalse);
    rb_hash_aset(opts,
                 create_id_sym,
                 (NULL == oj_default_options.create_id) ? Qnil : rb_str_new2(oj_default_options.create_id));
    rb_hash_aset(
        opts,
        oj_space_sym,
        (0 == oj_default_options.dump_opts.after_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.after_sep));
    rb_hash_aset(
        opts,
        oj_space_before_sym,
        (0 == oj_default_options.dump_opts.before_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.before_sep));
    rb_hash_aset(
        opts,
        oj_object_nl_sym,
        (0 == oj_default_options.dump_opts.hash_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.hash_nl));
    rb_hash_aset(
        opts,
        oj_array_nl_sym,
        (0 == oj_default_options.dump_opts.array_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.array_nl));

    switch (oj_default_options.dump_opts.nan_dump) {
    case NullNan: rb_hash_aset(opts, nan_sym, null_sym); break;
    case RaiseNan: rb_hash_aset(opts, nan_sym, raise_sym); break;
    case WordNan: rb_hash_aset(opts, nan_sym, word_sym); break;
    case HugeNan: rb_hash_aset(opts, nan_sym, huge_sym); break;
    case AutoNan:
    default: rb_hash_aset(opts, nan_sym, auto_sym); break;
    }
    rb_hash_aset(opts, omit_nil_sym, oj_default_options.dump_opts.omit_nil ? Qtrue : Qfalse);
    rb_hash_aset(opts, omit_null_byte_sym, oj_default_options.dump_opts.omit_null_byte ? Qtrue : Qfalse);
    rb_hash_aset(opts, oj_hash_class_sym, oj_default_options.hash_class);
    rb_hash_aset(opts, oj_array_class_sym, oj_default_options.array_class);

    if (NULL == oj_default_options.ignore) {
        rb_hash_aset(opts, ignore_sym, Qnil);
    } else {
        VALUE         *vp;
        volatile VALUE a = rb_ary_new();

        for (vp = oj_default_options.ignore; Qnil != *vp; vp++) {
            rb_ary_push(a, *vp);
        }
        rb_hash_aset(opts, ignore_sym, a);
    }
    return opts;
}