To change default serialization mode use the following form. Attempting to modify the Oj.default_options Hash directly will not set the changes on the actual default options but on a copy of the Hash:
Oj.default_options = {:mode => :compat }
Another way to make use of options when calling load or dump methods is to pass in a Hash with the options already set in the Hash. This is slightly less efficient than setting the globals for many smaller JSON documents but does provide a more thread safe approach to using custom options for loading and dumping.
If true a nil input to load will return nil and not raise an Exception.
Allow or prohibit GC during parsing, default is true (allow).
Allow invalid unicode, default is false (don't allow).
Alias for the :nan option.
Class to use instead of Array on load.
Trailer appended to the end of an array dump. The default is an empty string. Primarily intended for json gem compatibility. Using just indent as an integer gives better performance.
If true all non-ASCII character are escaped when dumping. This is the same as setting the :escape_mode options to :ascii and exists for json gem compatibility.
Automatically define classes if they do not exist.
If true dump BigDecimal as a decimal number otherwise as a String
Determines how to load decimals.
-
:bigdecimal
convert all decimal numbers to BigDecimal. -
:float
convert all decimal numbers to Float. -
:auto
the most precise for the number of digits is used.
Detect circular references while dumping. In :compat mode raise a NestingError. For other modes except the :object mode place a null in the output. For :object mode place references in the output that will be used to recreate the looped references on load.
Cache classes for faster parsing. This option should not be used if dynamically modifying classes or reloading classes then don't use this.
A flag indicating the :create_id key when encountered during parsing should creating an Object mactching the class name specified in the value associated with the key.
The :create_id option specifies that key is used for dumping and loading when
specifying the class for an encoded object. The default is json_create
.
In the :custom
mode setting the :create_id
to nil will cause Complex,
Rational, Range, and Regexp to be output as strings instead of as JSON
objects.
If true an empty or all whitespace input will not raise an Exception. The default_options will be honored for :null, :strict, and :custom modes. Ignored for :custom and :wab. The :compat has a more complex set of rules. The JSON gem compatibility is best described by examples.
JSON.parse('') => raise
JSON.parse(' ') => raise
JSON.load('') => nil
JSON.load('', nil, allow_blank: false) => raise
JSON.load('', nil, allow_blank: true) => nil
JSON.load(' ') => raise
JSON.load(' ', nil, allow_blank: false) => raise
JSON.load(' ', nil, allow_blank: true) => raise
Determines the characters to escape when dumping. Only the :ascii and :json modes are supported in :compat mode.
-
:newline
allows unescaped newlines in the output. -
:json
follows the JSON specification. This is the default mode. -
:xss_safe
escapes HTML and XML characters such as&
and<
. -
:ascii
escapes all non-ascii or characters with the hi-bit set. -
:unicode_xss
escapes a special unicodes and is xss safe.
The number of digits of precision when dumping floats, 0 indicates use Ruby directly.
Class to use instead of Hash on load. This is the same as the :object_class.
Ignore all the classes in the Array when dumping. A value of nil indicates ignore nothing.
Number of spaces to indent each element in a JSON document, zero is no newline between JSON elements, negative indicates no newline between top level JSON elements in a stream.
Indentation for each element when dumping. The default is an empty string. Primarily intended for json gem compatibility. Using just indent as an integer gives better performance.
Dump integers outside range as strings. Note: range bounds must be Fixnum.
Provides a means to detect strings that should be used to create non-String objects. The value to the option must be a Hash with keys that are regular expressions and values are class names. For strict json gem compatibility a RegExp should be used. For better performance but sacrificing some regexp options a string can be used and the C version of regex will be used instead.
The maximum nesting depth on both dump and load that is allowed. This exists for json gem compatibility.
Primary behavior for loading and dumping. The :mode option controls which other options are in effect. For more details see the {file:Modes.md} page. By default Oj uses the :custom mode which is provides the highest degree of customization.
How to dump Infinity, -Infinity, and NaN in :null, :strict, and :compat mode. Default is :auto but is ignored in the :compat and :rails 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 which are:raise
for:strict
,:null
for:null
, and:word
for:compat
.
If true a nil input to load will return nil and not raise an Exception.
The class to use when creating a Hash on load instead of the Hash class.
Trailer appended to the end of an object dump. The default is an empty string. Primarily intended for json gem compatibility. Using just indent as an integer gives better performance.
If true, Hash and Object attributes with nil values are omitted.
Allow single JSON values instead of documents, default is true (allow). This can also be used in :compat mode to be backward compatible with older versions of the json gem.
The JSON gem includes the complete JSON in parse errors with no limit
on size. To break from the JSON gem behavior for this case set :safe
to true.
The number of digits after the decimal when dumping the seconds of time.
String inserted after the ':' character when dumping a JSON object. The default is an empty string. Primarily intended for json gem compatibility. Using just indent as an integer gives better performance.
String inserted before the ':' character when dumping a JSON object. The default is an empty string. Primarily intended for json gem compatibility. Using just indent as an integer gives better performance.
Use symbols instead of strings for hash keys. :symbolize_names is an alias.
When true dump and load functions are traced by printing beginning and ending of blocks and of specific calls.
The :time_format when dumping.
-
:unix
time is output as a decimal number in seconds since epoch including fractions of a second. -
:unix_zone
similar to the:unix
format but with the timezone encoded in the exponent of the decimal number of seconds since epoch. -
:xmlschema
time is output as a string that follows the XML schema definition. -
:ruby
time is output as a string formatted using the Rubyto_s
conversion.
Call as_json()
methods on dump, default is false. The option is ignored in
the :compat and :rails mode.
Call raw_json()
methods on dump, default is false. The option is
accepted in the :compat and :rails mode even though it is not
supported by other JSON gems. It provides a means to optimize dump or
generate performance. The raw_json(depth, indent)
method should be
called only by Oj. It is not intended for any other use. This is mean
to replace the abused to_json
methods. Calling Oj.dump
inside the
raw_json
with the object itself when :use_raw_json
is true will
result in an infinite loop.
Call to_hash()
methods on dump, default is false. The option is ignored in
the :compat and :rails mode.
Call to_json()
methods on dump, default is false. The option is ignored in
the :compat and :rails mode.