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

  • :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|: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] load and dump modes to use for JSON

  • :time_format [:unix|:unix_zone|:xmlschema|:ruby] time format when dumping in :compat and :object mode

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

  • :bigdecimal_load [:bigdecimal|:float|:auto] load decimals as BigDecimal instead of as a Float. :auto pick the most precise for the number of digits.

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

  • :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

  • :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

  • :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 in null, strict, and compat mode. :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

Return [Hash] all current option settings.



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

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, 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, 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, float_prec_sym, INT2FIX(oj_default_options.float_prec));
    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:
    case CustomMode:  rb_hash_aset(opts, mode_sym, custom_sym);  break;
    case RailsMode: rb_hash_aset(opts, mode_sym, rails_sym);  break;
    default:    rb_hash_aset(opts, mode_sym, object_sym); break;
    }
    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 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 AutoDec:
    default:    rb_hash_aset(opts, bigdecimal_load_sym, auto_sym);  break;
    }
    rb_hash_aset(opts, create_id_sym, (0 == 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, oj_hash_class_sym, oj_default_options.hash_class);
    rb_hash_aset(opts, oj_array_class_sym, oj_default_options.array_class);
    
    return opts;
}