Class: JSON::Coder
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | ext/json/lib/json/common.rb |
Overview
Constant Summary
-
EXCLUDED_GENERATOR_OPTIONS =
private
# File 'ext/json/lib/json/common.rb', line 799(PARSER_OPTIONS - %i( max_nesting allow_nan allow_duplicate_key )).freeze
-
PARSER_OPTIONS =
private
# File 'ext/json/lib/json/common.rb', line 785%i( max_nesting allow_nan allow_trailing_comma allow_comments allow_control_characters allow_invalid_escape symbolize_names freeze allow_duplicate_key decimal_class ).freeze
Class Method Summary
-
.dump(object) ⇒ String
(also: .generate)
mod_func
Serialize the given object into a JSON document.
-
.generate(object, io = nil)
mod_func
Alias for #dump.
-
JSON.new(options = nil, &block)
mod_func
Argument
options, if given, contains a Hash of options for both parsing and generating. -
.load(string) ⇒ Object
(also: .parse)
mod_func
Parse the given JSON document and return an equivalent Ruby object.
-
.load(path) ⇒ Object
mod_func
Parse the given JSON document and return an equivalent Ruby object.
-
.parse(source)
mod_func
Alias for #load.
-
.
constructor
private
Argument
options, if given, contains a Hash of options for both parsing and generating.
Constructor Details
. (private)
Argument options, if given, contains a Hash of options for both parsing and generating.
See Parsing Options,
and Generating Options.
For generation, the strict: true option is always set. When a Ruby object with no native JSON counterpart is encountered, the block provided to the initialize method is invoked, and must return a Ruby object that has a native JSON counterpart:
module MyApp API_JSON_CODER = JSON::Coder.new do |object| case object when Time object.iso8601(3) else object # Unknown type, will raise end end end
puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
# File 'ext/json/lib/json/common.rb', line 830
def initialize(object_class: nil, array_class: nil, on_load: nil, **, &as_json) if object_class || array_class on_load = ParserOptions.on_load(on_load, object_class, array_class) end = .slice(*PARSER_OPTIONS) [:on_load] = on_load if on_load @parser_config = Ext::Parser::Config.new().freeze = .reject { |k, _| EXCLUDED_GENERATOR_OPTIONS.include?(k) } @state = State.new( **, strict: true, as_json: as_json, ).freeze end
Class Method Details
.dump(object) ⇒ String (mod_func)
.dump(object, io) ⇒ IO
Also known as: .generate
String (mod_func)
.dump(object, io) ⇒ IO
Serialize the given object into a JSON document.
# File 'ext/json/lib/json/common.rb', line 851
def dump(object, io = nil) @state.generate(object, io) end
.generate(object, io = nil) (mod_func)
Alias for #dump.
# File 'ext/json/lib/json/common.rb', line 854
alias_method :generate, :dump
JSON.new(options = nil, &block) (mod_func)
Argument options, if given, contains a Hash of options for both parsing and generating.
See Parsing Options,
and Generating Options.
For generation, the strict: true option is always set. When a Ruby object with no native JSON counterpart is encountered, the block provided to the initialize method is invoked, and must return a Ruby object that has a native JSON counterpart:
module MyApp API_JSON_CODER = JSON::Coder.new do |object| case object when Time object.iso8601(3) else object # Unknown type, will raise end end end
puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
# File 'ext/json/lib/json/common.rb', line 830
def initialize(object_class: nil, array_class: nil, on_load: nil, **, &as_json) if object_class || array_class on_load = ParserOptions.on_load(on_load, object_class, array_class) end = .slice(*PARSER_OPTIONS) [:on_load] = on_load if on_load @parser_config = Ext::Parser::Config.new().freeze = .reject { |k, _| EXCLUDED_GENERATOR_OPTIONS.include?(k) } @state = State.new( **, strict: true, as_json: as_json, ).freeze end
.load(string) ⇒ Object (mod_func) Also known as: .parse
Parse the given JSON document and return an equivalent Ruby object.
# File 'ext/json/lib/json/common.rb', line 860
def load(source) @parser_config.parse(source) end
.load(path) ⇒ Object (mod_func)
Parse the given JSON document and return an equivalent Ruby object.
# File 'ext/json/lib/json/common.rb', line 869
def load_file(path) load(File.read(path, encoding: Encoding::UTF_8)) end
.parse(source) (mod_func)
Alias for #load.
# File 'ext/json/lib/json/common.rb', line 863
alias_method :parse, :load