Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby30/share/ruby
File: json.rb
#frozen_string_literal: false
[0] Fix | Delete
require 'json/common'
[1] Fix | Delete
[2] Fix | Delete
##
[3] Fix | Delete
# = JavaScript \Object Notation (\JSON)
[4] Fix | Delete
#
[5] Fix | Delete
# \JSON is a lightweight data-interchange format.
[6] Fix | Delete
#
[7] Fix | Delete
# A \JSON value is one of the following:
[8] Fix | Delete
# - Double-quoted text: <tt>"foo"</tt>.
[9] Fix | Delete
# - Number: +1+, +1.0+, +2.0e2+.
[10] Fix | Delete
# - Boolean: +true+, +false+.
[11] Fix | Delete
# - Null: +null+.
[12] Fix | Delete
# - \Array: an ordered list of values, enclosed by square brackets:
[13] Fix | Delete
# ["foo", 1, 1.0, 2.0e2, true, false, null]
[14] Fix | Delete
#
[15] Fix | Delete
# - \Object: a collection of name/value pairs, enclosed by curly braces;
[16] Fix | Delete
# each name is double-quoted text;
[17] Fix | Delete
# the values may be any \JSON values:
[18] Fix | Delete
# {"a": "foo", "b": 1, "c": 1.0, "d": 2.0e2, "e": true, "f": false, "g": null}
[19] Fix | Delete
#
[20] Fix | Delete
# A \JSON array or object may contain nested arrays, objects, and scalars
[21] Fix | Delete
# to any depth:
[22] Fix | Delete
# {"foo": {"bar": 1, "baz": 2}, "bat": [0, 1, 2]}
[23] Fix | Delete
# [{"foo": 0, "bar": 1}, ["baz", 2]]
[24] Fix | Delete
#
[25] Fix | Delete
# == Using \Module \JSON
[26] Fix | Delete
#
[27] Fix | Delete
# To make module \JSON available in your code, begin with:
[28] Fix | Delete
# require 'json'
[29] Fix | Delete
#
[30] Fix | Delete
# All examples here assume that this has been done.
[31] Fix | Delete
#
[32] Fix | Delete
# === Parsing \JSON
[33] Fix | Delete
#
[34] Fix | Delete
# You can parse a \String containing \JSON data using
[35] Fix | Delete
# either of two methods:
[36] Fix | Delete
# - <tt>JSON.parse(source, opts)</tt>
[37] Fix | Delete
# - <tt>JSON.parse!(source, opts)</tt>
[38] Fix | Delete
#
[39] Fix | Delete
# where
[40] Fix | Delete
# - +source+ is a Ruby object.
[41] Fix | Delete
# - +opts+ is a \Hash object containing options
[42] Fix | Delete
# that control both input allowed and output formatting.
[43] Fix | Delete
#
[44] Fix | Delete
# The difference between the two methods
[45] Fix | Delete
# is that JSON.parse! omits some checks
[46] Fix | Delete
# and may not be safe for some +source+ data;
[47] Fix | Delete
# use it only for data from trusted sources.
[48] Fix | Delete
# Use the safer method JSON.parse for less trusted sources.
[49] Fix | Delete
#
[50] Fix | Delete
# ==== Parsing \JSON Arrays
[51] Fix | Delete
#
[52] Fix | Delete
# When +source+ is a \JSON array, JSON.parse by default returns a Ruby \Array:
[53] Fix | Delete
# json = '["foo", 1, 1.0, 2.0e2, true, false, null]'
[54] Fix | Delete
# ruby = JSON.parse(json)
[55] Fix | Delete
# ruby # => ["foo", 1, 1.0, 200.0, true, false, nil]
[56] Fix | Delete
# ruby.class # => Array
[57] Fix | Delete
#
[58] Fix | Delete
# The \JSON array may contain nested arrays, objects, and scalars
[59] Fix | Delete
# to any depth:
[60] Fix | Delete
# json = '[{"foo": 0, "bar": 1}, ["baz", 2]]'
[61] Fix | Delete
# JSON.parse(json) # => [{"foo"=>0, "bar"=>1}, ["baz", 2]]
[62] Fix | Delete
#
[63] Fix | Delete
# ==== Parsing \JSON \Objects
[64] Fix | Delete
#
[65] Fix | Delete
# When the source is a \JSON object, JSON.parse by default returns a Ruby \Hash:
[66] Fix | Delete
# json = '{"a": "foo", "b": 1, "c": 1.0, "d": 2.0e2, "e": true, "f": false, "g": null}'
[67] Fix | Delete
# ruby = JSON.parse(json)
[68] Fix | Delete
# ruby # => {"a"=>"foo", "b"=>1, "c"=>1.0, "d"=>200.0, "e"=>true, "f"=>false, "g"=>nil}
[69] Fix | Delete
# ruby.class # => Hash
[70] Fix | Delete
#
[71] Fix | Delete
# The \JSON object may contain nested arrays, objects, and scalars
[72] Fix | Delete
# to any depth:
[73] Fix | Delete
# json = '{"foo": {"bar": 1, "baz": 2}, "bat": [0, 1, 2]}'
[74] Fix | Delete
# JSON.parse(json) # => {"foo"=>{"bar"=>1, "baz"=>2}, "bat"=>[0, 1, 2]}
[75] Fix | Delete
#
[76] Fix | Delete
# ==== Parsing \JSON Scalars
[77] Fix | Delete
#
[78] Fix | Delete
# When the source is a \JSON scalar (not an array or object),
[79] Fix | Delete
# JSON.parse returns a Ruby scalar.
[80] Fix | Delete
#
[81] Fix | Delete
# \String:
[82] Fix | Delete
# ruby = JSON.parse('"foo"')
[83] Fix | Delete
# ruby # => 'foo'
[84] Fix | Delete
# ruby.class # => String
[85] Fix | Delete
# \Integer:
[86] Fix | Delete
# ruby = JSON.parse('1')
[87] Fix | Delete
# ruby # => 1
[88] Fix | Delete
# ruby.class # => Integer
[89] Fix | Delete
# \Float:
[90] Fix | Delete
# ruby = JSON.parse('1.0')
[91] Fix | Delete
# ruby # => 1.0
[92] Fix | Delete
# ruby.class # => Float
[93] Fix | Delete
# ruby = JSON.parse('2.0e2')
[94] Fix | Delete
# ruby # => 200
[95] Fix | Delete
# ruby.class # => Float
[96] Fix | Delete
# Boolean:
[97] Fix | Delete
# ruby = JSON.parse('true')
[98] Fix | Delete
# ruby # => true
[99] Fix | Delete
# ruby.class # => TrueClass
[100] Fix | Delete
# ruby = JSON.parse('false')
[101] Fix | Delete
# ruby # => false
[102] Fix | Delete
# ruby.class # => FalseClass
[103] Fix | Delete
# Null:
[104] Fix | Delete
# ruby = JSON.parse('null')
[105] Fix | Delete
# ruby # => nil
[106] Fix | Delete
# ruby.class # => NilClass
[107] Fix | Delete
#
[108] Fix | Delete
# ==== Parsing Options
[109] Fix | Delete
#
[110] Fix | Delete
# ====== Input Options
[111] Fix | Delete
#
[112] Fix | Delete
# Option +max_nesting+ (\Integer) specifies the maximum nesting depth allowed;
[113] Fix | Delete
# defaults to +100+; specify +false+ to disable depth checking.
[114] Fix | Delete
#
[115] Fix | Delete
# With the default, +false+:
[116] Fix | Delete
# source = '[0, [1, [2, [3]]]]'
[117] Fix | Delete
# ruby = JSON.parse(source)
[118] Fix | Delete
# ruby # => [0, [1, [2, [3]]]]
[119] Fix | Delete
# Too deep:
[120] Fix | Delete
# # Raises JSON::NestingError (nesting of 2 is too deep):
[121] Fix | Delete
# JSON.parse(source, {max_nesting: 1})
[122] Fix | Delete
# Bad value:
[123] Fix | Delete
# # Raises TypeError (wrong argument type Symbol (expected Fixnum)):
[124] Fix | Delete
# JSON.parse(source, {max_nesting: :foo})
[125] Fix | Delete
#
[126] Fix | Delete
# ---
[127] Fix | Delete
#
[128] Fix | Delete
# Option +allow_nan+ (boolean) specifies whether to allow
[129] Fix | Delete
# NaN, Infinity, and MinusInfinity in +source+;
[130] Fix | Delete
# defaults to +false+.
[131] Fix | Delete
#
[132] Fix | Delete
# With the default, +false+:
[133] Fix | Delete
# # Raises JSON::ParserError (225: unexpected token at '[NaN]'):
[134] Fix | Delete
# JSON.parse('[NaN]')
[135] Fix | Delete
# # Raises JSON::ParserError (232: unexpected token at '[Infinity]'):
[136] Fix | Delete
# JSON.parse('[Infinity]')
[137] Fix | Delete
# # Raises JSON::ParserError (248: unexpected token at '[-Infinity]'):
[138] Fix | Delete
# JSON.parse('[-Infinity]')
[139] Fix | Delete
# Allow:
[140] Fix | Delete
# source = '[NaN, Infinity, -Infinity]'
[141] Fix | Delete
# ruby = JSON.parse(source, {allow_nan: true})
[142] Fix | Delete
# ruby # => [NaN, Infinity, -Infinity]
[143] Fix | Delete
#
[144] Fix | Delete
# ====== Output Options
[145] Fix | Delete
#
[146] Fix | Delete
# Option +symbolize_names+ (boolean) specifies whether returned \Hash keys
[147] Fix | Delete
# should be Symbols;
[148] Fix | Delete
# defaults to +false+ (use Strings).
[149] Fix | Delete
#
[150] Fix | Delete
# With the default, +false+:
[151] Fix | Delete
# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
[152] Fix | Delete
# ruby = JSON.parse(source)
[153] Fix | Delete
# ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil}
[154] Fix | Delete
# Use Symbols:
[155] Fix | Delete
# ruby = JSON.parse(source, {symbolize_names: true})
[156] Fix | Delete
# ruby # => {:a=>"foo", :b=>1.0, :c=>true, :d=>false, :e=>nil}
[157] Fix | Delete
#
[158] Fix | Delete
# ---
[159] Fix | Delete
#
[160] Fix | Delete
# Option +object_class+ (\Class) specifies the Ruby class to be used
[161] Fix | Delete
# for each \JSON object;
[162] Fix | Delete
# defaults to \Hash.
[163] Fix | Delete
#
[164] Fix | Delete
# With the default, \Hash:
[165] Fix | Delete
# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
[166] Fix | Delete
# ruby = JSON.parse(source)
[167] Fix | Delete
# ruby.class # => Hash
[168] Fix | Delete
# Use class \OpenStruct:
[169] Fix | Delete
# ruby = JSON.parse(source, {object_class: OpenStruct})
[170] Fix | Delete
# ruby # => #<OpenStruct a="foo", b=1.0, c=true, d=false, e=nil>
[171] Fix | Delete
#
[172] Fix | Delete
# ---
[173] Fix | Delete
#
[174] Fix | Delete
# Option +array_class+ (\Class) specifies the Ruby class to be used
[175] Fix | Delete
# for each \JSON array;
[176] Fix | Delete
# defaults to \Array.
[177] Fix | Delete
#
[178] Fix | Delete
# With the default, \Array:
[179] Fix | Delete
# source = '["foo", 1.0, true, false, null]'
[180] Fix | Delete
# ruby = JSON.parse(source)
[181] Fix | Delete
# ruby.class # => Array
[182] Fix | Delete
# Use class \Set:
[183] Fix | Delete
# ruby = JSON.parse(source, {array_class: Set})
[184] Fix | Delete
# ruby # => #<Set: {"foo", 1.0, true, false, nil}>
[185] Fix | Delete
#
[186] Fix | Delete
# ---
[187] Fix | Delete
#
[188] Fix | Delete
# Option +create_additions+ (boolean) specifies whether to use \JSON additions in parsing.
[189] Fix | Delete
# See {\JSON Additions}[#module-JSON-label-JSON+Additions].
[190] Fix | Delete
#
[191] Fix | Delete
# === Generating \JSON
[192] Fix | Delete
#
[193] Fix | Delete
# To generate a Ruby \String containing \JSON data,
[194] Fix | Delete
# use method <tt>JSON.generate(source, opts)</tt>, where
[195] Fix | Delete
# - +source+ is a Ruby object.
[196] Fix | Delete
# - +opts+ is a \Hash object containing options
[197] Fix | Delete
# that control both input allowed and output formatting.
[198] Fix | Delete
#
[199] Fix | Delete
# ==== Generating \JSON from Arrays
[200] Fix | Delete
#
[201] Fix | Delete
# When the source is a Ruby \Array, JSON.generate returns
[202] Fix | Delete
# a \String containing a \JSON array:
[203] Fix | Delete
# ruby = [0, 's', :foo]
[204] Fix | Delete
# json = JSON.generate(ruby)
[205] Fix | Delete
# json # => '[0,"s","foo"]'
[206] Fix | Delete
#
[207] Fix | Delete
# The Ruby \Array array may contain nested arrays, hashes, and scalars
[208] Fix | Delete
# to any depth:
[209] Fix | Delete
# ruby = [0, [1, 2], {foo: 3, bar: 4}]
[210] Fix | Delete
# json = JSON.generate(ruby)
[211] Fix | Delete
# json # => '[0,[1,2],{"foo":3,"bar":4}]'
[212] Fix | Delete
#
[213] Fix | Delete
# ==== Generating \JSON from Hashes
[214] Fix | Delete
#
[215] Fix | Delete
# When the source is a Ruby \Hash, JSON.generate returns
[216] Fix | Delete
# a \String containing a \JSON object:
[217] Fix | Delete
# ruby = {foo: 0, bar: 's', baz: :bat}
[218] Fix | Delete
# json = JSON.generate(ruby)
[219] Fix | Delete
# json # => '{"foo":0,"bar":"s","baz":"bat"}'
[220] Fix | Delete
#
[221] Fix | Delete
# The Ruby \Hash array may contain nested arrays, hashes, and scalars
[222] Fix | Delete
# to any depth:
[223] Fix | Delete
# ruby = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
[224] Fix | Delete
# json = JSON.generate(ruby)
[225] Fix | Delete
# json # => '{"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}'
[226] Fix | Delete
#
[227] Fix | Delete
# ==== Generating \JSON from Other Objects
[228] Fix | Delete
#
[229] Fix | Delete
# When the source is neither an \Array nor a \Hash,
[230] Fix | Delete
# the generated \JSON data depends on the class of the source.
[231] Fix | Delete
#
[232] Fix | Delete
# When the source is a Ruby \Integer or \Float, JSON.generate returns
[233] Fix | Delete
# a \String containing a \JSON number:
[234] Fix | Delete
# JSON.generate(42) # => '42'
[235] Fix | Delete
# JSON.generate(0.42) # => '0.42'
[236] Fix | Delete
#
[237] Fix | Delete
# When the source is a Ruby \String, JSON.generate returns
[238] Fix | Delete
# a \String containing a \JSON string (with double-quotes):
[239] Fix | Delete
# JSON.generate('A string') # => '"A string"'
[240] Fix | Delete
#
[241] Fix | Delete
# When the source is +true+, +false+ or +nil+, JSON.generate returns
[242] Fix | Delete
# a \String containing the corresponding \JSON token:
[243] Fix | Delete
# JSON.generate(true) # => 'true'
[244] Fix | Delete
# JSON.generate(false) # => 'false'
[245] Fix | Delete
# JSON.generate(nil) # => 'null'
[246] Fix | Delete
#
[247] Fix | Delete
# When the source is none of the above, JSON.generate returns
[248] Fix | Delete
# a \String containing a \JSON string representation of the source:
[249] Fix | Delete
# JSON.generate(:foo) # => '"foo"'
[250] Fix | Delete
# JSON.generate(Complex(0, 0)) # => '"0+0i"'
[251] Fix | Delete
# JSON.generate(Dir.new('.')) # => '"#<Dir>"'
[252] Fix | Delete
#
[253] Fix | Delete
# ==== Generating Options
[254] Fix | Delete
#
[255] Fix | Delete
# ====== Input Options
[256] Fix | Delete
#
[257] Fix | Delete
# Option +allow_nan+ (boolean) specifies whether
[258] Fix | Delete
# +NaN+, +Infinity+, and <tt>-Infinity</tt> may be generated;
[259] Fix | Delete
# defaults to +false+.
[260] Fix | Delete
#
[261] Fix | Delete
# With the default, +false+:
[262] Fix | Delete
# # Raises JSON::GeneratorError (920: NaN not allowed in JSON):
[263] Fix | Delete
# JSON.generate(JSON::NaN)
[264] Fix | Delete
# # Raises JSON::GeneratorError (917: Infinity not allowed in JSON):
[265] Fix | Delete
# JSON.generate(JSON::Infinity)
[266] Fix | Delete
# # Raises JSON::GeneratorError (917: -Infinity not allowed in JSON):
[267] Fix | Delete
# JSON.generate(JSON::MinusInfinity)
[268] Fix | Delete
#
[269] Fix | Delete
# Allow:
[270] Fix | Delete
# ruby = [Float::NaN, Float::Infinity, Float::MinusInfinity]
[271] Fix | Delete
# JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,-Infinity]'
[272] Fix | Delete
#
[273] Fix | Delete
# ---
[274] Fix | Delete
#
[275] Fix | Delete
# Option +max_nesting+ (\Integer) specifies the maximum nesting depth
[276] Fix | Delete
# in +obj+; defaults to +100+.
[277] Fix | Delete
#
[278] Fix | Delete
# With the default, +100+:
[279] Fix | Delete
# obj = [[[[[[0]]]]]]
[280] Fix | Delete
# JSON.generate(obj) # => '[[[[[[0]]]]]]'
[281] Fix | Delete
#
[282] Fix | Delete
# Too deep:
[283] Fix | Delete
# # Raises JSON::NestingError (nesting of 2 is too deep):
[284] Fix | Delete
# JSON.generate(obj, max_nesting: 2)
[285] Fix | Delete
#
[286] Fix | Delete
# ====== Output Options
[287] Fix | Delete
#
[288] Fix | Delete
# The default formatting options generate the most compact
[289] Fix | Delete
# \JSON data, all on one line and with no whitespace.
[290] Fix | Delete
#
[291] Fix | Delete
# You can use these formatting options to generate
[292] Fix | Delete
# \JSON data in a more open format, using whitespace.
[293] Fix | Delete
# See also JSON.pretty_generate.
[294] Fix | Delete
#
[295] Fix | Delete
# - Option +array_nl+ (\String) specifies a string (usually a newline)
[296] Fix | Delete
# to be inserted after each \JSON array; defaults to the empty \String, <tt>''</tt>.
[297] Fix | Delete
# - Option +object_nl+ (\String) specifies a string (usually a newline)
[298] Fix | Delete
# to be inserted after each \JSON object; defaults to the empty \String, <tt>''</tt>.
[299] Fix | Delete
# - Option +indent+ (\String) specifies the string (usually spaces) to be
[300] Fix | Delete
# used for indentation; defaults to the empty \String, <tt>''</tt>;
[301] Fix | Delete
# defaults to the empty \String, <tt>''</tt>;
[302] Fix | Delete
# has no effect unless options +array_nl+ or +object_nl+ specify newlines.
[303] Fix | Delete
# - Option +space+ (\String) specifies a string (usually a space) to be
[304] Fix | Delete
# inserted after the colon in each \JSON object's pair;
[305] Fix | Delete
# defaults to the empty \String, <tt>''</tt>.
[306] Fix | Delete
# - Option +space_before+ (\String) specifies a string (usually a space) to be
[307] Fix | Delete
# inserted before the colon in each \JSON object's pair;
[308] Fix | Delete
# defaults to the empty \String, <tt>''</tt>.
[309] Fix | Delete
#
[310] Fix | Delete
# In this example, +obj+ is used first to generate the shortest
[311] Fix | Delete
# \JSON data (no whitespace), then again with all formatting options
[312] Fix | Delete
# specified:
[313] Fix | Delete
#
[314] Fix | Delete
# obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
[315] Fix | Delete
# json = JSON.generate(obj)
[316] Fix | Delete
# puts 'Compact:', json
[317] Fix | Delete
# opts = {
[318] Fix | Delete
# array_nl: "\n",
[319] Fix | Delete
# object_nl: "\n",
[320] Fix | Delete
# indent: ' ',
[321] Fix | Delete
# space_before: ' ',
[322] Fix | Delete
# space: ' '
[323] Fix | Delete
# }
[324] Fix | Delete
# puts 'Open:', JSON.generate(obj, opts)
[325] Fix | Delete
#
[326] Fix | Delete
# Output:
[327] Fix | Delete
# Compact:
[328] Fix | Delete
# {"foo":["bar","baz"],"bat":{"bam":0,"bad":1}}
[329] Fix | Delete
# Open:
[330] Fix | Delete
# {
[331] Fix | Delete
# "foo" : [
[332] Fix | Delete
# "bar",
[333] Fix | Delete
# "baz"
[334] Fix | Delete
# ],
[335] Fix | Delete
# "bat" : {
[336] Fix | Delete
# "bam" : 0,
[337] Fix | Delete
# "bad" : 1
[338] Fix | Delete
# }
[339] Fix | Delete
# }
[340] Fix | Delete
#
[341] Fix | Delete
# == \JSON Additions
[342] Fix | Delete
#
[343] Fix | Delete
# When you "round trip" a non-\String object from Ruby to \JSON and back,
[344] Fix | Delete
# you have a new \String, instead of the object you began with:
[345] Fix | Delete
# ruby0 = Range.new(0, 2)
[346] Fix | Delete
# json = JSON.generate(ruby0)
[347] Fix | Delete
# json # => '0..2"'
[348] Fix | Delete
# ruby1 = JSON.parse(json)
[349] Fix | Delete
# ruby1 # => '0..2'
[350] Fix | Delete
# ruby1.class # => String
[351] Fix | Delete
#
[352] Fix | Delete
# You can use \JSON _additions_ to preserve the original object.
[353] Fix | Delete
# The addition is an extension of a ruby class, so that:
[354] Fix | Delete
# - \JSON.generate stores more information in the \JSON string.
[355] Fix | Delete
# - \JSON.parse, called with option +create_additions+,
[356] Fix | Delete
# uses that information to create a proper Ruby object.
[357] Fix | Delete
#
[358] Fix | Delete
# This example shows a \Range being generated into \JSON
[359] Fix | Delete
# and parsed back into Ruby, both without and with
[360] Fix | Delete
# the addition for \Range:
[361] Fix | Delete
# ruby = Range.new(0, 2)
[362] Fix | Delete
# # This passage does not use the addition for Range.
[363] Fix | Delete
# json0 = JSON.generate(ruby)
[364] Fix | Delete
# ruby0 = JSON.parse(json0)
[365] Fix | Delete
# # This passage uses the addition for Range.
[366] Fix | Delete
# require 'json/add/range'
[367] Fix | Delete
# json1 = JSON.generate(ruby)
[368] Fix | Delete
# ruby1 = JSON.parse(json1, create_additions: true)
[369] Fix | Delete
# # Make a nice display.
[370] Fix | Delete
# display = <<EOT
[371] Fix | Delete
# Generated JSON:
[372] Fix | Delete
# Without addition: #{json0} (#{json0.class})
[373] Fix | Delete
# With addition: #{json1} (#{json1.class})
[374] Fix | Delete
# Parsed JSON:
[375] Fix | Delete
# Without addition: #{ruby0.inspect} (#{ruby0.class})
[376] Fix | Delete
# With addition: #{ruby1.inspect} (#{ruby1.class})
[377] Fix | Delete
# EOT
[378] Fix | Delete
# puts display
[379] Fix | Delete
#
[380] Fix | Delete
# This output shows the different results:
[381] Fix | Delete
# Generated JSON:
[382] Fix | Delete
# Without addition: "0..2" (String)
[383] Fix | Delete
# With addition: {"json_class":"Range","a":[0,2,false]} (String)
[384] Fix | Delete
# Parsed JSON:
[385] Fix | Delete
# Without addition: "0..2" (String)
[386] Fix | Delete
# With addition: 0..2 (Range)
[387] Fix | Delete
#
[388] Fix | Delete
# The \JSON module includes additions for certain classes.
[389] Fix | Delete
# You can also craft custom additions.
[390] Fix | Delete
# See {Custom \JSON Additions}[#module-JSON-label-Custom+JSON+Additions].
[391] Fix | Delete
#
[392] Fix | Delete
# === Built-in Additions
[393] Fix | Delete
#
[394] Fix | Delete
# The \JSON module includes additions for certain classes.
[395] Fix | Delete
# To use an addition, +require+ its source:
[396] Fix | Delete
# - BigDecimal: <tt>require 'json/add/bigdecimal'</tt>
[397] Fix | Delete
# - Complex: <tt>require 'json/add/complex'</tt>
[398] Fix | Delete
# - Date: <tt>require 'json/add/date'</tt>
[399] Fix | Delete
# - DateTime: <tt>require 'json/add/date_time'</tt>
[400] Fix | Delete
# - Exception: <tt>require 'json/add/exception'</tt>
[401] Fix | Delete
# - OpenStruct: <tt>require 'json/add/ostruct'</tt>
[402] Fix | Delete
# - Range: <tt>require 'json/add/range'</tt>
[403] Fix | Delete
# - Rational: <tt>require 'json/add/rational'</tt>
[404] Fix | Delete
# - Regexp: <tt>require 'json/add/regexp'</tt>
[405] Fix | Delete
# - Set: <tt>require 'json/add/set'</tt>
[406] Fix | Delete
# - Struct: <tt>require 'json/add/struct'</tt>
[407] Fix | Delete
# - Symbol: <tt>require 'json/add/symbol'</tt>
[408] Fix | Delete
# - Time: <tt>require 'json/add/time'</tt>
[409] Fix | Delete
#
[410] Fix | Delete
# To reduce punctuation clutter, the examples below
[411] Fix | Delete
# show the generated \JSON via +puts+, rather than the usual +inspect+,
[412] Fix | Delete
#
[413] Fix | Delete
# \BigDecimal:
[414] Fix | Delete
# require 'json/add/bigdecimal'
[415] Fix | Delete
# ruby0 = BigDecimal(0) # 0.0
[416] Fix | Delete
# json = JSON.generate(ruby0) # {"json_class":"BigDecimal","b":"27:0.0"}
[417] Fix | Delete
# ruby1 = JSON.parse(json, create_additions: true) # 0.0
[418] Fix | Delete
# ruby1.class # => BigDecimal
[419] Fix | Delete
#
[420] Fix | Delete
# \Complex:
[421] Fix | Delete
# require 'json/add/complex'
[422] Fix | Delete
# ruby0 = Complex(1+0i) # 1+0i
[423] Fix | Delete
# json = JSON.generate(ruby0) # {"json_class":"Complex","r":1,"i":0}
[424] Fix | Delete
# ruby1 = JSON.parse(json, create_additions: true) # 1+0i
[425] Fix | Delete
# ruby1.class # Complex
[426] Fix | Delete
#
[427] Fix | Delete
# \Date:
[428] Fix | Delete
# require 'json/add/date'
[429] Fix | Delete
# ruby0 = Date.today # 2020-05-02
[430] Fix | Delete
# json = JSON.generate(ruby0) # {"json_class":"Date","y":2020,"m":5,"d":2,"sg":2299161.0}
[431] Fix | Delete
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02
[432] Fix | Delete
# ruby1.class # Date
[433] Fix | Delete
#
[434] Fix | Delete
# \DateTime:
[435] Fix | Delete
# require 'json/add/date_time'
[436] Fix | Delete
# ruby0 = DateTime.now # 2020-05-02T10:38:13-05:00
[437] Fix | Delete
# json = JSON.generate(ruby0) # {"json_class":"DateTime","y":2020,"m":5,"d":2,"H":10,"M":38,"S":13,"of":"-5/24","sg":2299161.0}
[438] Fix | Delete
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02T10:38:13-05:00
[439] Fix | Delete
# ruby1.class # DateTime
[440] Fix | Delete
#
[441] Fix | Delete
# \Exception (and its subclasses including \RuntimeError):
[442] Fix | Delete
# require 'json/add/exception'
[443] Fix | Delete
# ruby0 = Exception.new('A message') # A message
[444] Fix | Delete
# json = JSON.generate(ruby0) # {"json_class":"Exception","m":"A message","b":null}
[445] Fix | Delete
# ruby1 = JSON.parse(json, create_additions: true) # A message
[446] Fix | Delete
# ruby1.class # Exception
[447] Fix | Delete
# ruby0 = RuntimeError.new('Another message') # Another message
[448] Fix | Delete
# json = JSON.generate(ruby0) # {"json_class":"RuntimeError","m":"Another message","b":null}
[449] Fix | Delete
# ruby1 = JSON.parse(json, create_additions: true) # Another message
[450] Fix | Delete
# ruby1.class # RuntimeError
[451] Fix | Delete
#
[452] Fix | Delete
# \OpenStruct:
[453] Fix | Delete
# require 'json/add/ostruct'
[454] Fix | Delete
# ruby0 = OpenStruct.new(name: 'Matz', language: 'Ruby') # #<OpenStruct name="Matz", language="Ruby">
[455] Fix | Delete
# json = JSON.generate(ruby0) # {"json_class":"OpenStruct","t":{"name":"Matz","language":"Ruby"}}
[456] Fix | Delete
# ruby1 = JSON.parse(json, create_additions: true) # #<OpenStruct name="Matz", language="Ruby">
[457] Fix | Delete
# ruby1.class # OpenStruct
[458] Fix | Delete
#
[459] Fix | Delete
# \Range:
[460] Fix | Delete
# require 'json/add/range'
[461] Fix | Delete
# ruby0 = Range.new(0, 2) # 0..2
[462] Fix | Delete
# json = JSON.generate(ruby0) # {"json_class":"Range","a":[0,2,false]}
[463] Fix | Delete
# ruby1 = JSON.parse(json, create_additions: true) # 0..2
[464] Fix | Delete
# ruby1.class # Range
[465] Fix | Delete
#
[466] Fix | Delete
# \Rational:
[467] Fix | Delete
# require 'json/add/rational'
[468] Fix | Delete
# ruby0 = Rational(1, 3) # 1/3
[469] Fix | Delete
# json = JSON.generate(ruby0) # {"json_class":"Rational","n":1,"d":3}
[470] Fix | Delete
# ruby1 = JSON.parse(json, create_additions: true) # 1/3
[471] Fix | Delete
# ruby1.class # Rational
[472] Fix | Delete
#
[473] Fix | Delete
# \Regexp:
[474] Fix | Delete
# require 'json/add/regexp'
[475] Fix | Delete
# ruby0 = Regexp.new('foo') # (?-mix:foo)
[476] Fix | Delete
# json = JSON.generate(ruby0) # {"json_class":"Regexp","o":0,"s":"foo"}
[477] Fix | Delete
# ruby1 = JSON.parse(json, create_additions: true) # (?-mix:foo)
[478] Fix | Delete
# ruby1.class # Regexp
[479] Fix | Delete
#
[480] Fix | Delete
# \Set:
[481] Fix | Delete
# require 'json/add/set'
[482] Fix | Delete
# ruby0 = Set.new([0, 1, 2]) # #<Set: {0, 1, 2}>
[483] Fix | Delete
# json = JSON.generate(ruby0) # {"json_class":"Set","a":[0,1,2]}
[484] Fix | Delete
# ruby1 = JSON.parse(json, create_additions: true) # #<Set: {0, 1, 2}>
[485] Fix | Delete
# ruby1.class # Set
[486] Fix | Delete
#
[487] Fix | Delete
# \Struct:
[488] Fix | Delete
# require 'json/add/struct'
[489] Fix | Delete
# Customer = Struct.new(:name, :address) # Customer
[490] Fix | Delete
# ruby0 = Customer.new("Dave", "123 Main") # #<struct Customer name="Dave", address="123 Main">
[491] Fix | Delete
# json = JSON.generate(ruby0) # {"json_class":"Customer","v":["Dave","123 Main"]}
[492] Fix | Delete
# ruby1 = JSON.parse(json, create_additions: true) # #<struct Customer name="Dave", address="123 Main">
[493] Fix | Delete
# ruby1.class # Customer
[494] Fix | Delete
#
[495] Fix | Delete
# \Symbol:
[496] Fix | Delete
# require 'json/add/symbol'
[497] Fix | Delete
# ruby0 = :foo # foo
[498] Fix | Delete
# json = JSON.generate(ruby0) # {"json_class":"Symbol","s":"foo"}
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function