123456789_123456789_123456789_123456789_123456789_

Class: JSON::Coder

Relationships & Source Files
Inherits: Object
Defined in: ext/json/lib/json/common.rb

Overview

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 Method Summary

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 encoutered, 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"
[ GitHub ]

  
# File 'ext/json/lib/json/common.rb', line 900

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
  options[:create_additions] = false unless options.key?(:create_additions)

  @state = State.new(options).freeze
  @parser_config = Ext::Parser::Config.new(options)
end

Class Method Details

.dump(object) ⇒ String (mod_func) .dump(object, io) ⇒ IO
Also known as: .generate

Serialize the given object into a JSON document.

[ GitHub ]

  
# File 'ext/json/lib/json/common.rb', line 919

def dump(object, io = nil)
  @state.generate_new(object, io)
end

.generate(object, io = nil) (mod_func)

Alias for #dump.

[ GitHub ]

  
# File 'ext/json/lib/json/common.rb', line 922

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 encoutered, 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"
[ GitHub ]

  
# File 'ext/json/lib/json/common.rb', line 900

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
  options[:create_additions] = false unless options.key?(:create_additions)

  @state = State.new(options).freeze
  @parser_config = Ext::Parser::Config.new(options)
end

.load(string) ⇒ Object (mod_func) Also known as: .parse

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

[ GitHub ]

  
# File 'ext/json/lib/json/common.rb', line 928

def load(source)
  @parser_config.parse(source)
end

.load(path) ⇒ Object (mod_func)

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

[ GitHub ]

  
# File 'ext/json/lib/json/common.rb', line 937

def load_file(path)
  load(File.read(path, encoding: Encoding::UTF_8))
end

.parse(source) (mod_func)

Alias for #load.

[ GitHub ]

  
# File 'ext/json/lib/json/common.rb', line 931

alias_method :parse, :load