class JSON::Coder
MyApp::JSONC_CODER.load(document)
end
)
allow_trailing_comma: true
JSONC_CODER = JSON::Coder.new(
module MyApp
JSON::Coder holds a parser and generator configuration.
def dump(object, io = nil)
dump(object, io) -> io
dump(object) -> String
call-seq:
def dump(object, io = nil) @state.generate_new(object, io) end
def initialize(options = nil, &as_json)
puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
end
end
end
object # Unknown type, will raise
else
object.iso8601(3)
when Time
case object
API_JSON_CODER = JSON::Coder.new do |object|
module MyApp
\JSON counterpart:
encoutered, the block provided to the initialize method is invoked, and must return a Ruby object that has a native
For generation, the strict: true option is always set. When a Ruby object with no native \JSON counterpart is
See {Parsing Options}[#module-JSON-label-Parsing+Options], and {Generating Options}[#module-JSON-label-Generating+Options].
Argument +options+, if given, contains a \Hash of options for both parsing and generating.
JSON.new(options = nil, &block)
:call-seq:
def initialize(options = nil, &as_json) if options.nil? options = { strict: true } else options = options.dup options[:strict] = true end options[:as_json] = as_json if as_json @state = State.new(options).freeze @parser_config = Ext::Parser::Config.new(ParserOptions.prepare(options)) end
def load(source)
load(string) -> Object
call-seq:
def load(source) @parser_config.parse(source) end
def load_file(path)
load(path) -> Object
call-seq:
def load_file(path) load(File.read(path, encoding: Encoding::UTF_8)) end