Class

JSON::Coder holds a parser and generator configuration.

module MyApp
  JSONC_CODER = JSON::Coder.new(
    allow_trailing_comma: true
  )
end

MyApp::JSONC_CODER.load(document)
Class Methods

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"
Instance Methods

Serialize the given object into a JSON document.

Parse the given JSON document and return an equivalent Ruby object.

Parse the given JSON document and return an equivalent Ruby object.