Class: MultiJson::Adapters::JsonGem
- Inherits:
-
MultiJson::Adapter
- Object
- MultiJson::Adapter
- MultiJson::Adapters::JsonGem
- Defined in:
- lib/multi_json/adapters/json_gem.rb
Overview
Use the JSON gem to dump/load.
Constant Summary collapse
- ParseError =
::JSON::ParserError
Instance Method Summary collapse
-
#dump(object, options = {}) ⇒ String
private
Serialize a Ruby object to JSON.
-
#load(string, options = {}) ⇒ Object
private
Parse a JSON string into a Ruby object.
Methods inherited from MultiJson::Adapter
defaults, dump, inherited, load
Methods included from Options
#default_dump_options, #default_load_options, #dump_options, #dump_options=, #load_options, #load_options=
Instance Method Details
#dump(object, options = {}) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Serialize a Ruby object to JSON
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/multi_json/adapters/json_gem.rb', line 45 def dump(object, = {}) opts = .except(:adapter) json_object = object.respond_to?(:as_json) ? object.as_json : object return ::JSON.dump(json_object) if opts.empty? if opts.delete(:pretty) opts = PRETTY_STATE_PROTOTYPE.merge(opts) return ::JSON.pretty_generate(json_object, opts) end ::JSON.generate(json_object, opts) end |
#load(string, options = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parse a JSON string into a Ruby object
29 30 31 32 33 34 |
# File 'lib/multi_json/adapters/json_gem.rb', line 29 def load(string, = {}) string = string.dup.force_encoding(Encoding::UTF_8) if string.encoding != Encoding::UTF_8 [:symbolize_names] = true if .delete(:symbolize_keys) ::JSON.parse(string, ) end |