1. JSON::
  2. Coder

class JSON::Coder

JSON::Coder holds a parser and generator configuration.

moduleMyAppJSONC_CODER =JSON::Coder.new(allow_trailing_comma:true  )endMyApp::JSONC_CODER.load(document)

Public Class Methods

Source
# File ext/json/lib/json/common.rb, line 1046definitialize(options =nil,&as_json)ifoptions.nil?options = {strict:true }elseoptions =options.dupoptions[:strict] =trueendoptions[:as_json] =as_jsonifas_json@state =State.new(options).freeze@parser_config =Ext::Parser::Config.new(ParserOptions.prepare(options))end

Argumentoptions, if given, contains a Hash of options for both parsing and generating. SeeParsing Options, andGenerating Options.

For generation, thestrict: 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:

moduleMyAppAPI_JSON_CODER =JSON::Coder.newdo|object|caseobjectwhenTimeobject.iso8601(3)elseobject# Unknown type, will raiseendendendputsMyApp::API_JSON_CODER.dump(Time.now.utc)# => "2025-01-21T08:41:44.286Z"

Public Instance Methods

Source
# File ext/json/lib/json/common.rb, line 1064defdump(object,io =nil)@state.generate_new(object,io)end

Serialize the given object into a JSON document.

Also aliased as:generate
Alias for:dump
Source
# File ext/json/lib/json/common.rb, line 1073defload(source)@parser_config.parse(source)end

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

Also aliased as:parse
Source
# File ext/json/lib/json/common.rb, line 1082defload_file(path)load(File.read(path,encoding:Encoding::UTF_8))end

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

Alias for:load