diff options
Diffstat (limited to 'ext/json/lib')
35 files changed, 2657 insertions, 4505 deletions
diff --git a/ext/json/lib/json.rb b/ext/json/lib/json.rb index 3b0b711550..26d601926f 100644 --- a/ext/json/lib/json.rb +++ b/ext/json/lib/json.rb @@ -1,235 +1,675 @@ +# frozen_string_literal: true require 'json/common' -# = json - JSON for Ruby + +## +# = JavaScript \Object Notation (\JSON) +# +# \JSON is a lightweight data-interchange format. # -# == Description +# \JSON is easy for us humans to read and write, +# and equally simple for machines to read (parse) and write (generate). # -# This is a implementation of the JSON specification according to RFC 4627 -# (http://www.ietf.org/rfc/rfc4627.txt). Starting from version 1.0.0 on there -# will be two variants available: +# \JSON is language-independent, making it an ideal interchange format +# for applications in differing programming languages +# and on differing operating systems. # -# * A pure ruby variant, that relies on the iconv and the stringscan -# extensions, which are both part of the ruby standard library. -# * The quite a bit faster C extension variant, which is in parts implemented -# in C and comes with its own unicode conversion functions and a parser -# generated by the ragel state machine compiler -# (http://www.cs.queensu.ca/~thurston/ragel). +# == \JSON Values # -# Both variants of the JSON generator escape all non-ASCII an control -# characters with \uXXXX escape sequences, and support UTF-16 surrogate pairs -# in order to be able to generate the whole range of unicode code points. This -# means that generated JSON text is encoded as UTF-8 (because ASCII is a subset -# of UTF-8) and at the same time avoids decoding problems for receiving -# endpoints, that don't expect UTF-8 encoded texts. On the negative side this -# may lead to a bit longer strings than necessarry. +# A \JSON value is one of the following: +# - Double-quoted text: <tt>"foo"</tt>. +# - Number: +1+, +1.0+, +2.0e2+. +# - Boolean: +true+, +false+. +# - Null: +null+. +# - \Array: an ordered list of values, enclosed by square brackets: +# ["foo", 1, 1.0, 2.0e2, true, false, null] # -# All strings, that are to be encoded as JSON strings, should be UTF-8 byte -# sequences on the Ruby side. To encode raw binary strings, that aren't UTF-8 -# encoded, please use the to_json_raw_object method of String (which produces -# an object, that contains a byte array) and decode the result on the receiving -# endpoint. +# - \Object: a collection of name/value pairs, enclosed by curly braces; +# each name is double-quoted text; +# the values may be any \JSON values: +# {"a": "foo", "b": 1, "c": 1.0, "d": 2.0e2, "e": true, "f": false, "g": null} # -# == Author +# A \JSON array or object may contain nested arrays, objects, and scalars +# to any depth: +# {"foo": {"bar": 1, "baz": 2}, "bat": [0, 1, 2]} +# [{"foo": 0, "bar": 1}, ["baz", 2]] # -# Florian Frank <mailto:flori@ping.de> +# == Using \Module \JSON # -# == License +# To make module \JSON available in your code, begin with: +# require 'json' # -# This software is distributed under the same license as Ruby itself, see -# http://www.ruby-lang.org/en/LICENSE.txt. +# All examples here assume that this has been done. +# +# === Parsing \JSON +# +# You can parse a \String containing \JSON data using +# either of two methods: +# - <tt>JSON.parse(source, opts)</tt> +# - <tt>JSON.parse!(source, opts)</tt> +# +# where +# - +source+ is a Ruby object. +# - +opts+ is a \Hash object containing options +# that control both input allowed and output formatting. +# +# The difference between the two methods +# is that JSON.parse! omits some checks +# and may not be safe for some +source+ data; +# use it only for data from trusted sources. +# Use the safer method JSON.parse for less trusted sources. +# +# ==== Parsing \JSON Arrays +# +# When +source+ is a \JSON array, JSON.parse by default returns a Ruby \Array: +# json = '["foo", 1, 1.0, 2.0e2, true, false, null]' +# ruby = JSON.parse(json) +# ruby # => ["foo", 1, 1.0, 200.0, true, false, nil] +# ruby.class # => Array +# +# The \JSON array may contain nested arrays, objects, and scalars +# to any depth: +# json = '[{"foo": 0, "bar": 1}, ["baz", 2]]' +# JSON.parse(json) # => [{"foo"=>0, "bar"=>1}, ["baz", 2]] +# +# ==== Parsing \JSON \Objects +# +# When the source is a \JSON object, JSON.parse by default returns a Ruby \Hash: +# json = '{"a": "foo", "b": 1, "c": 1.0, "d": 2.0e2, "e": true, "f": false, "g": null}' +# ruby = JSON.parse(json) +# ruby # => {"a"=>"foo", "b"=>1, "c"=>1.0, "d"=>200.0, "e"=>true, "f"=>false, "g"=>nil} +# ruby.class # => Hash +# +# The \JSON object may contain nested arrays, objects, and scalars +# to any depth: +# json = '{"foo": {"bar": 1, "baz": 2}, "bat": [0, 1, 2]}' +# JSON.parse(json) # => {"foo"=>{"bar"=>1, "baz"=>2}, "bat"=>[0, 1, 2]} +# +# ==== Parsing \JSON Scalars +# +# When the source is a \JSON scalar (not an array or object), +# JSON.parse returns a Ruby scalar. +# +# \String: +# ruby = JSON.parse('"foo"') +# ruby # => 'foo' +# ruby.class # => String +# \Integer: +# ruby = JSON.parse('1') +# ruby # => 1 +# ruby.class # => Integer +# \Float: +# ruby = JSON.parse('1.0') +# ruby # => 1.0 +# ruby.class # => Float +# ruby = JSON.parse('2.0e2') +# ruby # => 200 +# ruby.class # => Float +# Boolean: +# ruby = JSON.parse('true') +# ruby # => true +# ruby.class # => TrueClass +# ruby = JSON.parse('false') +# ruby # => false +# ruby.class # => FalseClass +# Null: +# ruby = JSON.parse('null') +# ruby # => nil +# ruby.class # => NilClass +# +# ==== Parsing Options +# +# ====== Input Options +# +# Option +max_nesting+ (\Integer) specifies the maximum nesting depth allowed; +# defaults to +100+; specify +false+ to disable depth checking. +# +# With the default, +false+: +# source = '[0, [1, [2, [3]]]]' +# ruby = JSON.parse(source) +# ruby # => [0, [1, [2, [3]]]] +# Too deep: +# # Raises JSON::NestingError (nesting of 2 is too deep): +# JSON.parse(source, {max_nesting: 1}) +# Bad value: +# # Raises TypeError (wrong argument type Symbol (expected Fixnum)): +# JSON.parse(source, {max_nesting: :foo}) +# +# --- +# +# Option +allow_duplicate_key+ specifies whether duplicate keys in objects +# should be ignored or cause an error to be raised: +# +# When not specified: +# # The last value is used and a deprecation warning emitted. +# JSON.parse('{"a": 1, "a":2}') => {"a" => 2} +# # warning: detected duplicate keys in JSON object. +# # This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true` +# +# When set to `+true+` +# # The last value is used. +# JSON.parse('{"a": 1, "a":2}') => {"a" => 2} +# +# When set to `+false+`, the future default: +# JSON.parse('{"a": 1, "a":2}') => duplicate key at line 1 column 1 (JSON::ParserError) +# +# --- +# +# Option +allow_nan+ (boolean) specifies whether to allow +# NaN, Infinity, and MinusInfinity in +source+; +# defaults to +false+. +# +# With the default, +false+: +# # Raises JSON::ParserError (225: unexpected token at '[NaN]'): +# JSON.parse('[NaN]') +# # Raises JSON::ParserError (232: unexpected token at '[Infinity]'): +# JSON.parse('[Infinity]') +# # Raises JSON::ParserError (248: unexpected token at '[-Infinity]'): +# JSON.parse('[-Infinity]') +# Allow: +# source = '[NaN, Infinity, -Infinity]' +# ruby = JSON.parse(source, {allow_nan: true}) +# ruby # => [NaN, Infinity, -Infinity] +# +# --- +# +# Option +allow_trailing_comma+ (boolean) specifies whether to allow +# trailing commas in objects and arrays; +# defaults to +false+. +# +# With the default, +false+: +# JSON.parse('[1,]') # unexpected character: ']' at line 1 column 4 (JSON::ParserError) +# +# When enabled: +# JSON.parse('[1,]', allow_trailing_comma: true) # => [1] +# +# --- +# +# Option +allow_control_characters+ (boolean) specifies whether to allow +# unescaped ASCII control characters, such as newlines, in strings; +# defaults to +false+. +# +# With the default, +false+: +# JSON.parse(%{"Hello\nWorld"}) # invalid ASCII control character in string (JSON::ParserError) +# +# When enabled: +# JSON.parse(%{"Hello\nWorld"}, allow_control_characters: true) # => "Hello\nWorld" +# +# --- +# +# Option +allow_invalid_escape+ (boolean) specifies whether to ignore backslahes that are followed +# by an invalid escape character in strings; +# defaults to +false+. +# +# With the default, +false+: +# JSON.parse('"Hell\o"') # invalid escape character in string (JSON::ParserError) +# +# When enabled: +# JSON.parse('"Hell\o"', allow_invalid_escape: true) # => "Hello" +# +# ====== Output Options +# +# Option +freeze+ (boolean) specifies whether the returned objects will be frozen; +# defaults to +false+. +# +# Option +symbolize_names+ (boolean) specifies whether returned \Hash keys +# should be Symbols; +# defaults to +false+ (use Strings). # -# == Download +# With the default, +false+: +# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}' +# ruby = JSON.parse(source) +# ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil} +# Use Symbols: +# ruby = JSON.parse(source, {symbolize_names: true}) +# ruby # => {:a=>"foo", :b=>1.0, :c=>true, :d=>false, :e=>nil} # -# The latest version of this library can be downloaded at +# --- +# +# Option +object_class+ (\Class) specifies the Ruby class to be used +# for each \JSON object; +# defaults to \Hash. # -# * http://rubyforge.org/frs?group_id=953 +# With the default, \Hash: +# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}' +# ruby = JSON.parse(source) +# ruby.class # => Hash +# Use class \OpenStruct: +# ruby = JSON.parse(source, {object_class: OpenStruct}) +# ruby # => #<OpenStruct a="foo", b=1.0, c=true, d=false, e=nil> +# +# --- # -# Online Documentation should be located at +# Option +array_class+ (\Class) specifies the Ruby class to be used +# for each \JSON array; +# defaults to \Array. # -# * http://json.rubyforge.org +# With the default, \Array: +# source = '["foo", 1.0, true, false, null]' +# ruby = JSON.parse(source) +# ruby.class # => Array +# Use class \Set: +# ruby = JSON.parse(source, {array_class: Set}) +# ruby # => #<Set: {"foo", 1.0, true, false, nil}> # -# == Usage -# -# To use JSON you can -# require 'json' -# to load the installed variant (either the extension 'json' or the pure -# variant 'json_pure'). If you have installed the extension variant, you can -# pick either the extension variant or the pure variant by typing -# require 'json/ext' -# or -# require 'json/pure' +# --- +# +# Option +create_additions+ (boolean) specifies whether to use \JSON additions in parsing. +# See {\JSON Additions}[#module-JSON-label-JSON+Additions]. +# +# === Generating \JSON +# +# To generate a Ruby \String containing \JSON data, +# use method <tt>JSON.generate(source, opts)</tt>, where +# - +source+ is a Ruby object. +# - +opts+ is a \Hash object containing options +# that control both input allowed and output formatting. +# +# ==== Generating \JSON from Arrays +# +# When the source is a Ruby \Array, JSON.generate returns +# a \String containing a \JSON array: +# ruby = [0, 's', :foo] +# json = JSON.generate(ruby) +# json # => '[0,"s","foo"]' # -# You can choose to load a set of common additions to ruby core's objects if -# you -# require 'json/add/core' +# The Ruby \Array array may contain nested arrays, hashes, and scalars +# to any depth: +# ruby = [0, [1, 2], {foo: 3, bar: 4}] +# json = JSON.generate(ruby) +# json # => '[0,[1,2],{"foo":3,"bar":4}]' # -# After requiring this you can, e. g., serialise/deserialise Ruby ranges: -# -# JSON JSON(1..10) # => 1..10 +# ==== Generating \JSON from Hashes # -# To find out how to add JSON support to other or your own classes, read the -# Examples section below. -# -# To get the best compatibility to rails' JSON implementation, you can -# require 'json/add/rails' -# -# Both of the additions attempt to require 'json' (like above) first, if it has -# not been required yet. -# -# == Speed Comparisons -# -# I have created some benchmark results (see the benchmarks subdir of the -# package) for the JSON-Parser to estimate the speed up in the C extension: -# -# JSON::Pure::Parser:: 28.90 calls/second -# JSON::Ext::Parser:: 505.50 calls/second -# -# This is ca. <b>17.5</b> times the speed of the pure Ruby implementation. -# -# I have benchmarked the JSON-Generator as well. This generates a few more -# values, because there are different modes, that also influence the achieved -# speed: -# -# * JSON::Pure::Generator: -# generate:: 35.06 calls/second -# pretty_generate:: 34.00 calls/second -# fast_generate:: 41.06 calls/second -# -# * JSON::Ext::Generator: -# generate:: 492.11 calls/second -# pretty_generate:: 348.85 calls/second -# fast_generate:: 541.60 calls/second -# -# * Speedup Ext/Pure: -# generate safe:: 14.0 times -# generate pretty:: 10.3 times -# generate fast:: 13.2 times -# -# The rails framework includes a generator as well, also it seems to be rather -# slow: I measured only 23.87 calls/second which is slower than any of my pure -# generator results. Here a comparison of the different speedups with the Rails -# measurement as the divisor: -# -# * Speedup Pure/Rails: -# generate safe:: 1.5 times -# generate pretty:: 1.4 times -# generate fast:: 1.7 times -# -# * Speedup Ext/Rails: -# generate safe:: 20.6 times -# generate pretty:: 14.6 times -# generate fast:: 22.7 times -# -# To achieve the fastest JSON text output, you can use the -# fast_generate/fast_unparse methods. Beware, that this will disable the -# checking for circular Ruby data structures, which may cause JSON to go into -# an infinite loop. -# -# == Examples -# -# To create a JSON text from a ruby data structure, you -# can call JSON.generate (or JSON.unparse) like that: -# -# json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10] -# # => "[1,2,{\"a\":3.141},false,true,null,\"4..10\"]" -# -# To create a valid JSON text you have to make sure, that the output is -# embedded in either a JSON array [] or a JSON object {}. The easiest way to do -# this, is by putting your values in a Ruby Array or Hash instance. -# -# To get back a ruby data structure from a JSON text, you have to call -# JSON.parse on it: -# -# JSON.parse json -# # => [1, 2, {"a"=>3.141}, false, true, nil, "4..10"] -# -# Note, that the range from the original data structure is a simple -# string now. The reason for this is, that JSON doesn't support ranges -# or arbitrary classes. In this case the json library falls back to call -# Object#to_json, which is the same as #to_s.to_json. -# -# It's possible to add JSON support serialization to arbitrary classes by -# simply implementing a more specialized version of the #to_json method, that -# should return a JSON object (a hash converted to JSON with #to_json) like -# this (don't forget the *a for all the arguments): -# -# class Range -# def to_json(*a) -# { -# 'json_class' => self.class.name, # = 'Range' -# 'data' => [ first, last, exclude_end? ] -# }.to_json(*a) -# end -# end -# -# The hash key 'json_class' is the class, that will be asked to deserialise the -# JSON representation later. In this case it's 'Range', but any namespace of -# the form 'A::B' or '::A::B' will do. All other keys are arbitrary and can be -# used to store the necessary data to configure the object to be deserialised. -# -# If a the key 'json_class' is found in a JSON object, the JSON parser checks -# if the given class responds to the json_create class method. If so, it is -# called with the JSON object converted to a Ruby hash. So a range can -# be deserialised by implementing Range.json_create like this: -# -# class Range -# def self.json_create(o) -# new(*o['data']) -# end -# end -# -# Now it possible to serialise/deserialise ranges as well: -# -# json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10] -# # => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]" -# JSON.parse json -# # => [1, 2, {"a"=>3.141}, false, true, nil, 4..10] -# -# JSON.generate always creates the shortest possible string representation of a -# ruby data structure in one line. This good for data storage or network -# protocols, but not so good for humans to read. Fortunately there's also -# JSON.pretty_generate (or JSON.pretty_generate) that creates a more -# readable output: -# -# puts JSON.pretty_generate([1, 2, {"a"=>3.141}, false, true, nil, 4..10]) -# [ -# 1, -# 2, -# { -# "a": 3.141 -# }, -# false, -# true, -# null, -# { -# "json_class": "Range", -# "data": [ -# 4, -# 10, -# false -# ] -# } -# ] -# -# There are also the methods Kernel#j for unparse, and Kernel#jj for -# pretty_unparse output to the console, that work analogous to Core Ruby's p -# and the pp library's pp methods. -# -# The script tools/server.rb contains a small example if you want to test, how -# receiving a JSON object from a webrick server in your browser with the -# javasript prototype library (http://www.prototypejs.org) works. +# When the source is a Ruby \Hash, JSON.generate returns +# a \String containing a \JSON object: +# ruby = {foo: 0, bar: 's', baz: :bat} +# json = JSON.generate(ruby) +# json # => '{"foo":0,"bar":"s","baz":"bat"}' +# +# The Ruby \Hash array may contain nested arrays, hashes, and scalars +# to any depth: +# ruby = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad} +# json = JSON.generate(ruby) +# json # => '{"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}' +# +# ==== Generating \JSON from Other Objects +# +# When the source is neither an \Array nor a \Hash, +# the generated \JSON data depends on the class of the source. +# +# When the source is a Ruby \Integer or \Float, JSON.generate returns +# a \String containing a \JSON number: +# JSON.generate(42) # => '42' +# JSON.generate(0.42) # => '0.42' +# +# When the source is a Ruby \String, JSON.generate returns +# a \String containing a \JSON string (with double-quotes): +# JSON.generate('A string') # => '"A string"' +# +# When the source is +true+, +false+ or +nil+, JSON.generate returns +# a \String containing the corresponding \JSON token: +# JSON.generate(true) # => 'true' +# JSON.generate(false) # => 'false' +# JSON.generate(nil) # => 'null' +# +# When the source is none of the above, JSON.generate returns +# a \String containing a \JSON string representation of the source: +# JSON.generate(:foo) # => '"foo"' +# JSON.generate(Complex(0, 0)) # => '"0+0i"' +# JSON.generate(Dir.new('.')) # => '"#<Dir>"' +# +# ==== Generating Options +# +# ====== Input Options +# +# Option +allow_nan+ (boolean) specifies whether +# +NaN+, +Infinity+, and <tt>-Infinity</tt> may be generated; +# defaults to +false+. +# +# With the default, +false+: +# # Raises JSON::GeneratorError (920: NaN not allowed in JSON): +# JSON.generate(JSON::NaN) +# # Raises JSON::GeneratorError (917: Infinity not allowed in JSON): +# JSON.generate(JSON::Infinity) +# # Raises JSON::GeneratorError (917: -Infinity not allowed in JSON): +# JSON.generate(JSON::MinusInfinity) +# +# Allow: +# ruby = [Float::NAN, Float::INFINITY, JSON::NaN, JSON::Infinity, JSON::MinusInfinity] +# JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,NaN,Infinity,-Infinity]' +# +# --- +# +# Option +allow_duplicate_key+ (boolean) specifies whether +# hashes with duplicate keys should be allowed or produce an error. +# defaults to emit a deprecation warning. +# +# With the default, (not set): +# Warning[:deprecated] = true +# JSON.generate({ foo: 1, "foo" => 2 }) +# # warning: detected duplicate key "foo" in {foo: 1, "foo" => 2}. +# # This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true` +# # => '{"foo":1,"foo":2}' +# +# With <tt>false</tt> +# JSON.generate({ foo: 1, "foo" => 2 }, allow_duplicate_key: false) +# # detected duplicate key "foo" in {foo: 1, "foo" => 2} (JSON::GeneratorError) +# +# In version 3.0, <tt>false</tt> will become the default. +# +# --- +# +# Option +max_nesting+ (\Integer) specifies the maximum nesting depth +# in +obj+; defaults to +100+. +# +# With the default, +100+: +# obj = [[[[[[0]]]]]] +# JSON.generate(obj) # => '[[[[[[0]]]]]]' +# +# Too deep: +# # Raises JSON::NestingError (nesting of 2 is too deep): +# JSON.generate(obj, max_nesting: 2) +# +# ====== Escaping Options +# +# Options +script_safe+ (boolean) specifies wether <tt>'\u2028'</tt>, <tt>'\u2029'</tt> +# and <tt>'/'</tt> should be escaped as to make the JSON object safe to interpolate in script +# tags. +# +# Options +ascii_only+ (boolean) specifies wether all characters outside the ASCII range +# should be escaped. +# +# ====== Output Options +# +# The default formatting options generate the most compact +# \JSON data, all on one line and with no whitespace. +# +# You can use these formatting options to generate +# \JSON data in a more open format, using whitespace. +# See also JSON.pretty_generate. +# +# - Option +array_nl+ (\String) specifies a string (usually a newline) +# to be inserted after each \JSON array; defaults to the empty \String, <tt>''</tt>. +# - Option +object_nl+ (\String) specifies a string (usually a newline) +# to be inserted after each \JSON object; defaults to the empty \String, <tt>''</tt>. +# - Option +indent+ (\String) specifies the string (usually spaces) to be +# used for indentation; defaults to the empty \String, <tt>''</tt>; +# defaults to the empty \String, <tt>''</tt>; +# has no effect unless options +array_nl+ or +object_nl+ specify newlines. +# - Option +space+ (\String) specifies a string (usually a space) to be +# inserted after the colon in each \JSON object's pair; +# defaults to the empty \String, <tt>''</tt>. +# - Option +space_before+ (\String) specifies a string (usually a space) to be +# inserted before the colon in each \JSON object's pair; +# defaults to the empty \String, <tt>''</tt>. +# +# In this example, +obj+ is used first to generate the shortest +# \JSON data (no whitespace), then again with all formatting options +# specified: +# +# obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}} +# json = JSON.generate(obj) +# puts 'Compact:', json +# opts = { +# array_nl: "\n", +# object_nl: "\n", +# indent: ' ', +# space_before: ' ', +# space: ' ' +# } +# puts 'Open:', JSON.generate(obj, opts) +# +# Output: +# Compact: +# {"foo":["bar","baz"],"bat":{"bam":0,"bad":1}} +# Open: +# { +# "foo" : [ +# "bar", +# "baz" +# ], +# "bat" : { +# "bam" : 0, +# "bad" : 1 +# } +# } +# +# == \JSON Additions +# +# Note that JSON Additions must only be used with trusted data, and is +# deprecated. +# +# When you "round trip" a non-\String object from Ruby to \JSON and back, +# you have a new \String, instead of the object you began with: +# ruby0 = Range.new(0, 2) +# json = JSON.generate(ruby0) +# json # => '0..2"' +# ruby1 = JSON.parse(json) +# ruby1 # => '0..2' +# ruby1.class # => String +# +# You can use \JSON _additions_ to preserve the original object. +# The addition is an extension of a ruby class, so that: +# - \JSON.generate stores more information in the \JSON string. +# - \JSON.parse, called with option +create_additions+, +# uses that information to create a proper Ruby object. +# +# This example shows a \Range being generated into \JSON +# and parsed back into Ruby, both without and with +# the addition for \Range: +# ruby = Range.new(0, 2) +# # This passage does not use the addition for Range. +# json0 = JSON.generate(ruby) +# ruby0 = JSON.parse(json0) +# # This passage uses the addition for Range. +# require 'json/add/range' +# json1 = JSON.generate(ruby) +# ruby1 = JSON.parse(json1, create_additions: true) +# # Make a nice display. +# display = <<~EOT +# Generated JSON: +# Without addition: #{json0} (#{json0.class}) +# With addition: #{json1} (#{json1.class}) +# Parsed JSON: +# Without addition: #{ruby0.inspect} (#{ruby0.class}) +# With addition: #{ruby1.inspect} (#{ruby1.class}) +# EOT +# puts display +# +# This output shows the different results: +# Generated JSON: +# Without addition: "0..2" (String) +# With addition: {"json_class":"Range","a":[0,2,false]} (String) +# Parsed JSON: +# Without addition: "0..2" (String) +# With addition: 0..2 (Range) +# +# The \JSON module includes additions for certain classes. +# You can also craft custom additions. +# See {Custom \JSON Additions}[#module-JSON-label-Custom+JSON+Additions]. +# +# === Built-in Additions +# +# The \JSON module includes additions for certain classes. +# To use an addition, +require+ its source: +# - BigDecimal: <tt>require 'json/add/bigdecimal'</tt> +# - Complex: <tt>require 'json/add/complex'</tt> +# - Date: <tt>require 'json/add/date'</tt> +# - DateTime: <tt>require 'json/add/date_time'</tt> +# - Exception: <tt>require 'json/add/exception'</tt> +# - OpenStruct: <tt>require 'json/add/ostruct'</tt> +# - Range: <tt>require 'json/add/range'</tt> +# - Rational: <tt>require 'json/add/rational'</tt> +# - Regexp: <tt>require 'json/add/regexp'</tt> +# - Set: <tt>require 'json/add/set'</tt> +# - Struct: <tt>require 'json/add/struct'</tt> +# - Symbol: <tt>require 'json/add/symbol'</tt> +# - Time: <tt>require 'json/add/time'</tt> +# +# To reduce punctuation clutter, the examples below +# show the generated \JSON via +puts+, rather than the usual +inspect+, +# +# \BigDecimal: +# require 'json/add/bigdecimal' +# ruby0 = BigDecimal(0) # 0.0 +# json = JSON.generate(ruby0) # {"json_class":"BigDecimal","b":"27:0.0"} +# ruby1 = JSON.parse(json, create_additions: true) # 0.0 +# ruby1.class # => BigDecimal +# +# \Complex: +# require 'json/add/complex' +# ruby0 = Complex(1+0i) # 1+0i +# json = JSON.generate(ruby0) # {"json_class":"Complex","r":1,"i":0} +# ruby1 = JSON.parse(json, create_additions: true) # 1+0i +# ruby1.class # Complex +# +# \Date: +# require 'json/add/date' +# ruby0 = Date.today # 2020-05-02 +# json = JSON.generate(ruby0) # {"json_class":"Date","y":2020,"m":5,"d":2,"sg":2299161.0} +# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02 +# ruby1.class # Date +# +# \DateTime: +# require 'json/add/date_time' +# ruby0 = DateTime.now # 2020-05-02T10:38:13-05:00 +# 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} +# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02T10:38:13-05:00 +# ruby1.class # DateTime +# +# \Exception (and its subclasses including \RuntimeError): +# require 'json/add/exception' +# ruby0 = Exception.new('A message') # A message +# json = JSON.generate(ruby0) # {"json_class":"Exception","m":"A message","b":null} +# ruby1 = JSON.parse(json, create_additions: true) # A message +# ruby1.class # Exception +# ruby0 = RuntimeError.new('Another message') # Another message +# json = JSON.generate(ruby0) # {"json_class":"RuntimeError","m":"Another message","b":null} +# ruby1 = JSON.parse(json, create_additions: true) # Another message +# ruby1.class # RuntimeError +# +# \OpenStruct: +# require 'json/add/ostruct' +# ruby0 = OpenStruct.new(name: 'Matz', language: 'Ruby') # #<OpenStruct name="Matz", language="Ruby"> +# json = JSON.generate(ruby0) # {"json_class":"OpenStruct","t":{"name":"Matz","language":"Ruby"}} +# ruby1 = JSON.parse(json, create_additions: true) # #<OpenStruct name="Matz", language="Ruby"> +# ruby1.class # OpenStruct +# +# \Range: +# require 'json/add/range' +# ruby0 = Range.new(0, 2) # 0..2 +# json = JSON.generate(ruby0) # {"json_class":"Range","a":[0,2,false]} +# ruby1 = JSON.parse(json, create_additions: true) # 0..2 +# ruby1.class # Range +# +# \Rational: +# require 'json/add/rational' +# ruby0 = Rational(1, 3) # 1/3 +# json = JSON.generate(ruby0) # {"json_class":"Rational","n":1,"d":3} +# ruby1 = JSON.parse(json, create_additions: true) # 1/3 +# ruby1.class # Rational +# +# \Regexp: +# require 'json/add/regexp' +# ruby0 = Regexp.new('foo') # (?-mix:foo) +# json = JSON.generate(ruby0) # {"json_class":"Regexp","o":0,"s":"foo"} +# ruby1 = JSON.parse(json, create_additions: true) # (?-mix:foo) +# ruby1.class # Regexp +# +# \Set: +# require 'json/add/set' +# ruby0 = Set.new([0, 1, 2]) # #<Set: {0, 1, 2}> +# json = JSON.generate(ruby0) # {"json_class":"Set","a":[0,1,2]} +# ruby1 = JSON.parse(json, create_additions: true) # #<Set: {0, 1, 2}> +# ruby1.class # Set +# +# \Struct: +# require 'json/add/struct' +# Customer = Struct.new(:name, :address) # Customer +# ruby0 = Customer.new("Dave", "123 Main") # #<struct Customer name="Dave", address="123 Main"> +# json = JSON.generate(ruby0) # {"json_class":"Customer","v":["Dave","123 Main"]} +# ruby1 = JSON.parse(json, create_additions: true) # #<struct Customer name="Dave", address="123 Main"> +# ruby1.class # Customer +# +# \Symbol: +# require 'json/add/symbol' +# ruby0 = :foo # foo +# json = JSON.generate(ruby0) # {"json_class":"Symbol","s":"foo"} +# ruby1 = JSON.parse(json, create_additions: true) # foo +# ruby1.class # Symbol +# +# \Time: +# require 'json/add/time' +# ruby0 = Time.now # 2020-05-02 11:28:26 -0500 +# json = JSON.generate(ruby0) # {"json_class":"Time","s":1588436906,"n":840560000} +# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02 11:28:26 -0500 +# ruby1.class # Time +# +# +# === Custom \JSON Additions +# +# In addition to the \JSON additions provided, +# you can craft \JSON additions of your own, +# either for Ruby built-in classes or for user-defined classes. +# +# Here's a user-defined class +Foo+: +# class Foo +# attr_accessor :bar, :baz +# def initialize(bar, baz) +# self.bar = bar +# self.baz = baz +# end +# end +# +# Here's the \JSON addition for it: +# # Extend class Foo with JSON addition. +# class Foo +# # Serialize Foo object with its class name and arguments +# def to_json(*args) +# { +# JSON.create_id => self.class.name, +# 'a' => [ bar, baz ] +# }.to_json(*args) +# end +# # Deserialize JSON string by constructing new Foo object with arguments. +# def self.json_create(object) +# new(*object['a']) +# end +# end +# +# Demonstration: +# require 'json' +# # This Foo object has no custom addition. +# foo0 = Foo.new(0, 1) +# json0 = JSON.generate(foo0) +# obj0 = JSON.parse(json0) +# # Lood the custom addition. +# require_relative 'foo_addition' +# # This foo has the custom addition. +# foo1 = Foo.new(0, 1) +# json1 = JSON.generate(foo1) +# obj1 = JSON.parse(json1, create_additions: true) +# # Make a nice display. +# display = <<~EOT +# Generated JSON: +# Without custom addition: #{json0} (#{json0.class}) +# With custom addition: #{json1} (#{json1.class}) +# Parsed JSON: +# Without custom addition: #{obj0.inspect} (#{obj0.class}) +# With custom addition: #{obj1.inspect} (#{obj1.class}) +# EOT +# puts display +# +# Output: +# +# Generated JSON: +# Without custom addition: "#<Foo:0x0000000006534e80>" (String) +# With custom addition: {"json_class":"Foo","a":[0,1]} (String) +# Parsed JSON: +# Without custom addition: "#<Foo:0x0000000006534e80>" (String) +# With custom addition: #<Foo:0x0000000006473bb8 @bar=0, @baz=1> (Foo) # module JSON require 'json/version' - - if VARIANT_BINARY - require 'json/ext' - else - begin - require 'json/ext' - rescue LoadError - require 'json/pure' - end - end - - JSON_LOADED = true + require 'json/ext' end diff --git a/ext/json/lib/json/Array.xpm b/ext/json/lib/json/Array.xpm deleted file mode 100644 index 27c48011f9..0000000000 --- a/ext/json/lib/json/Array.xpm +++ /dev/null @@ -1,21 +0,0 @@ -/* XPM */ -static char * Array_xpm[] = { -"16 16 2 1", -" c None", -". c #000000", -" ", -" ", -" ", -" .......... ", -" . . ", -" . . ", -" . . ", -" . . ", -" . . ", -" . . ", -" . . ", -" . . ", -" .......... ", -" ", -" ", -" "}; diff --git a/ext/json/lib/json/FalseClass.xpm b/ext/json/lib/json/FalseClass.xpm deleted file mode 100644 index 25ce60832d..0000000000 --- a/ext/json/lib/json/FalseClass.xpm +++ /dev/null @@ -1,21 +0,0 @@ -/* XPM */ -static char * False_xpm[] = { -"16 16 2 1", -" c None", -". c #FF0000", -" ", -" ", -" ", -" ...... ", -" . ", -" . ", -" . ", -" ...... ", -" . ", -" . ", -" . ", -" . ", -" . ", -" ", -" ", -" "}; diff --git a/ext/json/lib/json/Hash.xpm b/ext/json/lib/json/Hash.xpm deleted file mode 100644 index cd8f6f7b53..0000000000 --- a/ext/json/lib/json/Hash.xpm +++ /dev/null @@ -1,21 +0,0 @@ -/* XPM */ -static char * Hash_xpm[] = { -"16 16 2 1", -" c None", -". c #000000", -" ", -" ", -" ", -" . . ", -" . . ", -" . . ", -" ......... ", -" . . ", -" . . ", -" ......... ", -" . . ", -" . . ", -" . . ", -" ", -" ", -" "}; diff --git a/ext/json/lib/json/Key.xpm b/ext/json/lib/json/Key.xpm deleted file mode 100644 index 9fd7281388..0000000000 --- a/ext/json/lib/json/Key.xpm +++ /dev/null @@ -1,73 +0,0 @@ -/* XPM */ -static char * Key_xpm[] = { -"16 16 54 1", -" c None", -". c #110007", -"+ c #0E0900", -"@ c #000013", -"# c #070600", -"$ c #F6F006", -"% c #ECE711", -"& c #E5EE00", -"* c #16021E", -"= c #120900", -"- c #EDF12B", -"; c #000033", -"> c #0F0000", -", c #FFFE03", -"' c #E6E500", -") c #16021B", -"! c #F7F502", -"~ c #000E00", -"{ c #130000", -"] c #FFF000", -"^ c #FFE711", -"/ c #140005", -"( c #190025", -"_ c #E9DD27", -": c #E7DC04", -"< c #FFEC09", -"[ c #FFE707", -"} c #FFDE10", -"| c #150021", -"1 c #160700", -"2 c #FAF60E", -"3 c #EFE301", -"4 c #FEF300", -"5 c #E7E000", -"6 c #FFFF08", -"7 c #0E0206", -"8 c #040000", -"9 c #03052E", -"0 c #041212", -"a c #070300", -"b c #F2E713", -"c c #F9DE13", -"d c #36091E", -"e c #00001C", -"f c #1F0010", -"g c #FFF500", -"h c #DEDE00", -"i c #050A00", -"j c #FAF14A", -"k c #F5F200", -"l c #040404", -"m c #1A0D00", -"n c #EDE43D", -"o c #ECE007", -" ", -" ", -" .+@ ", -" #$%&* ", -" =-;>,') ", -" >!~{]^/ ", -" (_:<[}| ", -" 1234567 ", -" 890abcd ", -" efghi ", -" >jkl ", -" mnol ", -" >kl ", -" ll ", -" ", -" "}; diff --git a/ext/json/lib/json/NilClass.xpm b/ext/json/lib/json/NilClass.xpm deleted file mode 100644 index 3509f06c99..0000000000 --- a/ext/json/lib/json/NilClass.xpm +++ /dev/null @@ -1,21 +0,0 @@ -/* XPM */ -static char * False_xpm[] = { -"16 16 2 1", -" c None", -". c #000000", -" ", -" ", -" ", -" ... ", -" . . ", -" . . ", -" . . ", -" . . ", -" . . ", -" . . ", -" . . ", -" . . ", -" ... ", -" ", -" ", -" "}; diff --git a/ext/json/lib/json/Numeric.xpm b/ext/json/lib/json/Numeric.xpm deleted file mode 100644 index e071e2ee9c..0000000000 --- a/ext/json/lib/json/Numeric.xpm +++ /dev/null @@ -1,28 +0,0 @@ -/* XPM */ -static char * Numeric_xpm[] = { -"16 16 9 1", -" c None", -". c #FF0000", -"+ c #0000FF", -"@ c #0023DB", -"# c #00EA14", -"$ c #00FF00", -"% c #004FAF", -"& c #0028D6", -"* c #00F20C", -" ", -" ", -" ", -" ... +++@#$$$$ ", -" .+ %& $$ ", -" . + $ ", -" . + $$ ", -" . ++$$$$ ", -" . + $$ ", -" . + $ ", -" . + $ ", -" . + $ $$ ", -" .....++++*$$ ", -" ", -" ", -" "}; diff --git a/ext/json/lib/json/String.xpm b/ext/json/lib/json/String.xpm deleted file mode 100644 index f79a89cdc1..0000000000 --- a/ext/json/lib/json/String.xpm +++ /dev/null @@ -1,96 +0,0 @@ -/* XPM */ -static char * String_xpm[] = { -"16 16 77 1", -" c None", -". c #000000", -"+ c #040404", -"@ c #080806", -"# c #090606", -"$ c #EEEAE1", -"% c #E7E3DA", -"& c #E0DBD1", -"* c #D4B46F", -"= c #0C0906", -"- c #E3C072", -"; c #E4C072", -"> c #060505", -", c #0B0A08", -"' c #D5B264", -") c #D3AF5A", -"! c #080602", -"~ c #E1B863", -"{ c #DDB151", -"] c #DBAE4A", -"^ c #DDB152", -"/ c #DDB252", -"( c #070705", -"_ c #0C0A07", -": c #D3A33B", -"< c #020201", -"[ c #DAAA41", -"} c #040302", -"| c #E4D9BF", -"1 c #0B0907", -"2 c #030201", -"3 c #020200", -"4 c #C99115", -"5 c #080704", -"6 c #DBC8A2", -"7 c #E7D7B4", -"8 c #E0CD9E", -"9 c #080601", -"0 c #040400", -"a c #010100", -"b c #0B0B08", -"c c #DCBF83", -"d c #DCBC75", -"e c #DEB559", -"f c #040301", -"g c #BC8815", -"h c #120E07", -"i c #060402", -"j c #0A0804", -"k c #D4A747", -"l c #D6A12F", -"m c #0E0C05", -"n c #C8C1B0", -"o c #1D1B15", -"p c #D7AD51", -"q c #070502", -"r c #080804", -"s c #BC953B", -"t c #C4BDAD", -"u c #0B0807", -"v c #DBAC47", -"w c #1B150A", -"x c #B78A2C", -"y c #D8A83C", -"z c #D4A338", -"A c #0F0B03", -"B c #181105", -"C c #C59325", -"D c #C18E1F", -"E c #060600", -"F c #CC992D", -"G c #B98B25", -"H c #B3831F", -"I c #C08C1C", -"J c #060500", -"K c #0E0C03", -"L c #0D0A00", -" ", -" .+@# ", -" .$%&*= ", -" .-;>,')! ", -" .~. .{]. ", -" .^/. (_:< ", -" .[.}|$12 ", -" 345678}90 ", -" a2bcdefgh ", -" ijkl.mno ", -" <pq. rstu ", -" .]v. wx= ", -" .yzABCDE ", -" .FGHIJ ", -" 0KL0 ", -" "}; diff --git a/ext/json/lib/json/TrueClass.xpm b/ext/json/lib/json/TrueClass.xpm deleted file mode 100644 index 143eef49bb..0000000000 --- a/ext/json/lib/json/TrueClass.xpm +++ /dev/null @@ -1,21 +0,0 @@ -/* XPM */ -static char * TrueClass_xpm[] = { -"16 16 2 1", -" c None", -". c #0BF311", -" ", -" ", -" ", -" ......... ", -" . ", -" . ", -" . ", -" . ", -" . ", -" . ", -" . ", -" . ", -" . ", -" ", -" ", -" "}; diff --git a/ext/json/lib/json/add/bigdecimal.rb b/ext/json/lib/json/add/bigdecimal.rb new file mode 100644 index 0000000000..dc84572f31 --- /dev/null +++ b/ext/json/lib/json/add/bigdecimal.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end +begin + require 'bigdecimal' +rescue LoadError +end + +class BigDecimal + + # See #as_json. + def self.json_create(object) + BigDecimal._load object['b'] + end + + # Methods <tt>BigDecimal#as_json</tt> and +BigDecimal.json_create+ may be used + # to serialize and deserialize a \BigDecimal object; + # see Marshal[rdoc-ref:Marshal]. + # + # \Method <tt>BigDecimal#as_json</tt> serializes +self+, + # returning a 2-element hash representing +self+: + # + # require 'json/add/bigdecimal' + # x = BigDecimal(2).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"} + # y = BigDecimal(2.0, 4).as_json # => {"json_class"=>"BigDecimal", "b"=>"36:0.2e1"} + # z = BigDecimal(Complex(2, 0)).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"} + # + # \Method +JSON.create+ deserializes such a hash, returning a \BigDecimal object: + # + # BigDecimal.json_create(x) # => 0.2e1 + # BigDecimal.json_create(y) # => 0.2e1 + # BigDecimal.json_create(z) # => 0.2e1 + # + def as_json(*) + { + JSON.create_id => self.class.name, + 'b' => _dump.force_encoding(Encoding::UTF_8), + } + end + + # Returns a JSON string representing +self+: + # + # require 'json/add/bigdecimal' + # puts BigDecimal(2).to_json + # puts BigDecimal(2.0, 4).to_json + # puts BigDecimal(Complex(2, 0)).to_json + # + # Output: + # + # {"json_class":"BigDecimal","b":"27:0.2e1"} + # {"json_class":"BigDecimal","b":"36:0.2e1"} + # {"json_class":"BigDecimal","b":"27:0.2e1"} + # + def to_json(*args) + as_json.to_json(*args) + end +end if defined?(::BigDecimal) diff --git a/ext/json/lib/json/add/complex.rb b/ext/json/lib/json/add/complex.rb new file mode 100644 index 0000000000..9e3c6f2d0a --- /dev/null +++ b/ext/json/lib/json/add/complex.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end + +class Complex + + # See #as_json. + def self.json_create(object) + Complex(object['r'], object['i']) + end + + # Methods <tt>Complex#as_json</tt> and +Complex.json_create+ may be used + # to serialize and deserialize a \Complex object; + # see Marshal[rdoc-ref:Marshal]. + # + # \Method <tt>Complex#as_json</tt> serializes +self+, + # returning a 2-element hash representing +self+: + # + # require 'json/add/complex' + # x = Complex(2).as_json # => {"json_class"=>"Complex", "r"=>2, "i"=>0} + # y = Complex(2.0, 4).as_json # => {"json_class"=>"Complex", "r"=>2.0, "i"=>4} + # + # \Method +JSON.create+ deserializes such a hash, returning a \Complex object: + # + # Complex.json_create(x) # => (2+0i) + # Complex.json_create(y) # => (2.0+4i) + # + def as_json(*) + { + JSON.create_id => self.class.name, + 'r' => real, + 'i' => imag, + } + end + + # Returns a JSON string representing +self+: + # + # require 'json/add/complex' + # puts Complex(2).to_json + # puts Complex(2.0, 4).to_json + # + # Output: + # + # {"json_class":"Complex","r":2,"i":0} + # {"json_class":"Complex","r":2.0,"i":4} + # + def to_json(*args) + as_json.to_json(*args) + end +end diff --git a/ext/json/lib/json/add/core.rb b/ext/json/lib/json/add/core.rb index 7121a77ff1..61ff454212 100644 --- a/ext/json/lib/json/add/core.rb +++ b/ext/json/lib/json/add/core.rb @@ -1,135 +1,13 @@ -# This file contains implementations of ruby core's custom objects for +# frozen_string_literal: true +# This file requires the implementations of ruby core's custom objects for # serialisation/deserialisation. -unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and - ::JSON::JSON_LOADED - require 'json' -end -require 'date' - -class Time - def self.json_create(object) - if usec = object.delete('u') # used to be tv_usec -> tv_nsec - object['n'] = usec * 1000 - end - if respond_to?(:tv_nsec) - at(*object.values_at('s', 'n')) - else - at(object['s'], object['n'] / 1000) - end - end - - def to_json(*args) - { - 'json_class' => self.class.name, - 's' => tv_sec, - 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000 - }.to_json(*args) - end -end - -class Date - def self.json_create(object) - civil(*object.values_at('y', 'm', 'd', 'sg')) - end - - alias start sg unless method_defined?(:start) - - def to_json(*args) - { - 'json_class' => self.class.name, - 'y' => year, - 'm' => month, - 'd' => day, - 'sg' => start, - }.to_json(*args) - end -end - -class DateTime - def self.json_create(object) - args = object.values_at('y', 'm', 'd', 'H', 'M', 'S') - of_a, of_b = object['of'].split('/') - if of_b and of_b != '0' - args << Rational(of_a.to_i, of_b.to_i) - else - args << of_a - end - args << object['sg'] - civil(*args) - end - - alias start sg unless method_defined?(:start) - - def to_json(*args) - { - 'json_class' => self.class.name, - 'y' => year, - 'm' => month, - 'd' => day, - 'H' => hour, - 'M' => min, - 'S' => sec, - 'of' => offset.to_s, - 'sg' => start, - }.to_json(*args) - end -end - -class Range - def self.json_create(object) - new(*object['a']) - end - - def to_json(*args) - { - 'json_class' => self.class.name, - 'a' => [ first, last, exclude_end? ] - }.to_json(*args) - end -end - -class Struct - def self.json_create(object) - new(*object['v']) - end - - def to_json(*args) - klass = self.class.name - klass.nil? and raise JSON::JSONError, "Only named structs are supported!" - { - 'json_class' => klass, - 'v' => values, - }.to_json(*args) - end -end - -class Exception - def self.json_create(object) - result = new(object['m']) - result.set_backtrace object['b'] - result - end - - def to_json(*args) - { - 'json_class' => self.class.name, - 'm' => message, - 'b' => backtrace, - }.to_json(*args) - end -end - -class Regexp - def self.json_create(object) - new(object['s'], object['o']) - end - - def to_json(*) - { - 'json_class' => self.class.name, - 'o' => options, - 's' => source, - }.to_json - end -end +require 'json/add/date' +require 'json/add/date_time' +require 'json/add/exception' +require 'json/add/range' +require 'json/add/regexp' +require 'json/add/string' +require 'json/add/struct' +require 'json/add/symbol' +require 'json/add/time' diff --git a/ext/json/lib/json/add/date.rb b/ext/json/lib/json/add/date.rb new file mode 100644 index 0000000000..88a098b637 --- /dev/null +++ b/ext/json/lib/json/add/date.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end +require 'date' + +class Date + + # See #as_json. + def self.json_create(object) + civil(*object.values_at('y', 'm', 'd', 'sg')) + end + + alias start sg unless method_defined?(:start) + + # Methods <tt>Date#as_json</tt> and +Date.json_create+ may be used + # to serialize and deserialize a \Date object; + # see Marshal[rdoc-ref:Marshal]. + # + # \Method <tt>Date#as_json</tt> serializes +self+, + # returning a 2-element hash representing +self+: + # + # require 'json/add/date' + # x = Date.today.as_json + # # => {"json_class"=>"Date", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0} + # + # \Method +JSON.create+ deserializes such a hash, returning a \Date object: + # + # Date.json_create(x) + # # => #<Date: 2023-11-21 ((2460270j,0s,0n),+0s,2299161j)> + # + def as_json(*) + { + JSON.create_id => self.class.name, + 'y' => year, + 'm' => month, + 'd' => day, + 'sg' => start, + } + end + + # Returns a JSON string representing +self+: + # + # require 'json/add/date' + # puts Date.today.to_json + # + # Output: + # + # {"json_class":"Date","y":2023,"m":11,"d":21,"sg":2299161.0} + # + def to_json(*args) + as_json.to_json(*args) + end +end diff --git a/ext/json/lib/json/add/date_time.rb b/ext/json/lib/json/add/date_time.rb new file mode 100644 index 0000000000..8b0bb5d181 --- /dev/null +++ b/ext/json/lib/json/add/date_time.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end +require 'date' + +class DateTime + + # See #as_json. + def self.json_create(object) + args = object.values_at('y', 'm', 'd', 'H', 'M', 'S') + of_a, of_b = object['of'].split('/') + if of_b and of_b != '0' + args << Rational(of_a.to_i, of_b.to_i) + else + args << of_a + end + args << object['sg'] + civil(*args) + end + + alias start sg unless method_defined?(:start) + + # Methods <tt>DateTime#as_json</tt> and +DateTime.json_create+ may be used + # to serialize and deserialize a \DateTime object; + # see Marshal[rdoc-ref:Marshal]. + # + # \Method <tt>DateTime#as_json</tt> serializes +self+, + # returning a 2-element hash representing +self+: + # + # require 'json/add/datetime' + # x = DateTime.now.as_json + # # => {"json_class"=>"DateTime", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0} + # + # \Method +JSON.create+ deserializes such a hash, returning a \DateTime object: + # + # DateTime.json_create(x) # BUG? Raises Date::Error "invalid date" + # + def as_json(*) + { + JSON.create_id => self.class.name, + 'y' => year, + 'm' => month, + 'd' => day, + 'H' => hour, + 'M' => min, + 'S' => sec, + 'of' => offset.to_s, + 'sg' => start, + } + end + + # Returns a JSON string representing +self+: + # + # require 'json/add/datetime' + # puts DateTime.now.to_json + # + # Output: + # + # {"json_class":"DateTime","y":2023,"m":11,"d":21,"sg":2299161.0} + # + def to_json(*args) + as_json.to_json(*args) + end +end + + diff --git a/ext/json/lib/json/add/exception.rb b/ext/json/lib/json/add/exception.rb new file mode 100644 index 0000000000..e85d404982 --- /dev/null +++ b/ext/json/lib/json/add/exception.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end + +class Exception + + # See #as_json. + def self.json_create(object) + result = new(object['m']) + result.set_backtrace object['b'] + result + end + + # Methods <tt>Exception#as_json</tt> and +Exception.json_create+ may be used + # to serialize and deserialize a \Exception object; + # see Marshal[rdoc-ref:Marshal]. + # + # \Method <tt>Exception#as_json</tt> serializes +self+, + # returning a 2-element hash representing +self+: + # + # require 'json/add/exception' + # x = Exception.new('Foo').as_json # => {"json_class"=>"Exception", "m"=>"Foo", "b"=>nil} + # + # \Method +JSON.create+ deserializes such a hash, returning a \Exception object: + # + # Exception.json_create(x) # => #<Exception: Foo> + # + def as_json(*) + { + JSON.create_id => self.class.name, + 'm' => message, + 'b' => backtrace, + } + end + + # Returns a JSON string representing +self+: + # + # require 'json/add/exception' + # puts Exception.new('Foo').to_json + # + # Output: + # + # {"json_class":"Exception","m":"Foo","b":null} + # + def to_json(*args) + as_json.to_json(*args) + end +end diff --git a/ext/json/lib/json/add/ostruct.rb b/ext/json/lib/json/add/ostruct.rb new file mode 100644 index 0000000000..7750498144 --- /dev/null +++ b/ext/json/lib/json/add/ostruct.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end +begin + require 'ostruct' +rescue LoadError +end + +class OpenStruct + + # See #as_json. + def self.json_create(object) + new(object['t'] || object[:t]) + end + + # Methods <tt>OpenStruct#as_json</tt> and +OpenStruct.json_create+ may be used + # to serialize and deserialize a \OpenStruct object; + # see Marshal[rdoc-ref:Marshal]. + # + # \Method <tt>OpenStruct#as_json</tt> serializes +self+, + # returning a 2-element hash representing +self+: + # + # require 'json/add/ostruct' + # x = OpenStruct.new('name' => 'Rowdy', :age => nil).as_json + # # => {"json_class"=>"OpenStruct", "t"=>{:name=>'Rowdy', :age=>nil}} + # + # \Method +JSON.create+ deserializes such a hash, returning a \OpenStruct object: + # + # OpenStruct.json_create(x) + # # => #<OpenStruct name='Rowdy', age=nil> + # + def as_json(*) + klass = self.class.name + klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!" + { + JSON.create_id => klass, + 't' => table, + } + end + + # Returns a JSON string representing +self+: + # + # require 'json/add/ostruct' + # puts OpenStruct.new('name' => 'Rowdy', :age => nil).to_json + # + # Output: + # + # {"json_class":"OpenStruct","t":{'name':'Rowdy',"age":null}} + # + def to_json(*args) + as_json.to_json(*args) + end +end if defined?(::OpenStruct) diff --git a/ext/json/lib/json/add/rails.rb b/ext/json/lib/json/add/rails.rb deleted file mode 100644 index e86ed1aab9..0000000000 --- a/ext/json/lib/json/add/rails.rb +++ /dev/null @@ -1,58 +0,0 @@ -# This file contains implementations of rails custom objects for -# serialisation/deserialisation. - -unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and - ::JSON::JSON_LOADED - require 'json' -end - -class Object - def self.json_create(object) - obj = new - for key, value in object - next if key == 'json_class' - instance_variable_set "@#{key}", value - end - obj - end - - def to_json(*a) - result = { - 'json_class' => self.class.name - } - instance_variables.inject(result) do |r, name| - r[name[1..-1]] = instance_variable_get name - r - end - result.to_json(*a) - end -end - -class Symbol - def to_json(*a) - to_s.to_json(*a) - end -end - -module Enumerable - def to_json(*a) - to_a.to_json(*a) - end -end - -# class Regexp -# def to_json(*) -# inspect -# end -# end -# -# The above rails definition has some problems: -# -# 1. { 'foo' => /bar/ }.to_json # => "{foo: /bar/}" -# This isn't valid JSON, because the regular expression syntax is not -# defined in RFC 4627. (And unquoted strings are disallowed there, too.) -# Though it is valid Javascript. -# -# 2. { 'foo' => /bar/mix }.to_json # => "{foo: /bar/mix}" -# This isn't even valid Javascript. - diff --git a/ext/json/lib/json/add/range.rb b/ext/json/lib/json/add/range.rb new file mode 100644 index 0000000000..408d2c32f6 --- /dev/null +++ b/ext/json/lib/json/add/range.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end + +class Range + + # See #as_json. + def self.json_create(object) + new(*object['a']) + end + + # Methods <tt>Range#as_json</tt> and +Range.json_create+ may be used + # to serialize and deserialize a \Range object; + # see Marshal[rdoc-ref:Marshal]. + # + # \Method <tt>Range#as_json</tt> serializes +self+, + # returning a 2-element hash representing +self+: + # + # require 'json/add/range' + # x = (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]} + # y = (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]} + # z = ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]} + # + # \Method +JSON.create+ deserializes such a hash, returning a \Range object: + # + # Range.json_create(x) # => 1..4 + # Range.json_create(y) # => 1...4 + # Range.json_create(z) # => "a".."d" + # + def as_json(*) + { + JSON.create_id => self.class.name, + 'a' => [ first, last, exclude_end? ] + } + end + + # Returns a JSON string representing +self+: + # + # require 'json/add/range' + # puts (1..4).to_json + # puts (1...4).to_json + # puts ('a'..'d').to_json + # + # Output: + # + # {"json_class":"Range","a":[1,4,false]} + # {"json_class":"Range","a":[1,4,true]} + # {"json_class":"Range","a":["a","d",false]} + # + def to_json(*args) + as_json.to_json(*args) + end +end diff --git a/ext/json/lib/json/add/rational.rb b/ext/json/lib/json/add/rational.rb new file mode 100644 index 0000000000..c95812ea8e --- /dev/null +++ b/ext/json/lib/json/add/rational.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end + +class Rational + + # See #as_json. + def self.json_create(object) + Rational(object['n'], object['d']) + end + + # Methods <tt>Rational#as_json</tt> and +Rational.json_create+ may be used + # to serialize and deserialize a \Rational object; + # see Marshal[rdoc-ref:Marshal]. + # + # \Method <tt>Rational#as_json</tt> serializes +self+, + # returning a 2-element hash representing +self+: + # + # require 'json/add/rational' + # x = Rational(2, 3).as_json + # # => {"json_class"=>"Rational", "n"=>2, "d"=>3} + # + # \Method +JSON.create+ deserializes such a hash, returning a \Rational object: + # + # Rational.json_create(x) + # # => (2/3) + # + def as_json(*) + { + JSON.create_id => self.class.name, + 'n' => numerator, + 'd' => denominator, + } + end + + # Returns a JSON string representing +self+: + # + # require 'json/add/rational' + # puts Rational(2, 3).to_json + # + # Output: + # + # {"json_class":"Rational","n":2,"d":3} + # + def to_json(*args) + as_json.to_json(*args) + end +end diff --git a/ext/json/lib/json/add/regexp.rb b/ext/json/lib/json/add/regexp.rb new file mode 100644 index 0000000000..aebfb2db5c --- /dev/null +++ b/ext/json/lib/json/add/regexp.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end + +class Regexp + + # See #as_json. + def self.json_create(object) + new(object['s'], object['o']) + end + + # Methods <tt>Regexp#as_json</tt> and +Regexp.json_create+ may be used + # to serialize and deserialize a \Regexp object; + # see Marshal[rdoc-ref:Marshal]. + # + # \Method <tt>Regexp#as_json</tt> serializes +self+, + # returning a 2-element hash representing +self+: + # + # require 'json/add/regexp' + # x = /foo/.as_json + # # => {"json_class"=>"Regexp", "o"=>0, "s"=>"foo"} + # + # \Method +JSON.create+ deserializes such a hash, returning a \Regexp object: + # + # Regexp.json_create(x) # => /foo/ + # + def as_json(*) + { + JSON.create_id => self.class.name, + 'o' => options, + 's' => source, + } + end + + # Returns a JSON string representing +self+: + # + # require 'json/add/regexp' + # puts /foo/.to_json + # + # Output: + # + # {"json_class":"Regexp","o":0,"s":"foo"} + # + def to_json(*args) + as_json.to_json(*args) + end +end diff --git a/ext/json/lib/json/add/set.rb b/ext/json/lib/json/add/set.rb new file mode 100644 index 0000000000..1918353187 --- /dev/null +++ b/ext/json/lib/json/add/set.rb @@ -0,0 +1,48 @@ +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end +defined?(::Set) or require 'set' + +class Set + + # See #as_json. + def self.json_create(object) + new object['a'] + end + + # Methods <tt>Set#as_json</tt> and +Set.json_create+ may be used + # to serialize and deserialize a \Set object; + # see Marshal[rdoc-ref:Marshal]. + # + # \Method <tt>Set#as_json</tt> serializes +self+, + # returning a 2-element hash representing +self+: + # + # require 'json/add/set' + # x = Set.new(%w/foo bar baz/).as_json + # # => {"json_class"=>"Set", "a"=>["foo", "bar", "baz"]} + # + # \Method +JSON.create+ deserializes such a hash, returning a \Set object: + # + # Set.json_create(x) # => #<Set: {"foo", "bar", "baz"}> + # + def as_json(*) + { + JSON.create_id => self.class.name, + 'a' => to_a, + } + end + + # Returns a JSON string representing +self+: + # + # require 'json/add/set' + # puts Set.new(%w/foo bar baz/).to_json + # + # Output: + # + # {"json_class":"Set","a":["foo","bar","baz"]} + # + def to_json(*args) + as_json.to_json(*args) + end +end + diff --git a/ext/json/lib/json/add/string.rb b/ext/json/lib/json/add/string.rb new file mode 100644 index 0000000000..9c3bde27fb --- /dev/null +++ b/ext/json/lib/json/add/string.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end + +class String + # call-seq: json_create(o) + # + # Raw Strings are JSON Objects (the raw bytes are stored in an array for the + # key "raw"). The Ruby String can be created by this class method. + def self.json_create(object) + object["raw"].pack("C*") + end + + # call-seq: to_json_raw_object() + # + # This method creates a raw object hash, that can be nested into + # other data structures and will be generated as a raw string. This + # method should be used, if you want to convert raw strings to JSON + # instead of UTF-8 strings, e. g. binary data. + def to_json_raw_object + { + JSON.create_id => self.class.name, + "raw" => unpack("C*"), + } + end + + # call-seq: to_json_raw(*args) + # + # This method creates a JSON text from the result of a call to + # to_json_raw_object of this String. + def to_json_raw(...) + to_json_raw_object.to_json(...) + end +end diff --git a/ext/json/lib/json/add/struct.rb b/ext/json/lib/json/add/struct.rb new file mode 100644 index 0000000000..6760c3d86c --- /dev/null +++ b/ext/json/lib/json/add/struct.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end + +class Struct + + # See #as_json. + def self.json_create(object) + new(*object['v']) + end + + # Methods <tt>Struct#as_json</tt> and +Struct.json_create+ may be used + # to serialize and deserialize a \Struct object; + # see Marshal[rdoc-ref:Marshal]. + # + # \Method <tt>Struct#as_json</tt> serializes +self+, + # returning a 2-element hash representing +self+: + # + # require 'json/add/struct' + # Customer = Struct.new('Customer', :name, :address, :zip) + # x = Struct::Customer.new.as_json + # # => {"json_class"=>"Struct::Customer", "v"=>[nil, nil, nil]} + # + # \Method +JSON.create+ deserializes such a hash, returning a \Struct object: + # + # Struct::Customer.json_create(x) + # # => #<struct Struct::Customer name=nil, address=nil, zip=nil> + # + def as_json(*) + klass = self.class.name + klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!" + { + JSON.create_id => klass, + 'v' => values, + } + end + + # Returns a JSON string representing +self+: + # + # require 'json/add/struct' + # Customer = Struct.new('Customer', :name, :address, :zip) + # puts Struct::Customer.new.to_json + # + # Output: + # + # {"json_class":"Struct","t":{'name':'Rowdy',"age":null}} + # + def to_json(*args) + as_json.to_json(*args) + end +end diff --git a/ext/json/lib/json/add/symbol.rb b/ext/json/lib/json/add/symbol.rb new file mode 100644 index 0000000000..806be4f025 --- /dev/null +++ b/ext/json/lib/json/add/symbol.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end + +class Symbol + + # Methods <tt>Symbol#as_json</tt> and +Symbol.json_create+ may be used + # to serialize and deserialize a \Symbol object; + # see Marshal[rdoc-ref:Marshal]. + # + # \Method <tt>Symbol#as_json</tt> serializes +self+, + # returning a 2-element hash representing +self+: + # + # require 'json/add/symbol' + # x = :foo.as_json + # # => {"json_class"=>"Symbol", "s"=>"foo"} + # + # \Method +JSON.create+ deserializes such a hash, returning a \Symbol object: + # + # Symbol.json_create(x) # => :foo + # + def as_json(*) + { + JSON.create_id => self.class.name, + 's' => to_s, + } + end + + # Returns a JSON string representing +self+: + # + # require 'json/add/symbol' + # puts :foo.to_json + # + # Output: + # + # # {"json_class":"Symbol","s":"foo"} + # + def to_json(state = nil, *a) + state = ::JSON::State.from_state(state) + if state.strict? + super + else + as_json.to_json(state, *a) + end + end + + # See #as_json. + def self.json_create(o) + o['s'].to_sym + end +end diff --git a/ext/json/lib/json/add/time.rb b/ext/json/lib/json/add/time.rb new file mode 100644 index 0000000000..b03d4ff251 --- /dev/null +++ b/ext/json/lib/json/add/time.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end + +class Time + + # See #as_json. + def self.json_create(object) + if usec = object.delete('u') # used to be tv_usec -> tv_nsec + object['n'] = usec * 1000 + end + at(object['s'], Rational(object['n'], 1000)) + end + + # Methods <tt>Time#as_json</tt> and +Time.json_create+ may be used + # to serialize and deserialize a \Time object; + # see Marshal[rdoc-ref:Marshal]. + # + # \Method <tt>Time#as_json</tt> serializes +self+, + # returning a 2-element hash representing +self+: + # + # require 'json/add/time' + # x = Time.now.as_json + # # => {"json_class"=>"Time", "s"=>1700931656, "n"=>472846644} + # + # \Method +JSON.create+ deserializes such a hash, returning a \Time object: + # + # Time.json_create(x) + # # => 2023-11-25 11:00:56.472846644 -0600 + # + def as_json(*) + { + JSON.create_id => self.class.name, + 's' => tv_sec, + 'n' => tv_nsec, + } + end + + # Returns a JSON string representing +self+: + # + # require 'json/add/time' + # puts Time.now.to_json + # + # Output: + # + # {"json_class":"Time","s":1700931678,"n":980650786} + # + def to_json(*args) + as_json.to_json(*args) + end +end diff --git a/ext/json/lib/json/common.rb b/ext/json/lib/json/common.rb index 499fcc0dae..230bf08012 100644 --- a/ext/json/lib/json/common.rb +++ b/ext/json/lib/json/common.rb @@ -1,354 +1,1173 @@ +# frozen_string_literal: true + require 'json/version' module JSON + autoload :GenericObject, 'json/generic_object' + + module ParserOptions # :nodoc: + class << self + def prepare(opts) + if opts[:object_class] || opts[:array_class] + opts = opts.dup + on_load = opts[:on_load] + + on_load = object_class_proc(opts[:object_class], on_load) if opts[:object_class] + on_load = array_class_proc(opts[:array_class], on_load) if opts[:array_class] + opts[:on_load] = on_load + end + + if opts.fetch(:create_additions, false) != false + opts = create_additions_proc(opts) + end + + opts + end + + private + + def object_class_proc(object_class, on_load) + ->(obj) do + if Hash === obj + object = object_class.new + obj.each { |k, v| object[k] = v } + obj = object + end + on_load.nil? ? obj : on_load.call(obj) + end + end + + def array_class_proc(array_class, on_load) + ->(obj) do + if Array === obj + array = array_class.new + obj.each { |v| array << v } + obj = array + end + on_load.nil? ? obj : on_load.call(obj) + end + end + + # TODO: extract :create_additions support to another gem for version 3.0 + def create_additions_proc(opts) + if opts[:symbolize_names] + raise ArgumentError, "options :symbolize_names and :create_additions cannot be used in conjunction" + end + + opts = opts.dup + create_additions = opts.fetch(:create_additions, false) + on_load = opts[:on_load] + object_class = opts[:object_class] || Hash + + opts[:on_load] = ->(object) do + case object + when String + opts[:match_string]&.each do |pattern, klass| + if match = pattern.match(object) + create_additions_warning if create_additions.nil? + object = klass.json_create(object) + break + end + end + when object_class + if opts[:create_additions] != false + if class_path = object[JSON.create_id] + klass = begin + Object.const_get(class_path) + rescue NameError => e + raise ArgumentError, "can't get const #{class_path}: #{e}" + end + + if klass.respond_to?(:json_creatable?) ? klass.json_creatable? : klass.respond_to?(:json_create) + create_additions_warning if create_additions.nil? + object = klass.json_create(object) + end + end + end + end + + on_load.nil? ? object : on_load.call(object) + end + + opts + end + + def create_additions_warning + JSON.deprecation_warning "JSON.load implicit support for `create_additions: true` is deprecated " \ + "and will be removed in 3.0, use JSON.unsafe_load or explicitly " \ + "pass `create_additions: true`" + end + end + end + class << self - # If _object_ is string-like parse the string and return the parsed result - # as a Ruby data structure. Otherwise generate a JSON text from the Ruby - # data structure object and return it. - # - # The _opts_ argument is passed through to generate/parse respectively, see - # generate and parse for their documentation. - def [](object, opts = {}) - if object.respond_to? :to_str - JSON.parse(object.to_str, opts => {}) + def deprecation_warning(message, uplevel = 3) # :nodoc: + gem_root = File.expand_path("..", __dir__) + "/" + caller_locations(uplevel, 10).each do |frame| + if frame.path.nil? || frame.path.start_with?(gem_root) || frame.path.end_with?("/truffle/cext_ruby.rb", ".c") + uplevel += 1 + else + break + end + end + + if RUBY_VERSION >= "3.0" + warn(message, uplevel: uplevel, category: :deprecated) else - JSON.generate(object, opts => {}) + warn(message, uplevel: uplevel) end end - # Returns the JSON parser class, that is used by JSON. This might be either - # JSON::Ext::Parser or JSON::Pure::Parser. + # :call-seq: + # JSON[object] -> new_array or new_string + # + # If +object+ is a \String, + # calls JSON.parse with +object+ and +opts+ (see method #parse): + # json = '[0, 1, null]' + # JSON[json]# => [0, 1, nil] + # + # Otherwise, calls JSON.generate with +object+ and +opts+ (see method #generate): + # ruby = [0, 1, nil] + # JSON[ruby] # => '[0,1,null]' + def [](object, opts = nil) + if object.is_a?(String) + return JSON.parse(object, opts) + elsif object.respond_to?(:to_str) + str = object.to_str + if str.is_a?(String) + return JSON.parse(str, opts) + end + end + + JSON.generate(object, opts) + end + + # Returns the JSON parser class that is used by JSON. attr_reader :parser # Set the JSON parser class _parser_ to be used by JSON. def parser=(parser) # :nodoc: @parser = parser - remove_const :Parser if const_defined? :Parser + remove_const :Parser if const_defined?(:Parser, false) const_set :Parser, parser end - # Return the constant located at _path_. The format of _path_ has to be - # either ::A::B::C or A::B::C. In any case A has to be located at the top - # level (absolute namespace path?). If there doesn't exist a constant at - # the given path, an ArgumentError is raised. - def deep_const_get(path) # :nodoc: - path = path.to_s - path.split(/::/).inject(Object) do |p, c| - case - when c.empty? then p - when p.const_defined?(c) then p.const_get(c) - else raise ArgumentError, "can't find const #{path}" - end - end - end - # Set the module _generator_ to be used by JSON. def generator=(generator) # :nodoc: + old, $VERBOSE = $VERBOSE, nil @generator = generator - generator_methods = generator::GeneratorMethods - for const in generator_methods.constants - klass = deep_const_get(const) - modul = generator_methods.const_get(const) - klass.class_eval do - instance_methods(false).each do |m| - m.to_s == 'to_json' and remove_method m + if generator.const_defined?(:GeneratorMethods) + generator_methods = generator::GeneratorMethods + for const in generator_methods.constants + klass = const_get(const) + modul = generator_methods.const_get(const) + klass.class_eval do + instance_methods(false).each do |m| + m.to_s == 'to_json' and remove_method m + end + include modul end - include modul end end self.state = generator::State - const_set :State, self.state + const_set :State, state + ensure + $VERBOSE = old end - # Returns the JSON generator modul, that is used by JSON. This might be - # either JSON::Ext::Generator or JSON::Pure::Generator. + # Returns the JSON generator module that is used by JSON. attr_reader :generator - # Returns the JSON generator state class, that is used by JSON. This might - # be either JSON::Ext::Generator::State or JSON::Pure::Generator::State. + # Sets or Returns the JSON generator state class that is used by JSON. attr_accessor :state - # This is create identifier, that is used to decide, if the _json_create_ - # hook of a class should be called. It defaults to 'json_class'. - attr_accessor :create_id + private + + # Called from the extension when a hash has both string and symbol keys + def on_mixed_keys_hash(hash, do_raise) + set = {} + hash.each_key do |key| + key_str = key.to_s + + if set[key_str] + message = "detected duplicate key #{key_str.inspect} in #{hash.inspect}" + if do_raise + raise GeneratorError, message + else + deprecation_warning("#{message}.\nThis will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`") + end + else + set[key_str] = true + end + end + end + + def deprecated_singleton_attr_accessor(*attrs) + args = RUBY_VERSION >= "3.0" ? ", category: :deprecated" : "" + attrs.each do |attr| + singleton_class.class_eval <<~RUBY + def #{attr} + warn "JSON.#{attr} is deprecated and will be removed in json 3.0.0", uplevel: 1 #{args} + @#{attr} + end + + def #{attr}=(val) + warn "JSON.#{attr}= is deprecated and will be removed in json 3.0.0", uplevel: 1 #{args} + @#{attr} = val + end + + def _#{attr} + @#{attr} + end + RUBY + end + end + end + + # Sets create identifier, which is used to decide if the _json_create_ + # hook of a class should be called; initial value is +json_class+: + # JSON.create_id # => 'json_class' + def self.create_id=(new_value) + Thread.current[:"JSON.create_id"] = new_value.dup.freeze end - self.create_id = 'json_class' - NaN = (-1.0) ** 0.5 + # Returns the current create identifier. + # See also JSON.create_id=. + def self.create_id + Thread.current[:"JSON.create_id"] || 'json_class' + end + + NaN = Float::NAN - Infinity = 1.0/0 + Infinity = Float::INFINITY MinusInfinity = -Infinity # The base exception for JSON errors. class JSONError < StandardError; end - # This exception is raised, if a parser error occurs. - class ParserError < JSONError; end + # This exception is raised if a parser error occurs. + class ParserError < JSONError + attr_reader :line, :column + end - # This exception is raised, if the nesting of parsed datastructures is too + # This exception is raised if the nesting of parsed data structures is too # deep. class NestingError < ParserError; end - # This exception is raised, if a generator or unparser error occurs. - class GeneratorError < JSONError; end - # For backwards compatibility - UnparserError = GeneratorError + # This exception is raised if a generator or unparser error occurs. + class GeneratorError < JSONError + attr_reader :invalid_object - # If a circular data structure is encountered while unparsing - # this exception is raised. - class CircularDatastructure < GeneratorError; end + def initialize(message, invalid_object = nil) + super(message) + @invalid_object = invalid_object + end - # This exception is raised, if the required unicode support is missing on the - # system. Usually this means, that the iconv library is not installed. - class MissingUnicodeSupport < JSONError; end + def detailed_message(...) + # Exception#detailed_message doesn't exist until Ruby 3.2 + super_message = defined?(super) ? super : message - module_function + if @invalid_object.nil? + super_message + else + "#{super_message}\nInvalid object: #{@invalid_object.inspect}" + end + end + end + + # Fragment of JSON document that is to be included as is: + # fragment = JSON::Fragment.new("[1, 2, 3]") + # JSON.generate({ count: 3, items: fragments }) + # + # This allows to easily assemble multiple JSON fragments that have + # been persisted somewhere without having to parse them nor resorting + # to string interpolation. + # + # Note: no validation is performed on the provided string. It is the + # responsibility of the caller to ensure the string contains valid JSON. + Fragment = Struct.new(:json) do + def initialize(json) + unless string = String.try_convert(json) + raise TypeError, " no implicit conversion of #{json.class} into String" + end - # Parse the JSON string _source_ into a Ruby data structure and return it. - # - # _opts_ can have the following - # keys: - # * *max_nesting*: The maximum depth of nesting allowed in the parsed data - # structures. Disable depth checking with :max_nesting => false, it defaults - # to 19. - # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in - # defiance of RFC 4627 to be parsed by the Parser. This option defaults - # to false. - # * *create_additions*: If set to false, the Parser doesn't create - # additions even if a matchin class and create_id was found. This option - # defaults to true. - def parse(source, opts = {}) - JSON.parser.new(source, opts).parse + super(string) + end + + def to_json(state = nil, *) + json + end end - # Parse the JSON string _source_ into a Ruby data structure and return it. - # The bang version of the parse method, defaults to the more dangerous values - # for the _opts_ hash, so be sure only to parse trusted _source_ strings. - # - # _opts_ can have the following keys: - # * *max_nesting*: The maximum depth of nesting allowed in the parsed data - # structures. Enable depth checking with :max_nesting => anInteger. The parse! - # methods defaults to not doing max depth checking: This can be dangerous, - # if someone wants to fill up your stack. - # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in - # defiance of RFC 4627 to be parsed by the Parser. This option defaults - # to true. - # * *create_additions*: If set to false, the Parser doesn't create - # additions even if a matchin class and create_id was found. This option - # defaults to true. - def parse!(source, opts = {}) - opts = { - :max_nesting => false, - :allow_nan => true - }.update(opts) - JSON.parser.new(source, opts).parse + module_function + + # :call-seq: + # JSON.parse(source, opts) -> object + # + # Returns the Ruby objects created by parsing the given +source+. + # + # Argument +source+ contains the \String to be parsed. + # + # Argument +opts+, if given, contains a \Hash of options for the parsing. + # See {Parsing Options}[#module-JSON-label-Parsing+Options]. + # + # --- + # + # When +source+ is a \JSON array, returns a Ruby \Array: + # source = '["foo", 1.0, true, false, null]' + # ruby = JSON.parse(source) + # ruby # => ["foo", 1.0, true, false, nil] + # ruby.class # => Array + # + # When +source+ is a \JSON object, returns a Ruby \Hash: + # source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}' + # ruby = JSON.parse(source) + # ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil} + # ruby.class # => Hash + # + # For examples of parsing for all \JSON data types, see + # {Parsing \JSON}[#module-JSON-label-Parsing+JSON]. + # + # Parses nested JSON objects: + # source = <<~JSON + # { + # "name": "Dave", + # "age" :40, + # "hats": [ + # "Cattleman's", + # "Panama", + # "Tophat" + # ] + # } + # JSON + # ruby = JSON.parse(source) + # ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]} + # + # --- + # + # Raises an exception if +source+ is not valid JSON: + # # Raises JSON::ParserError (783: unexpected token at ''): + # JSON.parse('') + # + def parse(source, opts = nil) + opts = ParserOptions.prepare(opts) unless opts.nil? + Parser.parse(source, opts) end - # Unparse the Ruby data structure _obj_ into a single line JSON string and - # return it. _state_ is - # * a JSON::State object, - # * or a Hash like object (responding to to_hash), - # * an object convertible into a hash by a to_h method, - # that is used as or to configure a State object. - # - # It defaults to a state object, that creates the shortest possible JSON text - # in one line, checks for circular data structures and doesn't allow NaN, - # Infinity, and -Infinity. - # - # A _state_ hash can have the following keys: - # * *indent*: a string used to indent levels (default: ''), - # * *space*: a string that is put after, a : or , delimiter (default: ''), - # * *space_before*: a string that is put before a : pair delimiter (default: ''), - # * *object_nl*: a string that is put at the end of a JSON object (default: ''), - # * *array_nl*: a string that is put at the end of a JSON array (default: ''), - # * *check_circular*: true if checking for circular data structures - # should be done (the default), false otherwise. - # * *allow_nan*: true if NaN, Infinity, and -Infinity should be - # generated, otherwise an exception is thrown, if these values are - # encountered. This options defaults to false. - # * *max_nesting*: The maximum depth of nesting allowed in the data - # structures from which JSON is to be generated. Disable depth checking - # with :max_nesting => false, it defaults to 19. - # - # See also the fast_generate for the fastest creation method with the least - # amount of sanity checks, and the pretty_generate method for some - # defaults for a pretty output. - def generate(obj, state = nil) - if state - state = State.from_state(state) + PARSE_L_OPTIONS = { + max_nesting: false, + allow_nan: true, + }.freeze + private_constant :PARSE_L_OPTIONS + + # :call-seq: + # JSON.parse!(source, opts) -> object + # + # Calls + # parse(source, opts) + # with +source+ and possibly modified +opts+. + # + # Differences from JSON.parse: + # - Option +max_nesting+, if not provided, defaults to +false+, + # which disables checking for nesting depth. + # - Option +allow_nan+, if not provided, defaults to +true+. + def parse!(source, opts = nil) + if opts.nil? + parse(source, PARSE_L_OPTIONS) else - state = State.new + parse(source, PARSE_L_OPTIONS.merge(opts)) end - obj.to_json(state) end - # :stopdoc: - # I want to deprecate these later, so I'll first be silent about them, and - # later delete them. - alias unparse generate - module_function :unparse - # :startdoc: + # :call-seq: + # JSON.load_file(path, opts={}) -> object + # + # Calls: + # parse(File.read(path), opts) + # + # See method #parse. + def load_file(filespec, opts = nil) + parse(File.read(filespec, encoding: Encoding::UTF_8), opts) + end - # Unparse the Ruby data structure _obj_ into a single line JSON string and - # return it. This method disables the checks for circles in Ruby objects, and - # also generates NaN, Infinity, and, -Infinity float values. + # :call-seq: + # JSON.load_file!(path, opts = {}) # - # *WARNING*: Be careful not to pass any Ruby data structures with circles as - # _obj_ argument, because this will cause JSON to go into an infinite loop. - def fast_generate(obj) - obj.to_json(nil) + # Calls: + # JSON.parse!(File.read(path, opts)) + # + # See method #parse! + def load_file!(filespec, opts = nil) + parse!(File.read(filespec, encoding: Encoding::UTF_8), opts) end - # :stopdoc: - # I want to deprecate these later, so I'll first be silent about them, and later delete them. - alias fast_unparse fast_generate - module_function :fast_unparse - # :startdoc: + # :call-seq: + # JSON.generate(obj, opts = nil) -> new_string + # + # Returns a \String containing the generated \JSON data. + # + # See also JSON.pretty_generate. + # + # Argument +obj+ is the Ruby object to be converted to \JSON. + # + # Argument +opts+, if given, contains a \Hash of options for the generation. + # See {Generating Options}[#module-JSON-label-Generating+Options]. + # + # --- + # + # When +obj+ is an \Array, returns a \String containing a \JSON array: + # obj = ["foo", 1.0, true, false, nil] + # json = JSON.generate(obj) + # json # => '["foo",1.0,true,false,null]' + # + # When +obj+ is a \Hash, returns a \String containing a \JSON object: + # obj = {foo: 0, bar: 's', baz: :bat} + # json = JSON.generate(obj) + # json # => '{"foo":0,"bar":"s","baz":"bat"}' + # + # For examples of generating from other Ruby objects, see + # {Generating \JSON from Other Objects}[#module-JSON-label-Generating+JSON+from+Other+Objects]. + # + # --- + # + # Raises an exception if any formatting option is not a \String. + # + # Raises an exception if +obj+ contains circular references: + # a = []; b = []; a.push(b); b.push(a) + # # Raises JSON::NestingError (nesting of 100 is too deep): + # JSON.generate(a) + # + def generate(obj, opts = nil) + if State === opts + opts.generate(obj) + else + State.generate(obj, opts, nil) + end + end + + # :call-seq: + # JSON.fast_generate(obj, opts) -> new_string + # + # Arguments +obj+ and +opts+ here are the same as + # arguments +obj+ and +opts+ in JSON.generate. + # + # By default, generates \JSON data without checking + # for circular references in +obj+ (option +max_nesting+ set to +false+, disabled). + # + # Raises an exception if +obj+ contains circular references: + # a = []; b = []; a.push(b); b.push(a) + # # Raises SystemStackError (stack level too deep): + # JSON.fast_generate(a) + def fast_generate(obj, opts = nil) + if RUBY_VERSION >= "3.0" + warn "JSON.fast_generate is deprecated and will be removed in json 3.0.0, just use JSON.generate", uplevel: 1, category: :deprecated + else + warn "JSON.fast_generate is deprecated and will be removed in json 3.0.0, just use JSON.generate", uplevel: 1 + end + generate(obj, opts) + end + + PRETTY_GENERATE_OPTIONS = { + indent: ' ', + space: ' ', + object_nl: "\n", + array_nl: "\n", + }.freeze + private_constant :PRETTY_GENERATE_OPTIONS - # Unparse the Ruby data structure _obj_ into a JSON string and return it. The - # returned string is a prettier form of the string returned by #unparse. + # :call-seq: + # JSON.pretty_generate(obj, opts = nil) -> new_string + # + # Arguments +obj+ and +opts+ here are the same as + # arguments +obj+ and +opts+ in JSON.generate. + # + # Default options are: + # { + # indent: ' ', # Two spaces + # space: ' ', # One space + # array_nl: "\n", # Newline + # object_nl: "\n" # Newline + # } + # + # Example: + # obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}} + # json = JSON.pretty_generate(obj) + # puts json + # Output: + # { + # "foo": [ + # "bar", + # "baz" + # ], + # "bat": { + # "bam": 0, + # "bad": 1 + # } + # } # - # The _opts_ argument can be used to configure the generator, see the - # generate method for a more detailed explanation. def pretty_generate(obj, opts = nil) - state = JSON.state.new( - :indent => ' ', - :space => ' ', - :object_nl => "\n", - :array_nl => "\n", - :check_circular => true - ) + return opts.generate(obj) if State === opts + + options = PRETTY_GENERATE_OPTIONS + if opts - if opts.respond_to? :to_hash - opts = opts.to_hash - elsif opts.respond_to? :to_h - opts = opts.to_h + unless opts.is_a?(Hash) + if opts.respond_to? :to_hash + opts = opts.to_hash + elsif opts.respond_to? :to_h + opts = opts.to_h + else + raise TypeError, "can't convert #{opts.class} into Hash" + end + end + options = options.merge(opts) + end + + State.generate(obj, options, nil) + end + + # Sets or returns default options for the JSON.unsafe_load method. + # Initially: + # opts = JSON.load_default_options + # opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true} + deprecated_singleton_attr_accessor :unsafe_load_default_options + + @unsafe_load_default_options = { + :max_nesting => false, + :allow_nan => true, + :allow_blank => true, + :create_additions => true, + } + + # Sets or returns default options for the JSON.load method. + # Initially: + # opts = JSON.load_default_options + # opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true} + deprecated_singleton_attr_accessor :load_default_options + + @load_default_options = { + :allow_nan => true, + :allow_blank => true, + :create_additions => nil, + } + # :call-seq: + # JSON.unsafe_load(source, options = {}) -> object + # JSON.unsafe_load(source, proc = nil, options = {}) -> object + # + # Returns the Ruby objects created by parsing the given +source+. + # + # BEWARE: This method is meant to serialise data from trusted user input, + # like from your own database server or clients under your control, it could + # be dangerous to allow untrusted users to pass JSON sources into it. + # + # - Argument +source+ must be, or be convertible to, a \String: + # - If +source+ responds to instance method +to_str+, + # <tt>source.to_str</tt> becomes the source. + # - If +source+ responds to instance method +to_io+, + # <tt>source.to_io.read</tt> becomes the source. + # - If +source+ responds to instance method +read+, + # <tt>source.read</tt> becomes the source. + # - If both of the following are true, source becomes the \String <tt>'null'</tt>: + # - Option +allow_blank+ specifies a truthy value. + # - The source, as defined above, is +nil+ or the empty \String <tt>''</tt>. + # - Otherwise, +source+ remains the source. + # - Argument +proc+, if given, must be a \Proc that accepts one argument. + # It will be called recursively with each result (depth-first order). + # See details below. + # - Argument +opts+, if given, contains a \Hash of options for the parsing. + # See {Parsing Options}[#module-JSON-label-Parsing+Options]. + # The default options can be changed via method JSON.unsafe_load_default_options=. + # + # --- + # + # When no +proc+ is given, modifies +source+ as above and returns the result of + # <tt>parse(source, opts)</tt>; see #parse. + # + # Source for following examples: + # source = <<~JSON + # { + # "name": "Dave", + # "age" :40, + # "hats": [ + # "Cattleman's", + # "Panama", + # "Tophat" + # ] + # } + # JSON + # + # Load a \String: + # ruby = JSON.unsafe_load(source) + # ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]} + # + # Load an \IO object: + # require 'stringio' + # object = JSON.unsafe_load(StringIO.new(source)) + # object # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]} + # + # Load a \File object: + # path = 't.json' + # File.write(path, source) + # File.open(path) do |file| + # JSON.unsafe_load(file) + # end # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]} + # + # --- + # + # When +proc+ is given: + # - Modifies +source+ as above. + # - Gets the +result+ from calling <tt>parse(source, opts)</tt>. + # - Recursively calls <tt>proc(result)</tt>. + # - Returns the final result. + # + # Example: + # require 'json' + # + # # Some classes for the example. + # class Base + # def initialize(attributes) + # @attributes = attributes + # end + # end + # class User < Base; end + # class Account < Base; end + # class Admin < Base; end + # # The JSON source. + # json = <<-EOF + # { + # "users": [ + # {"type": "User", "username": "jane", "email": "jane@example.com"}, + # {"type": "User", "username": "john", "email": "john@example.com"} + # ], + # "accounts": [ + # {"account": {"type": "Account", "paid": true, "account_id": "1234"}}, + # {"account": {"type": "Account", "paid": false, "account_id": "1235"}} + # ], + # "admins": {"type": "Admin", "password": "0wn3d"} + # } + # EOF + # # Deserializer method. + # def deserialize_obj(obj, safe_types = %w(User Account Admin)) + # type = obj.is_a?(Hash) && obj["type"] + # safe_types.include?(type) ? Object.const_get(type).new(obj) : obj + # end + # # Call to JSON.unsafe_load + # ruby = JSON.unsafe_load(json, proc {|obj| + # case obj + # when Hash + # obj.each {|k, v| obj[k] = deserialize_obj v } + # when Array + # obj.map! {|v| deserialize_obj v } + # end + # obj + # }) + # pp ruby + # Output: + # {"users"=> + # [#<User:0x00000000064c4c98 + # @attributes= + # {"type"=>"User", "username"=>"jane", "email"=>"jane@example.com"}>, + # #<User:0x00000000064c4bd0 + # @attributes= + # {"type"=>"User", "username"=>"john", "email"=>"john@example.com"}>], + # "accounts"=> + # [{"account"=> + # #<Account:0x00000000064c4928 + # @attributes={"type"=>"Account", "paid"=>true, "account_id"=>"1234"}>}, + # {"account"=> + # #<Account:0x00000000064c4680 + # @attributes={"type"=>"Account", "paid"=>false, "account_id"=>"1235"}>}], + # "admins"=> + # #<Admin:0x00000000064c41f8 + # @attributes={"type"=>"Admin", "password"=>"0wn3d"}>} + # + def unsafe_load(source, proc = nil, options = nil) + opts = if options.nil? + if proc && proc.is_a?(Hash) + options, proc = proc, nil + options + else + _unsafe_load_default_options + end + else + _unsafe_load_default_options.merge(options) + end + + unless source.is_a?(String) + if source.respond_to? :to_str + source = source.to_str + elsif source.respond_to? :to_io + source = source.to_io.read + elsif source.respond_to?(:read) + source = source.read + end + end + + if opts[:allow_blank] && (source.nil? || source.empty?) + source = 'null' + end + + if proc + opts = opts.dup + opts[:on_load] = proc.to_proc + end + + parse(source, opts) + end + + # :call-seq: + # JSON.load(source, options = {}) -> object + # JSON.load(source, proc = nil, options = {}) -> object + # + # Returns the Ruby objects created by parsing the given +source+. + # + # BEWARE: This method is meant to serialise data from trusted user input, + # like from your own database server or clients under your control, it could + # be dangerous to allow untrusted users to pass JSON sources into it. + # If you must use it, use JSON.unsafe_load instead to make it clear. + # + # Since JSON version 2.8.0, `load` emits a deprecation warning when a + # non native type is deserialized, without `create_additions` being explicitly + # enabled, and in JSON version 3.0, `load` will have `create_additions` disabled + # by default. + # + # - Argument +source+ must be, or be convertible to, a \String: + # - If +source+ responds to instance method +to_str+, + # <tt>source.to_str</tt> becomes the source. + # - If +source+ responds to instance method +to_io+, + # <tt>source.to_io.read</tt> becomes the source. + # - If +source+ responds to instance method +read+, + # <tt>source.read</tt> becomes the source. + # - If both of the following are true, source becomes the \String <tt>'null'</tt>: + # - Option +allow_blank+ specifies a truthy value. + # - The source, as defined above, is +nil+ or the empty \String <tt>''</tt>. + # - Otherwise, +source+ remains the source. + # - Argument +proc+, if given, must be a \Proc that accepts one argument. + # It will be called recursively with each result (depth-first order). + # See details below. + # - Argument +opts+, if given, contains a \Hash of options for the parsing. + # See {Parsing Options}[#module-JSON-label-Parsing+Options]. + # The default options can be changed via method JSON.load_default_options=. + # + # --- + # + # When no +proc+ is given, modifies +source+ as above and returns the result of + # <tt>parse(source, opts)</tt>; see #parse. + # + # Source for following examples: + # source = <<~JSON + # { + # "name": "Dave", + # "age" :40, + # "hats": [ + # "Cattleman's", + # "Panama", + # "Tophat" + # ] + # } + # JSON + # + # Load a \String: + # ruby = JSON.load(source) + # ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]} + # + # Load an \IO object: + # require 'stringio' + # object = JSON.load(StringIO.new(source)) + # object # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]} + # + # Load a \File object: + # path = 't.json' + # File.write(path, source) + # File.open(path) do |file| + # JSON.load(file) + # end # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]} + # + # --- + # + # When +proc+ is given: + # - Modifies +source+ as above. + # - Gets the +result+ from calling <tt>parse(source, opts)</tt>. + # - Recursively calls <tt>proc(result)</tt>. + # - Returns the final result. + # + # Example: + # require 'json' + # + # # Some classes for the example. + # class Base + # def initialize(attributes) + # @attributes = attributes + # end + # end + # class User < Base; end + # class Account < Base; end + # class Admin < Base; end + # # The JSON source. + # json = <<-EOF + # { + # "users": [ + # {"type": "User", "username": "jane", "email": "jane@example.com"}, + # {"type": "User", "username": "john", "email": "john@example.com"} + # ], + # "accounts": [ + # {"account": {"type": "Account", "paid": true, "account_id": "1234"}}, + # {"account": {"type": "Account", "paid": false, "account_id": "1235"}} + # ], + # "admins": {"type": "Admin", "password": "0wn3d"} + # } + # EOF + # # Deserializer method. + # def deserialize_obj(obj, safe_types = %w(User Account Admin)) + # type = obj.is_a?(Hash) && obj["type"] + # safe_types.include?(type) ? Object.const_get(type).new(obj) : obj + # end + # # Call to JSON.load + # ruby = JSON.load(json, proc {|obj| + # case obj + # when Hash + # obj.each {|k, v| obj[k] = deserialize_obj v } + # when Array + # obj.map! {|v| deserialize_obj v } + # end + # obj + # }) + # pp ruby + # Output: + # {"users"=> + # [#<User:0x00000000064c4c98 + # @attributes= + # {"type"=>"User", "username"=>"jane", "email"=>"jane@example.com"}>, + # #<User:0x00000000064c4bd0 + # @attributes= + # {"type"=>"User", "username"=>"john", "email"=>"john@example.com"}>], + # "accounts"=> + # [{"account"=> + # #<Account:0x00000000064c4928 + # @attributes={"type"=>"Account", "paid"=>true, "account_id"=>"1234"}>}, + # {"account"=> + # #<Account:0x00000000064c4680 + # @attributes={"type"=>"Account", "paid"=>false, "account_id"=>"1235"}>}], + # "admins"=> + # #<Admin:0x00000000064c41f8 + # @attributes={"type"=>"Admin", "password"=>"0wn3d"}>} + # + def load(source, proc = nil, options = nil) + if proc && options.nil? && proc.is_a?(Hash) + options = proc + proc = nil + end + + opts = if options.nil? + if proc && proc.is_a?(Hash) + options, proc = proc, nil + options else - raise TypeError, "can't convert #{opts.class} into Hash" + _load_default_options end - state.configure(opts) + else + _load_default_options.merge(options) + end + + unless source.is_a?(String) + if source.respond_to? :to_str + source = source.to_str + elsif source.respond_to? :to_io + source = source.to_io.read + elsif source.respond_to?(:read) + source = source.read + end + end + + if opts[:allow_blank] && (source.nil? || (String === source && source.empty?)) + source = 'null' + end + + if proc + opts = opts.dup + opts[:on_load] = proc.to_proc + end + + parse(source, opts) + end + + # Sets or returns the default options for the JSON.dump method. + # Initially: + # opts = JSON.dump_default_options + # opts # => {:max_nesting=>false, :allow_nan=>true} + deprecated_singleton_attr_accessor :dump_default_options + @dump_default_options = { + :max_nesting => false, + :allow_nan => true, + } + + # :call-seq: + # JSON.dump(obj, io = nil, limit = nil) + # + # Dumps +obj+ as a \JSON string, i.e. calls generate on the object and returns the result. + # + # The default options can be changed via method JSON.dump_default_options. + # + # - Argument +io+, if given, should respond to method +write+; + # the \JSON \String is written to +io+, and +io+ is returned. + # If +io+ is not given, the \JSON \String is returned. + # - Argument +limit+, if given, is passed to JSON.generate as option +max_nesting+. + # + # --- + # + # When argument +io+ is not given, returns the \JSON \String generated from +obj+: + # obj = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad} + # json = JSON.dump(obj) + # json # => "{\"foo\":[0,1],\"bar\":{\"baz\":2,\"bat\":3},\"bam\":\"bad\"}" + # + # When argument +io+ is given, writes the \JSON \String to +io+ and returns +io+: + # path = 't.json' + # File.open(path, 'w') do |file| + # JSON.dump(obj, file) + # end # => #<File:t.json (closed)> + # puts File.read(path) + # Output: + # {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"} + def dump(obj, anIO = nil, limit = nil, kwargs = nil) + if kwargs.nil? + if limit.nil? + if anIO.is_a?(Hash) + kwargs = anIO + anIO = nil + end + elsif limit.is_a?(Hash) + kwargs = limit + limit = nil + end + end + + unless anIO.nil? + if anIO.respond_to?(:to_io) + anIO = anIO.to_io + elsif limit.nil? && !anIO.respond_to?(:write) + anIO, limit = nil, anIO + end + end + + opts = JSON._dump_default_options + opts = opts.merge(:max_nesting => limit) if limit + opts = opts.merge(kwargs) if kwargs + + begin + State.generate(obj, opts, anIO) + rescue JSON::NestingError + raise ArgumentError, "exceed depth limit" end - obj.to_json(state) end # :stopdoc: - # I want to deprecate these later, so I'll first be silent about them, and later delete them. - alias pretty_unparse pretty_generate - module_function :pretty_unparse - # :startdoc: + # All these were meant to be deprecated circa 2009, but were just set as undocumented + # so usage still exist in the wild. + def unparse(...) + if RUBY_VERSION >= "3.0" + warn "JSON.unparse is deprecated and will be removed in json 3.0.0, just use JSON.generate", uplevel: 1, category: :deprecated + else + warn "JSON.unparse is deprecated and will be removed in json 3.0.0, just use JSON.generate", uplevel: 1 + end + generate(...) + end + module_function :unparse - # Load a ruby data structure from a JSON _source_ and return it. A source can - # either be a string-like object, an IO like object, or an object responding - # to the read method. If _proc_ was given, it will be called with any nested - # Ruby object as an argument recursively in depth first order. - # - # This method is part of the implementation of the load/dump interface of - # Marshal and YAML. - def load(source, proc = nil) - if source.respond_to? :to_str - source = source.to_str - elsif source.respond_to? :to_io - source = source.to_io.read + def fast_unparse(...) + if RUBY_VERSION >= "3.0" + warn "JSON.fast_unparse is deprecated and will be removed in json 3.0.0, just use JSON.generate", uplevel: 1, category: :deprecated else - source = source.read + warn "JSON.fast_unparse is deprecated and will be removed in json 3.0.0, just use JSON.generate", uplevel: 1 end - result = parse(source, :max_nesting => false, :allow_nan => true) - recurse_proc(result, &proc) if proc - result + generate(...) end + module_function :fast_unparse - def recurse_proc(result, &proc) - case result - when Array - result.each { |x| recurse_proc x, &proc } - proc.call result - when Hash - result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc } - proc.call result + def pretty_unparse(...) + if RUBY_VERSION >= "3.0" + warn "JSON.pretty_unparse is deprecated and will be removed in json 3.0.0, just use JSON.pretty_generate", uplevel: 1, category: :deprecated else - proc.call result + warn "JSON.pretty_unparse is deprecated and will be removed in json 3.0.0, just use JSON.pretty_generate", uplevel: 1 end + pretty_generate(...) end - private :recurse_proc - module_function :recurse_proc + module_function :fast_unparse - alias restore load + def restore(...) + if RUBY_VERSION >= "3.0" + warn "JSON.restore is deprecated and will be removed in json 3.0.0, just use JSON.load", uplevel: 1, category: :deprecated + else + warn "JSON.restore is deprecated and will be removed in json 3.0.0, just use JSON.load", uplevel: 1 + end + load(...) + end module_function :restore - # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns - # the result. + class << self + private + + def const_missing(const_name) + case const_name + when :PRETTY_STATE_PROTOTYPE + if RUBY_VERSION >= "3.0" + warn "JSON::PRETTY_STATE_PROTOTYPE is deprecated and will be removed in json 3.0.0, just use JSON.pretty_generate", uplevel: 1, category: :deprecated + else + warn "JSON::PRETTY_STATE_PROTOTYPE is deprecated and will be removed in json 3.0.0, just use JSON.pretty_generate", uplevel: 1 + end + state.new(PRETTY_GENERATE_OPTIONS) + else + super + end + end + end + # :startdoc: + + # JSON::Coder holds a parser and generator configuration. # - # If anIO (an IO like object or an object that responds to the write method) - # was given, the resulting JSON is written to it. + # module MyApp + # JSONC_CODER = JSON::Coder.new( + # allow_trailing_comma: true + # ) + # end # - # If the number of nested arrays or objects exceeds _limit_ an ArgumentError - # exception is raised. This argument is similar (but not exactly the - # same!) to the _limit_ argument in Marshal.dump. + # MyApp::JSONC_CODER.load(document) # - # This method is part of the implementation of the load/dump interface of - # Marshal and YAML. - def dump(obj, anIO = nil, limit = nil) - if anIO and limit.nil? - anIO = anIO.to_io if anIO.respond_to?(:to_io) - unless anIO.respond_to?(:write) - limit = anIO - anIO = nil + class Coder + # :call-seq: + # JSON.new(options = nil, &block) + # + # Argument +options+, if given, contains a \Hash of options for both parsing and generating. + # See {Parsing Options}[rdoc-ref:JSON@Parsing+Options], + # and {Generating Options}[rdoc-ref:JSON@Generating+Options]. + # + # For generation, the <tt>strict: true</tt> 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" + # + 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)).freeze end - limit ||= 0 - result = generate(obj, :allow_nan => true, :max_nesting => limit) - if anIO - anIO.write result - anIO - else - result + + # call-seq: + # dump(object) -> String + # dump(object, io) -> io + # + # Serialize the given object into a \JSON document. + def dump(object, io = nil) + @state.generate(object, io) + end + alias_method :generate, :dump + + # call-seq: + # load(string) -> Object + # + # Parse the given \JSON document and return an equivalent Ruby object. + def load(source) + @parser_config.parse(source) + end + alias_method :parse, :load + + # call-seq: + # load(path) -> Object + # + # Parse the given \JSON document and return an equivalent Ruby object. + def load_file(path) + load(File.read(path, encoding: Encoding::UTF_8)) + end + end + + module GeneratorMethods + # call-seq: to_json(*) + # + # Converts this object into a JSON string. + # If this object doesn't directly maps to a JSON native type, + # first convert it to a string (calling #to_s), then converts + # it to a JSON string, and returns the result. + # This is a fallback, if no special method #to_json was defined for some object. + def to_json(state = nil, *) + obj = case self + when nil, false, true, Integer, Float, Array, Hash + self + else + "#{self}" + end + + if state.nil? + JSON::State._generate_no_fallback(obj, nil, nil) + else + JSON::State.from_state(state)._generate_no_fallback(obj) + end end - rescue JSON::NestingError - raise ArgumentError, "exceed depth limit" end end module ::Kernel + private + # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in # one line. def j(*objs) + if RUBY_VERSION >= "3.0" + warn "Kernel#j is deprecated and will be removed in json 3.0.0", uplevel: 1, category: :deprecated + else + warn "Kernel#j is deprecated and will be removed in json 3.0.0", uplevel: 1 + end + objs.each do |obj| - puts JSON::generate(obj, :allow_nan => true, :max_nesting => false) + puts JSON.generate(obj, :allow_nan => true, :max_nesting => false) end nil end - # Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with + # Outputs _objs_ to STDOUT as JSON strings in a pretty format, with # indentation and over many lines. def jj(*objs) + if RUBY_VERSION >= "3.0" + warn "Kernel#jj is deprecated and will be removed in json 3.0.0", uplevel: 1, category: :deprecated + else + warn "Kernel#jj is deprecated and will be removed in json 3.0.0", uplevel: 1 + end + objs.each do |obj| - puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false) + puts JSON.pretty_generate(obj, :allow_nan => true, :max_nesting => false) end nil end - # If _object_ is string-like parse the string and return the parsed result as - # a Ruby data structure. Otherwise generate a JSON text from the Ruby data + # If _object_ is string-like, parse the string and return the parsed result as + # a Ruby data structure. Otherwise, generate a JSON text from the Ruby data # structure object and return it. # - # The _opts_ argument is passed through to generate/parse respectively, see + # The _opts_ argument is passed through to generate/parse respectively. See # generate and parse for their documentation. - def JSON(object, opts = {}) - if object.respond_to? :to_str - JSON.parse(object.to_str, opts) - else - JSON.generate(object, opts) - end + def JSON(object, opts = nil) + JSON[object, opts] end end -class ::Class - # Returns true, if this class can be used to create an instance - # from a serialised JSON string. The class has to implement a class - # method _json_create_ that expects a hash as first parameter, which includes - # the required data. - def json_creatable? - respond_to?(:json_create) - end +class Object + include JSON::GeneratorMethods end diff --git a/ext/json/lib/json/editor.rb b/ext/json/lib/json/editor.rb deleted file mode 100644 index 12a7f94591..0000000000 --- a/ext/json/lib/json/editor.rb +++ /dev/null @@ -1,1362 +0,0 @@ -# To use the GUI JSON editor, start the edit_json.rb executable script. It -# requires ruby-gtk to be installed. - -require 'gtk2' -require 'iconv' -require 'json' -require 'rbconfig' -require 'open-uri' - -module JSON - module Editor - include Gtk - - # Beginning of the editor window title - TITLE = 'JSON Editor'.freeze - - # Columns constants - ICON_COL, TYPE_COL, CONTENT_COL = 0, 1, 2 - - # JSON primitive types (Containers) - CONTAINER_TYPES = %w[Array Hash].sort - # All JSON primitive types - ALL_TYPES = (%w[TrueClass FalseClass Numeric String NilClass] + - CONTAINER_TYPES).sort - - # The Nodes necessary for the tree representation of a JSON document - ALL_NODES = (ALL_TYPES + %w[Key]).sort - - DEFAULT_DIALOG_KEY_PRESS_HANDLER = lambda do |dialog, event| - case event.keyval - when Gdk::Keyval::GDK_Return - dialog.response Dialog::RESPONSE_ACCEPT - when Gdk::Keyval::GDK_Escape - dialog.response Dialog::RESPONSE_REJECT - end - end - - # Returns the Gdk::Pixbuf of the icon named _name_ from the icon cache. - def Editor.fetch_icon(name) - @icon_cache ||= {} - unless @icon_cache.key?(name) - path = File.dirname(__FILE__) - @icon_cache[name] = Gdk::Pixbuf.new(File.join(path, name + '.xpm')) - end - @icon_cache[name] - end - - # Opens an error dialog on top of _window_ showing the error message - # _text_. - def Editor.error_dialog(window, text) - dialog = MessageDialog.new(window, Dialog::MODAL, - MessageDialog::ERROR, - MessageDialog::BUTTONS_CLOSE, text) - dialog.show_all - dialog.run - rescue TypeError - dialog = MessageDialog.new(Editor.window, Dialog::MODAL, - MessageDialog::ERROR, - MessageDialog::BUTTONS_CLOSE, text) - dialog.show_all - dialog.run - ensure - dialog.destroy if dialog - end - - # Opens a yes/no question dialog on top of _window_ showing the error - # message _text_. If yes was answered _true_ is returned, otherwise - # _false_. - def Editor.question_dialog(window, text) - dialog = MessageDialog.new(window, Dialog::MODAL, - MessageDialog::QUESTION, - MessageDialog::BUTTONS_YES_NO, text) - dialog.show_all - dialog.run do |response| - return Gtk::Dialog::RESPONSE_YES === response - end - ensure - dialog.destroy if dialog - end - - # Convert the tree model starting from Gtk::TreeIter _iter_ into a Ruby - # data structure and return it. - def Editor.model2data(iter) - return nil if iter.nil? - case iter.type - when 'Hash' - hash = {} - iter.each { |c| hash[c.content] = Editor.model2data(c.first_child) } - hash - when 'Array' - array = Array.new(iter.n_children) - iter.each_with_index { |c, i| array[i] = Editor.model2data(c) } - array - when 'Key' - iter.content - when 'String' - iter.content - when 'Numeric' - content = iter.content - if /\./.match(content) - content.to_f - else - content.to_i - end - when 'TrueClass' - true - when 'FalseClass' - false - when 'NilClass' - nil - else - fail "Unknown type found in model: #{iter.type}" - end - end - - # Convert the Ruby data structure _data_ into tree model data for Gtk and - # returns the whole model. If the parameter _model_ wasn't given a new - # Gtk::TreeStore is created as the model. The _parent_ parameter specifies - # the parent node (iter, Gtk:TreeIter instance) to which the data is - # appended, alternativeley the result of the yielded block is used as iter. - def Editor.data2model(data, model = nil, parent = nil) - model ||= TreeStore.new(Gdk::Pixbuf, String, String) - iter = if block_given? - yield model - else - model.append(parent) - end - case data - when Hash - iter.type = 'Hash' - data.sort.each do |key, value| - pair_iter = model.append(iter) - pair_iter.type = 'Key' - pair_iter.content = key.to_s - Editor.data2model(value, model, pair_iter) - end - when Array - iter.type = 'Array' - data.each do |value| - Editor.data2model(value, model, iter) - end - when Numeric - iter.type = 'Numeric' - iter.content = data.to_s - when String, true, false, nil - iter.type = data.class.name - iter.content = data.nil? ? 'null' : data.to_s - else - iter.type = 'String' - iter.content = data.to_s - end - model - end - - # The Gtk::TreeIter class is reopened and some auxiliary methods are added. - class Gtk::TreeIter - include Enumerable - - # Traverse each of this Gtk::TreeIter instance's children - # and yield to them. - def each - n_children.times { |i| yield nth_child(i) } - end - - # Recursively traverse all nodes of this Gtk::TreeIter's subtree - # (including self) and yield to them. - def recursive_each(&block) - yield self - each do |i| - i.recursive_each(&block) - end - end - - # Remove the subtree of this Gtk::TreeIter instance from the - # model _model_. - def remove_subtree(model) - while current = first_child - model.remove(current) - end - end - - # Returns the type of this node. - def type - self[TYPE_COL] - end - - # Sets the type of this node to _value_. This implies setting - # the respective icon accordingly. - def type=(value) - self[TYPE_COL] = value - self[ICON_COL] = Editor.fetch_icon(value) - end - - # Returns the content of this node. - def content - self[CONTENT_COL] - end - - # Sets the content of this node to _value_. - def content=(value) - self[CONTENT_COL] = value - end - end - - # This module bundles some method, that can be used to create a menu. It - # should be included into the class in question. - module MenuExtension - include Gtk - - # Creates a Menu, that includes MenuExtension. _treeview_ is the - # Gtk::TreeView, on which it operates. - def initialize(treeview) - @treeview = treeview - @menu = Menu.new - end - - # Returns the Gtk::TreeView of this menu. - attr_reader :treeview - - # Returns the menu. - attr_reader :menu - - # Adds a Gtk::SeparatorMenuItem to this instance's #menu. - def add_separator - menu.append SeparatorMenuItem.new - end - - # Adds a Gtk::MenuItem to this instance's #menu. _label_ is the label - # string, _klass_ is the item type, and _callback_ is the procedure, that - # is called if the _item_ is activated. - def add_item(label, keyval = nil, klass = MenuItem, &callback) - label = "#{label} (C-#{keyval.chr})" if keyval - item = klass.new(label) - item.signal_connect(:activate, &callback) - if keyval - self.signal_connect(:'key-press-event') do |item, event| - if event.state & Gdk::Window::ModifierType::CONTROL_MASK != 0 and - event.keyval == keyval - callback.call item - end - end - end - menu.append item - item - end - - # This method should be implemented in subclasses to create the #menu of - # this instance. It has to be called after an instance of this class is - # created, to build the menu. - def create - raise NotImplementedError - end - - def method_missing(*a, &b) - treeview.__send__(*a, &b) - end - end - - # This class creates the popup menu, that opens when clicking onto the - # treeview. - class PopUpMenu - include MenuExtension - - # Change the type or content of the selected node. - def change_node(item) - if current = selection.selected - parent = current.parent - old_type, old_content = current.type, current.content - if ALL_TYPES.include?(old_type) - @clipboard_data = Editor.model2data(current) - type, content = ask_for_element(parent, current.type, - current.content) - if type - current.type, current.content = type, content - current.remove_subtree(model) - toplevel.display_status("Changed a node in tree.") - window.change - end - else - toplevel.display_status( - "Cannot change node of type #{old_type} in tree!") - end - end - end - - # Cut the selected node and its subtree, and save it into the - # clipboard. - def cut_node(item) - if current = selection.selected - if current and current.type == 'Key' - @clipboard_data = { - current.content => Editor.model2data(current.first_child) - } - else - @clipboard_data = Editor.model2data(current) - end - model.remove(current) - window.change - toplevel.display_status("Cut a node from tree.") - end - end - - # Copy the selected node and its subtree, and save it into the - # clipboard. - def copy_node(item) - if current = selection.selected - if current and current.type == 'Key' - @clipboard_data = { - current.content => Editor.model2data(current.first_child) - } - else - @clipboard_data = Editor.model2data(current) - end - window.change - toplevel.display_status("Copied a node from tree.") - end - end - - # Paste the data in the clipboard into the selected Array or Hash by - # appending it. - def paste_node_appending(item) - if current = selection.selected - if @clipboard_data - case current.type - when 'Array' - Editor.data2model(@clipboard_data, model, current) - expand_collapse(current) - when 'Hash' - if @clipboard_data.is_a? Hash - parent = current.parent - hash = Editor.model2data(current) - model.remove(current) - hash.update(@clipboard_data) - Editor.data2model(hash, model, parent) - if parent - expand_collapse(parent) - elsif @expanded - expand_all - end - window.change - else - toplevel.display_status( - "Cannot paste non-#{current.type} data into '#{current.type}'!") - end - else - toplevel.display_status( - "Cannot paste node below '#{current.type}'!") - end - else - toplevel.display_status("Nothing to paste in clipboard!") - end - else - toplevel.display_status("Append a node into the root first!") - end - end - - # Paste the data in the clipboard into the selected Array inserting it - # before the selected element. - def paste_node_inserting_before(item) - if current = selection.selected - if @clipboard_data - parent = current.parent or return - parent_type = parent.type - if parent_type == 'Array' - selected_index = parent.each_with_index do |c, i| - break i if c == current - end - Editor.data2model(@clipboard_data, model, parent) do |m| - m.insert_before(parent, current) - end - expand_collapse(current) - toplevel.display_status("Inserted an element to " + - "'#{parent_type}' before index #{selected_index}.") - window.change - else - toplevel.display_status( - "Cannot insert node below '#{parent_type}'!") - end - else - toplevel.display_status("Nothing to paste in clipboard!") - end - else - toplevel.display_status("Append a node into the root first!") - end - end - - # Append a new node to the selected Hash or Array. - def append_new_node(item) - if parent = selection.selected - parent_type = parent.type - case parent_type - when 'Hash' - key, type, content = ask_for_hash_pair(parent) - key or return - iter = create_node(parent, 'Key', key) - iter = create_node(iter, type, content) - toplevel.display_status( - "Added a (key, value)-pair to '#{parent_type}'.") - window.change - when 'Array' - type, content = ask_for_element(parent) - type or return - iter = create_node(parent, type, content) - window.change - toplevel.display_status("Appendend an element to '#{parent_type}'.") - else - toplevel.display_status("Cannot append to '#{parent_type}'!") - end - else - type, content = ask_for_element - type or return - iter = create_node(nil, type, content) - window.change - end - end - - # Insert a new node into an Array before the selected element. - def insert_new_node(item) - if current = selection.selected - parent = current.parent or return - parent_parent = parent.parent - parent_type = parent.type - if parent_type == 'Array' - selected_index = parent.each_with_index do |c, i| - break i if c == current - end - type, content = ask_for_element(parent) - type or return - iter = model.insert_before(parent, current) - iter.type, iter.content = type, content - toplevel.display_status("Inserted an element to " + - "'#{parent_type}' before index #{selected_index}.") - window.change - else - toplevel.display_status( - "Cannot insert node below '#{parent_type}'!") - end - else - toplevel.display_status("Append a node into the root first!") - end - end - - # Recursively collapse/expand a subtree starting from the selected node. - def collapse_expand(item) - if current = selection.selected - if row_expanded?(current.path) - collapse_row(current.path) - else - expand_row(current.path, true) - end - else - toplevel.display_status("Append a node into the root first!") - end - end - - # Create the menu. - def create - add_item("Change node", ?n, &method(:change_node)) - add_separator - add_item("Cut node", ?X, &method(:cut_node)) - add_item("Copy node", ?C, &method(:copy_node)) - add_item("Paste node (appending)", ?A, &method(:paste_node_appending)) - add_item("Paste node (inserting before)", ?I, - &method(:paste_node_inserting_before)) - add_separator - add_item("Append new node", ?a, &method(:append_new_node)) - add_item("Insert new node before", ?i, &method(:insert_new_node)) - add_separator - add_item("Collapse/Expand node (recursively)", ?e, - &method(:collapse_expand)) - - menu.show_all - signal_connect(:button_press_event) do |widget, event| - if event.kind_of? Gdk::EventButton and event.button == 3 - menu.popup(nil, nil, event.button, event.time) - end - end - signal_connect(:popup_menu) do - menu.popup(nil, nil, 0, Gdk::Event::CURRENT_TIME) - end - end - end - - # This class creates the File pulldown menu. - class FileMenu - include MenuExtension - - # Clear the model and filename, but ask to save the JSON document, if - # unsaved changes have occured. - def new(item) - window.clear - end - - # Open a file and load it into the editor. Ask to save the JSON document - # first, if unsaved changes have occured. - def open(item) - window.file_open - end - - def open_location(item) - window.location_open - end - - # Revert the current JSON document in the editor to the saved version. - def revert(item) - window.instance_eval do - @filename and file_open(@filename) - end - end - - # Save the current JSON document. - def save(item) - window.file_save - end - - # Save the current JSON document under the given filename. - def save_as(item) - window.file_save_as - end - - # Quit the editor, after asking to save any unsaved changes first. - def quit(item) - window.quit - end - - # Create the menu. - def create - title = MenuItem.new('File') - title.submenu = menu - add_item('New', &method(:new)) - add_item('Open', ?o, &method(:open)) - add_item('Open location', ?l, &method(:open_location)) - add_item('Revert', &method(:revert)) - add_separator - add_item('Save', ?s, &method(:save)) - add_item('Save As', ?S, &method(:save_as)) - add_separator - add_item('Quit', ?q, &method(:quit)) - title - end - end - - # This class creates the Edit pulldown menu. - class EditMenu - include MenuExtension - - # Copy data from model into primary clipboard. - def copy(item) - data = Editor.model2data(model.iter_first) - json = JSON.pretty_generate(data, :max_nesting => false) - c = Gtk::Clipboard.get(Gdk::Selection::PRIMARY) - c.text = json - end - - # Copy json text from primary clipboard into model. - def paste(item) - c = Gtk::Clipboard.get(Gdk::Selection::PRIMARY) - if json = c.wait_for_text - window.ask_save if @changed - begin - window.edit json - rescue JSON::ParserError - window.clear - end - end - end - - # Find a string in all nodes' contents and select the found node in the - # treeview. - def find(item) - @search = ask_for_find_term(@search) or return - iter = model.get_iter('0') or return - iter.recursive_each do |i| - if @iter - if @iter != i - next - else - @iter = nil - next - end - elsif @search.match(i[CONTENT_COL]) - set_cursor(i.path, nil, false) - @iter = i - break - end - end - end - - # Repeat the last search given by #find. - def find_again(item) - @search or return - iter = model.get_iter('0') - iter.recursive_each do |i| - if @iter - if @iter != i - next - else - @iter = nil - next - end - elsif @search.match(i[CONTENT_COL]) - set_cursor(i.path, nil, false) - @iter = i - break - end - end - end - - # Sort (Reverse sort) all elements of the selected array by the given - # expression. _x_ is the element in question. - def sort(item) - if current = selection.selected - if current.type == 'Array' - parent = current.parent - ary = Editor.model2data(current) - order, reverse = ask_for_order - order or return - begin - block = eval "lambda { |x| #{order} }" - if reverse - ary.sort! { |a,b| block[b] <=> block[a] } - else - ary.sort! { |a,b| block[a] <=> block[b] } - end - rescue => e - Editor.error_dialog(self, "Failed to sort Array with #{order}: #{e}!") - else - Editor.data2model(ary, model, parent) do |m| - m.insert_before(parent, current) - end - model.remove(current) - expand_collapse(parent) - window.change - toplevel.display_status("Array has been sorted.") - end - else - toplevel.display_status("Only Array nodes can be sorted!") - end - else - toplevel.display_status("Select an Array to sort first!") - end - end - - # Create the menu. - def create - title = MenuItem.new('Edit') - title.submenu = menu - add_item('Copy', ?c, &method(:copy)) - add_item('Paste', ?v, &method(:paste)) - add_separator - add_item('Find', ?f, &method(:find)) - add_item('Find Again', ?g, &method(:find_again)) - add_separator - add_item('Sort', ?S, &method(:sort)) - title - end - end - - class OptionsMenu - include MenuExtension - - # Collapse/Expand all nodes by default. - def collapsed_nodes(item) - if expanded - self.expanded = false - collapse_all - else - self.expanded = true - expand_all - end - end - - # Toggle pretty saving mode on/off. - def pretty_saving(item) - @pretty_item.toggled - window.change - end - - attr_reader :pretty_item - - # Create the menu. - def create - title = MenuItem.new('Options') - title.submenu = menu - add_item('Collapsed nodes', nil, CheckMenuItem, &method(:collapsed_nodes)) - @pretty_item = add_item('Pretty saving', nil, CheckMenuItem, - &method(:pretty_saving)) - @pretty_item.active = true - window.unchange - title - end - end - - # This class inherits from Gtk::TreeView, to configure it and to add a lot - # of behaviour to it. - class JSONTreeView < Gtk::TreeView - include Gtk - - # Creates a JSONTreeView instance, the parameter _window_ is - # a MainWindow instance and used for self delegation. - def initialize(window) - @window = window - super(TreeStore.new(Gdk::Pixbuf, String, String)) - self.selection.mode = SELECTION_BROWSE - - @expanded = false - self.headers_visible = false - add_columns - add_popup_menu - end - - # Returns the MainWindow instance of this JSONTreeView. - attr_reader :window - - # Returns true, if nodes are autoexpanding, false otherwise. - attr_accessor :expanded - - private - - def add_columns - cell = CellRendererPixbuf.new - column = TreeViewColumn.new('Icon', cell, - 'pixbuf' => ICON_COL - ) - append_column(column) - - cell = CellRendererText.new - column = TreeViewColumn.new('Type', cell, - 'text' => TYPE_COL - ) - append_column(column) - - cell = CellRendererText.new - cell.editable = true - column = TreeViewColumn.new('Content', cell, - 'text' => CONTENT_COL - ) - cell.signal_connect(:edited, &method(:cell_edited)) - append_column(column) - end - - def unify_key(iter, key) - return unless iter.type == 'Key' - parent = iter.parent - if parent.any? { |c| c != iter and c.content == key } - old_key = key - i = 0 - begin - key = sprintf("%s.%d", old_key, i += 1) - end while parent.any? { |c| c != iter and c.content == key } - end - iter.content = key - end - - def cell_edited(cell, path, value) - iter = model.get_iter(path) - case iter.type - when 'Key' - unify_key(iter, value) - toplevel.display_status('Key has been changed.') - when 'FalseClass' - value.downcase! - if value == 'true' - iter.type, iter.content = 'TrueClass', 'true' - end - when 'TrueClass' - value.downcase! - if value == 'false' - iter.type, iter.content = 'FalseClass', 'false' - end - when 'Numeric' - iter.content = (Integer(value) rescue Float(value) rescue 0).to_s - when 'String' - iter.content = value - when 'Hash', 'Array' - return - else - fail "Unknown type found in model: #{iter.type}" - end - window.change - end - - def configure_value(value, type) - value.editable = false - case type - when 'Array', 'Hash' - value.text = '' - when 'TrueClass' - value.text = 'true' - when 'FalseClass' - value.text = 'false' - when 'NilClass' - value.text = 'null' - when 'Numeric', 'String' - value.text ||= '' - value.editable = true - else - raise ArgumentError, "unknown type '#{type}' encountered" - end - end - - def add_popup_menu - menu = PopUpMenu.new(self) - menu.create - end - - public - - # Create a _type_ node with content _content_, and add it to _parent_ - # in the model. If _parent_ is nil, create a new model and put it into - # the editor treeview. - def create_node(parent, type, content) - iter = if parent - model.append(parent) - else - new_model = Editor.data2model(nil) - toplevel.view_new_model(new_model) - new_model.iter_first - end - iter.type, iter.content = type, content - expand_collapse(parent) if parent - iter - end - - # Ask for a hash key, value pair to be added to the Hash node _parent_. - def ask_for_hash_pair(parent) - key_input = type_input = value_input = nil - - dialog = Dialog.new("New (key, value) pair for Hash", nil, nil, - [ Stock::OK, Dialog::RESPONSE_ACCEPT ], - [ Stock::CANCEL, Dialog::RESPONSE_REJECT ] - ) - dialog.width_request = 640 - - hbox = HBox.new(false, 5) - hbox.pack_start(Label.new("Key:"), false) - hbox.pack_start(key_input = Entry.new) - key_input.text = @key || '' - dialog.vbox.pack_start(hbox, false) - key_input.signal_connect(:activate) do - if parent.any? { |c| c.content == key_input.text } - toplevel.display_status('Key already exists in Hash!') - key_input.text = '' - else - toplevel.display_status('Key has been changed.') - end - end - - hbox = HBox.new(false, 5) - hbox.pack_start(Label.new("Type:"), false) - hbox.pack_start(type_input = ComboBox.new(true)) - ALL_TYPES.each { |t| type_input.append_text(t) } - type_input.active = @type || 0 - dialog.vbox.pack_start(hbox, false) - - type_input.signal_connect(:changed) do - value_input.editable = false - case ALL_TYPES[type_input.active] - when 'Array', 'Hash' - value_input.text = '' - when 'TrueClass' - value_input.text = 'true' - when 'FalseClass' - value_input.text = 'false' - when 'NilClass' - value_input.text = 'null' - else - value_input.text = '' - value_input.editable = true - end - end - - hbox = HBox.new(false, 5) - hbox.pack_start(Label.new("Value:"), false) - hbox.pack_start(value_input = Entry.new) - value_input.width_chars = 60 - value_input.text = @value || '' - dialog.vbox.pack_start(hbox, false) - - dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) - dialog.show_all - self.focus = dialog - dialog.run do |response| - if response == Dialog::RESPONSE_ACCEPT - @key = key_input.text - type = ALL_TYPES[@type = type_input.active] - content = value_input.text - return @key, type, content - end - end - return - ensure - dialog.destroy - end - - # Ask for an element to be appended _parent_. - def ask_for_element(parent = nil, default_type = nil, value_text = @content) - type_input = value_input = nil - - dialog = Dialog.new( - "New element into #{parent ? parent.type : 'root'}", - nil, nil, - [ Stock::OK, Dialog::RESPONSE_ACCEPT ], - [ Stock::CANCEL, Dialog::RESPONSE_REJECT ] - ) - hbox = HBox.new(false, 5) - hbox.pack_start(Label.new("Type:"), false) - hbox.pack_start(type_input = ComboBox.new(true)) - default_active = 0 - types = parent ? ALL_TYPES : CONTAINER_TYPES - types.each_with_index do |t, i| - type_input.append_text(t) - if t == default_type - default_active = i - end - end - type_input.active = default_active - dialog.vbox.pack_start(hbox, false) - type_input.signal_connect(:changed) do - configure_value(value_input, types[type_input.active]) - end - - hbox = HBox.new(false, 5) - hbox.pack_start(Label.new("Value:"), false) - hbox.pack_start(value_input = Entry.new) - value_input.width_chars = 60 - value_input.text = value_text if value_text - configure_value(value_input, types[type_input.active]) - - dialog.vbox.pack_start(hbox, false) - - dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) - dialog.show_all - self.focus = dialog - dialog.run do |response| - if response == Dialog::RESPONSE_ACCEPT - type = types[type_input.active] - @content = case type - when 'Numeric' - Integer(value_input.text) rescue Float(value_input.text) rescue 0 - else - value_input.text - end.to_s - return type, @content - end - end - return - ensure - dialog.destroy if dialog - end - - # Ask for an order criteria for sorting, using _x_ for the element in - # question. Returns the order criterium, and true/false for reverse - # sorting. - def ask_for_order - dialog = Dialog.new( - "Give an order criterium for 'x'.", - nil, nil, - [ Stock::OK, Dialog::RESPONSE_ACCEPT ], - [ Stock::CANCEL, Dialog::RESPONSE_REJECT ] - ) - hbox = HBox.new(false, 5) - - hbox.pack_start(Label.new("Order:"), false) - hbox.pack_start(order_input = Entry.new) - order_input.text = @order || 'x' - order_input.width_chars = 60 - - hbox.pack_start(reverse_checkbox = CheckButton.new('Reverse'), false) - - dialog.vbox.pack_start(hbox, false) - - dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) - dialog.show_all - self.focus = dialog - dialog.run do |response| - if response == Dialog::RESPONSE_ACCEPT - return @order = order_input.text, reverse_checkbox.active? - end - end - return - ensure - dialog.destroy if dialog - end - - # Ask for a find term to search for in the tree. Returns the term as a - # string. - def ask_for_find_term(search = nil) - dialog = Dialog.new( - "Find a node matching regex in tree.", - nil, nil, - [ Stock::OK, Dialog::RESPONSE_ACCEPT ], - [ Stock::CANCEL, Dialog::RESPONSE_REJECT ] - ) - hbox = HBox.new(false, 5) - - hbox.pack_start(Label.new("Regex:"), false) - hbox.pack_start(regex_input = Entry.new) - hbox.pack_start(icase_checkbox = CheckButton.new('Icase'), false) - regex_input.width_chars = 60 - if search - regex_input.text = search.source - icase_checkbox.active = search.casefold? - end - - dialog.vbox.pack_start(hbox, false) - - dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) - dialog.show_all - self.focus = dialog - dialog.run do |response| - if response == Dialog::RESPONSE_ACCEPT - begin - return Regexp.new(regex_input.text, icase_checkbox.active? ? Regexp::IGNORECASE : 0) - rescue => e - Editor.error_dialog(self, "Evaluation of regex /#{regex_input.text}/ failed: #{e}!") - return - end - end - end - return - ensure - dialog.destroy if dialog - end - - # Expand or collapse row pointed to by _iter_ according - # to the #expanded attribute. - def expand_collapse(iter) - if expanded - expand_row(iter.path, true) - else - collapse_row(iter.path) - end - end - end - - # The editor main window - class MainWindow < Gtk::Window - include Gtk - - def initialize(encoding) - @changed = false - @encoding = encoding - super(TOPLEVEL) - display_title - set_default_size(800, 600) - signal_connect(:delete_event) { quit } - - vbox = VBox.new(false, 0) - add(vbox) - #vbox.border_width = 0 - - @treeview = JSONTreeView.new(self) - @treeview.signal_connect(:'cursor-changed') do - display_status('') - end - - menu_bar = create_menu_bar - vbox.pack_start(menu_bar, false, false, 0) - - sw = ScrolledWindow.new(nil, nil) - sw.shadow_type = SHADOW_ETCHED_IN - sw.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC) - vbox.pack_start(sw, true, true, 0) - sw.add(@treeview) - - @status_bar = Statusbar.new - vbox.pack_start(@status_bar, false, false, 0) - - @filename ||= nil - if @filename - data = read_data(@filename) - view_new_model Editor.data2model(data) - end - - signal_connect(:button_release_event) do |_,event| - if event.button == 2 - c = Gtk::Clipboard.get(Gdk::Selection::PRIMARY) - if url = c.wait_for_text - location_open url - end - false - else - true - end - end - end - - # Creates the menu bar with the pulldown menus and returns it. - def create_menu_bar - menu_bar = MenuBar.new - @file_menu = FileMenu.new(@treeview) - menu_bar.append @file_menu.create - @edit_menu = EditMenu.new(@treeview) - menu_bar.append @edit_menu.create - @options_menu = OptionsMenu.new(@treeview) - menu_bar.append @options_menu.create - menu_bar - end - - # Sets editor status to changed, to indicate that the edited data - # containts unsaved changes. - def change - @changed = true - display_title - end - - # Sets editor status to unchanged, to indicate that the edited data - # doesn't containt unsaved changes. - def unchange - @changed = false - display_title - end - - # Puts a new model _model_ into the Gtk::TreeView to be edited. - def view_new_model(model) - @treeview.model = model - @treeview.expanded = true - @treeview.expand_all - unchange - end - - # Displays _text_ in the status bar. - def display_status(text) - @cid ||= nil - @status_bar.pop(@cid) if @cid - @cid = @status_bar.get_context_id('dummy') - @status_bar.push(@cid, text) - end - - # Opens a dialog, asking, if changes should be saved to a file. - def ask_save - if Editor.question_dialog(self, - "Unsaved changes to JSON model. Save?") - if @filename - file_save - else - file_save_as - end - end - end - - # Quit this editor, that is, leave this editor's main loop. - def quit - ask_save if @changed - if Gtk.main_level > 0 - destroy - Gtk.main_quit - end - nil - end - - # Display the new title according to the editor's current state. - def display_title - title = TITLE.dup - title << ": #@filename" if @filename - title << " *" if @changed - self.title = title - end - - # Clear the current model, after asking to save all unsaved changes. - def clear - ask_save if @changed - @filename = nil - self.view_new_model nil - end - - def check_pretty_printed(json) - pretty = !!((nl_index = json.index("\n")) && nl_index != json.size - 1) - @options_menu.pretty_item.active = pretty - end - private :check_pretty_printed - - # Open the data at the location _uri_, if given. Otherwise open a dialog - # to ask for the _uri_. - def location_open(uri = nil) - uri = ask_for_location unless uri - uri or return - ask_save if @changed - data = load_location(uri) or return - view_new_model Editor.data2model(data) - end - - # Open the file _filename_ or call the #select_file method to ask for a - # filename. - def file_open(filename = nil) - filename = select_file('Open as a JSON file') unless filename - data = load_file(filename) or return - view_new_model Editor.data2model(data) - end - - # Edit the string _json_ in the editor. - def edit(json) - if json.respond_to? :read - json = json.read - end - data = parse_json json - view_new_model Editor.data2model(data) - end - - # Save the current file. - def file_save - if @filename - store_file(@filename) - else - file_save_as - end - end - - # Save the current file as the filename - def file_save_as - filename = select_file('Save as a JSON file') - store_file(filename) - end - - # Store the current JSON document to _path_. - def store_file(path) - if path - data = Editor.model2data(@treeview.model.iter_first) - File.open(path + '.tmp', 'wb') do |output| - data or break - if @options_menu.pretty_item.active? - output.puts JSON.pretty_generate(data, :max_nesting => false) - else - output.write JSON.generate(data, :max_nesting => false) - end - end - File.rename path + '.tmp', path - @filename = path - toplevel.display_status("Saved data to '#@filename'.") - unchange - end - rescue SystemCallError => e - Editor.error_dialog(self, "Failed to store JSON file: #{e}!") - end - - # Load the file named _filename_ into the editor as a JSON document. - def load_file(filename) - if filename - if File.directory?(filename) - Editor.error_dialog(self, "Try to select a JSON file!") - nil - else - @filename = filename - if data = read_data(filename) - toplevel.display_status("Loaded data from '#@filename'.") - end - display_title - data - end - end - end - - # Load the data at location _uri_ into the editor as a JSON document. - def load_location(uri) - data = read_data(uri) or return - @filename = nil - toplevel.display_status("Loaded data from '#{uri}'.") - display_title - data - end - - def parse_json(json) - check_pretty_printed(json) - if @encoding && !/^utf8$/i.match(@encoding) - iconverter = Iconv.new('utf8', @encoding) - json = iconverter.iconv(json) - end - JSON::parse(json, :max_nesting => false, :create_additions => false) - end - private :parse_json - - # Read a JSON document from the file named _filename_, parse it into a - # ruby data structure, and return the data. - def read_data(filename) - open(filename) do |f| - json = f.read - return parse_json(json) - end - rescue => e - Editor.error_dialog(self, "Failed to parse JSON file: #{e}!") - return - end - - # Open a file selecton dialog, displaying _message_, and return the - # selected filename or nil, if no file was selected. - def select_file(message) - filename = nil - fs = FileSelection.new(message) - fs.set_modal(true) - @default_dir = File.join(Dir.pwd, '') unless @default_dir - fs.set_filename(@default_dir) - fs.set_transient_for(self) - fs.signal_connect(:destroy) { Gtk.main_quit } - fs.ok_button.signal_connect(:clicked) do - filename = fs.filename - @default_dir = File.join(File.dirname(filename), '') - fs.destroy - Gtk.main_quit - end - fs.cancel_button.signal_connect(:clicked) do - fs.destroy - Gtk.main_quit - end - fs.show_all - Gtk.main - filename - end - - # Ask for location URI a to load data from. Returns the URI as a string. - def ask_for_location - dialog = Dialog.new( - "Load data from location...", - nil, nil, - [ Stock::OK, Dialog::RESPONSE_ACCEPT ], - [ Stock::CANCEL, Dialog::RESPONSE_REJECT ] - ) - hbox = HBox.new(false, 5) - - hbox.pack_start(Label.new("Location:"), false) - hbox.pack_start(location_input = Entry.new) - location_input.width_chars = 60 - location_input.text = @location || '' - - dialog.vbox.pack_start(hbox, false) - - dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) - dialog.show_all - dialog.run do |response| - if response == Dialog::RESPONSE_ACCEPT - return @location = location_input.text - end - end - return - ensure - dialog.destroy if dialog - end - end - - class << self - # Starts a JSON Editor. If a block was given, it yields - # to the JSON::Editor::MainWindow instance. - def start(encoding = 'utf8') # :yield: window - Gtk.init - @window = Editor::MainWindow.new(encoding) - @window.icon_list = [ Editor.fetch_icon('json') ] - yield @window if block_given? - @window.show_all - Gtk.main - end - - # Edit the string _json_ with encoding _encoding_ in the editor. - def edit(json, encoding = 'utf8') - start(encoding) do |window| - window.edit json - end - end - - attr_reader :window - end - end -end diff --git a/ext/json/lib/json/ext.rb b/ext/json/lib/json/ext.rb index ff4fa42329..5bacc5e371 100644 --- a/ext/json/lib/json/ext.rb +++ b/ext/json/lib/json/ext.rb @@ -1,13 +1,45 @@ +# frozen_string_literal: true + require 'json/common' module JSON # This module holds all the modules/classes that implement JSON's # functionality as C extensions. module Ext + class Parser + class << self + def parse(...) + new(...).parse + end + alias_method :parse, :parse # Allow redefinition by extensions + end + + def initialize(source, opts = nil) + @source = source + @config = Config.new(opts) + end + + def source + @source.dup + end + + def parse + @config.parse(@source) + end + end + require 'json/ext/parser' - require 'json/ext/generator' - $DEBUG and warn "Using c extension for JSON." - JSON.parser = Parser - JSON.generator = Generator + Ext::Parser::Config = Ext::ParserConfig + JSON.parser = Ext::Parser + + if RUBY_ENGINE == 'truffleruby' + require 'json/truffle_ruby/generator' + JSON.generator = JSON::TruffleRuby::Generator + else + require 'json/ext/generator' + JSON.generator = Generator + end end + + JSON_LOADED = true unless defined?(JSON::JSON_LOADED) end diff --git a/ext/json/lib/json/ext/generator/state.rb b/ext/json/lib/json/ext/generator/state.rb new file mode 100644 index 0000000000..e4f425af6a --- /dev/null +++ b/ext/json/lib/json/ext/generator/state.rb @@ -0,0 +1,103 @@ +# frozen_string_literal: true + +module JSON + module Ext + module Generator + class State + # call-seq: new(opts = {}) + # + # Instantiates a new State object, configured by _opts_. + # + # Argument +opts+, if given, contains a \Hash of options for the generation. + # See {Generating Options}[rdoc-ref:JSON@Generating+Options]. + def initialize(opts = nil) + if opts && !opts.empty? + configure(opts) + end + end + + # call-seq: configure(opts) + # + # Configure this State instance with the Hash _opts_, and return + # itself. + def configure(opts) + unless opts.is_a?(Hash) + if opts.respond_to?(:to_hash) + opts = opts.to_hash + elsif opts.respond_to?(:to_h) + opts = opts.to_h + else + raise TypeError, "can't convert #{opts.class} into Hash" + end + end + _configure(opts) + end + + alias_method :merge, :configure + + # call-seq: to_h + # + # Returns the configuration instance variables as a hash, that can be + # passed to the configure method. + def to_h + result = { + indent: indent, + space: space, + space_before: space_before, + object_nl: object_nl, + array_nl: array_nl, + as_json: as_json, + allow_nan: allow_nan?, + ascii_only: ascii_only?, + max_nesting: max_nesting, + script_safe: script_safe?, + strict: strict?, + depth: depth, + buffer_initial_length: buffer_initial_length, + } + + allow_duplicate_key = allow_duplicate_key? + unless allow_duplicate_key.nil? + result[:allow_duplicate_key] = allow_duplicate_key + end + + instance_variables.each do |iv| + iv = iv.to_s[1..-1] + result[iv.to_sym] = self[iv] + end + + result + end + + alias_method :to_hash, :to_h + + # call-seq: [](name) + # + # Returns the value returned by method +name+. + def [](name) + ::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0") + + if respond_to?(name) + __send__(name) + else + instance_variable_get("@#{name}") if + instance_variables.include?("@#{name}".to_sym) # avoid warning + end + end + + # call-seq: []=(name, value) + # + # Sets the attribute name to value. + def []=(name, value) + ::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0") + + if respond_to?(name_writer = "#{name}=") + __send__ name_writer, value + else + instance_variable_set "@#{name}", value + end + end + end + end + end +end diff --git a/ext/json/lib/json/generic_object.rb b/ext/json/lib/json/generic_object.rb new file mode 100644 index 0000000000..5c8ace354b --- /dev/null +++ b/ext/json/lib/json/generic_object.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true +begin + require 'ostruct' +rescue LoadError + warn "JSON::GenericObject requires 'ostruct'. Please install it with `gem install ostruct`." +end + +module JSON + class GenericObject < OpenStruct + class << self + alias [] new + + def json_creatable? + @json_creatable + end + + attr_writer :json_creatable + + def json_create(data) + data = data.dup + data.delete JSON.create_id + self[data] + end + + def from_hash(object) + case + when object.respond_to?(:to_hash) + result = new + object.to_hash.each do |key, value| + result[key] = from_hash(value) + end + result + when object.respond_to?(:to_ary) + object.to_ary.map { |a| from_hash(a) } + else + object + end + end + + def load(source, proc = nil, opts = {}) + result = ::JSON.load(source, proc, opts.merge(:object_class => self)) + result.nil? ? new : result + end + + def dump(obj, *args) + ::JSON.dump(obj, *args) + end + end + self.json_creatable = false + + def to_hash + table + end + + def |(other) + self.class[other.to_hash.merge(to_hash)] + end + + def as_json(*) + { JSON.create_id => self.class.name }.merge to_hash + end + + def to_json(*a) + as_json.to_json(*a) + end + end if defined?(::OpenStruct) +end diff --git a/ext/json/lib/json/json.xpm b/ext/json/lib/json/json.xpm deleted file mode 100644 index 2cb626bb05..0000000000 --- a/ext/json/lib/json/json.xpm +++ /dev/null @@ -1,1499 +0,0 @@ -/* XPM */ -static char * json_xpm[] = { -"64 64 1432 2", -" c None", -". c #641839", -"+ c #CF163C", -"@ c #D31C3B", -"# c #E11A38", -"$ c #5F242D", -"% c #320C22", -"& c #9B532D", -"* c #F32E34", -"= c #820F33", -"- c #4B0F34", -"; c #8E1237", -"> c #944029", -", c #961325", -"' c #A00C24", -") c #872C23", -"! c #694021", -"~ c #590D1F", -"{ c #420528", -"] c #D85A2D", -"^ c #7E092B", -"/ c #0E0925", -"( c #0D081F", -"_ c #0F081E", -": c #12071F", -"< c #360620", -"[ c #682A21", -"} c #673F21", -"| c #780E21", -"1 c #A82320", -"2 c #8D1D1F", -"3 c #970127", -"4 c #0D0123", -"5 c #0D0324", -"6 c #3B1E28", -"7 c #C28429", -"8 c #0C0523", -"9 c #0C041E", -"0 c #0E031A", -"a c #11031A", -"b c #13031B", -"c c #13031C", -"d c #11031D", -"e c #19051E", -"f c #390E20", -"g c #9C0C20", -"h c #C00721", -"i c #980320", -"j c #14031E", -"k c #CD9F32", -"l c #C29F2E", -"m c #0F0325", -"n c #0D0321", -"o c #0E0324", -"p c #D08329", -"q c #9D1B27", -"r c #1C0320", -"s c #0D011A", -"t c #120117", -"u c #130017", -"v c #150018", -"w c #160119", -"x c #17021A", -"y c #15021B", -"z c #11021E", -"A c #0F021F", -"B c #8C1821", -"C c #CF4522", -"D c #831821", -"E c #BA7033", -"F c #EDB339", -"G c #C89733", -"H c #280727", -"I c #0F051F", -"J c #0E0420", -"K c #591F27", -"L c #E47129", -"M c #612224", -"N c #0C021D", -"O c #120018", -"P c #140017", -"Q c #170017", -"R c #190018", -"S c #1B0019", -"T c #1B011A", -"U c #18011B", -"V c #15011C", -"W c #12031E", -"X c #460A21", -"Y c #A13823", -"Z c #784323", -"` c #5A0C21", -" . c #BC4530", -".. c #EB5B38", -"+. c #CE4E3B", -"@. c #DD9334", -"#. c #751A27", -"$. c #11071E", -"%. c #0F041C", -"&. c #1E0824", -"*. c #955A28", -"=. c #9A5027", -"-. c #1E0321", -";. c #11011A", -">. c #140018", -",. c #180018", -"'. c #1F001A", -"). c #20001B", -"!. c #1E001A", -"~. c #1B001A", -"{. c #16021B", -"]. c #16041E", -"^. c #220622", -"/. c #5F3525", -"(. c #DE5724", -"_. c #611021", -":. c #0F0925", -"<. c #D1892E", -"[. c #F27036", -"}. c #EC633B", -"|. c #DA293C", -"1. c #E64833", -"2. c #912226", -"3. c #11081C", -"4. c #110419", -"5. c #0F041E", -"6. c #451425", -"7. c #BF6F28", -"8. c #332225", -"9. c #0E021E", -"0. c #13001B", -"a. c #17001A", -"b. c #1C001B", -"c. c #21001C", -"d. c #23001C", -"e. c #21001B", -"f. c #19021A", -"g. c #17041E", -"h. c #150721", -"i. c #602424", -"j. c #D51223", -"k. c #540820", -"l. c #D04D2D", -"m. c #EA8933", -"n. c #875637", -"o. c #88543A", -"p. c #E5923A", -"q. c #891931", -"r. c #130B25", -"s. c #10051B", -"t. c #110217", -"u. c #12021A", -"v. c #761826", -"w. c #E2A728", -"x. c #300224", -"y. c #10011E", -"z. c #16001B", -"A. c #1B001B", -"B. c #21001A", -"C. c #1E0019", -"D. c #1D0019", -"E. c #1A011A", -"F. c #17031C", -"G. c #120720", -"H. c #4E0822", -"I. c #670721", -"J. c #C07630", -"K. c #F59734", -"L. c #BE1B35", -"M. c #0E1435", -"N. c #522037", -"O. c #DB8039", -"P. c #D45933", -"Q. c #420927", -"R. c #0F041D", -"S. c #140118", -"T. c #13021D", -"U. c #100423", -"V. c #7B6227", -"W. c #C04326", -"X. c #0E0020", -"Y. c #13001D", -"Z. c #18001B", -"`. c #1E001B", -" + c #22001C", -".+ c #22001B", -"++ c #1B011B", -"@+ c #16041D", -"#+ c #130520", -"$+ c #860521", -"%+ c #710520", -"&+ c #670A2A", -"*+ c #A66431", -"=+ c #E97536", -"-+ c #F8833A", -";+ c #F77A3A", -">+ c #C45337", -",+ c #0A1C35", -"'+ c #993638", -")+ c #F7863B", -"!+ c #F49736", -"~+ c #94462B", -"{+ c #0E031F", -"]+ c #130119", -"^+ c #160018", -"/+ c #16011B", -"(+ c #15021F", -"_+ c #120123", -":+ c #A65C28", -"<+ c #5C4D23", -"[+ c #0F001F", -"}+ c #14001D", -"|+ c #1A001B", -"1+ c #1F001B", -"2+ c #24001D", -"3+ c #25001D", -"4+ c #24001C", -"5+ c #1F001C", -"6+ c #1A011C", -"7+ c #16021E", -"8+ c #3F0421", -"9+ c #BC0522", -"0+ c #1C041E", -"a+ c #7F5531", -"b+ c #E68A38", -"c+ c #F8933E", -"d+ c #FA7942", -"e+ c #FB7543", -"f+ c #FA6F41", -"g+ c #F1793D", -"h+ c #7D3B3A", -"i+ c #28263B", -"j+ c #D45441", -"k+ c #F8A238", -"l+ c #996B2D", -"m+ c #0E0421", -"n+ c #12011A", -"o+ c #180019", -"p+ c #17001C", -"q+ c #12001F", -"r+ c #4C2B2A", -"s+ c #DB8130", -"t+ c #540023", -"u+ c #0F0120", -"v+ c #16011C", -"w+ c #22001D", -"x+ c #25001F", -"y+ c #26001F", -"z+ c #25001E", -"A+ c #24001E", -"B+ c #1D001C", -"C+ c #18011D", -"D+ c #16031F", -"E+ c #3C0522", -"F+ c #9B0821", -"G+ c #13041E", -"H+ c #F6462E", -"I+ c #E6AB37", -"J+ c #E7A03E", -"K+ c #FA9F44", -"L+ c #FB8A48", -"M+ c #FD7A4A", -"N+ c #FD794A", -"O+ c #FD7748", -"P+ c #FD7E45", -"Q+ c #FD8343", -"R+ c #FB5D42", -"S+ c #6E3A40", -"T+ c #EE8A37", -"U+ c #7E252B", -"V+ c #100520", -"W+ c #13011A", -"X+ c #170019", -"Y+ c #15001C", -"Z+ c #0F0020", -"`+ c #564427", -" @ c #E0BA29", -".@ c #5E2B25", -"+@ c #10011F", -"@@ c #17011C", -"#@ c #1E001D", -"$@ c #23001F", -"%@ c #250020", -"&@ c #24001F", -"*@ c #23001E", -"=@ c #21001E", -"-@ c #1B001C", -";@ c #17021D", -">@ c #14041E", -",@ c #AC0B25", -"'@ c #5E1420", -")@ c #F28635", -"!@ c #C2733E", -"~@ c #984C44", -"{@ c #EA9148", -"]@ c #FB844B", -"^@ c #FD7E4C", -"/@ c #FE7E4C", -"(@ c #FE7E4B", -"_@ c #FE7749", -":@ c #FD7148", -"<@ c #FB7D46", -"[@ c #F89641", -"}@ c #B95634", -"|@ c #0D0927", -"1@ c #11041D", -"2@ c #150119", -"3@ c #180017", -"4@ c #16001A", -"5@ c #13001E", -"6@ c #110023", -"7@ c #944C29", -"8@ c #EE6229", -"9@ c #3D0324", -"0@ c #12021F", -"a@ c #19011D", -"b@ c #21001F", -"c@ c #22001F", -"d@ c #20001E", -"e@ c #1F001D", -"f@ c #1C001C", -"g@ c #19011C", -"h@ c #3D1621", -"i@ c #B53622", -"j@ c #31061F", -"k@ c #841D34", -"l@ c #F2703F", -"m@ c #C14445", -"n@ c #E67349", -"o@ c #FB8E4B", -"p@ c #FD834C", -"q@ c #FE834D", -"r@ c #FE834C", -"s@ c #FE804C", -"t@ c #FD814B", -"u@ c #FB7D49", -"v@ c #F79B43", -"w@ c #AF1234", -"x@ c #0D0625", -"y@ c #13021C", -"z@ c #1A0019", -"A@ c #190019", -"B@ c #410225", -"C@ c #D39729", -"D@ c #AA5927", -"E@ c #0E0422", -"F@ c #15021E", -"G@ c #1A011D", -"H@ c #1D001D", -"I@ c #15031D", -"J@ c #240820", -"K@ c #A01023", -"L@ c #670B21", -"M@ c #3D0D33", -"N@ c #E63C3E", -"O@ c #EF7C45", -"P@ c #F59048", -"Q@ c #FB944A", -"R@ c #FD904A", -"S@ c #FE8E4B", -"T@ c #FE854A", -"U@ c #FE854B", -"V@ c #FE884C", -"W@ c #FC954B", -"X@ c #F8AB45", -"Y@ c #C37A35", -"Z@ c #0D0425", -"`@ c #13011B", -" # c #170018", -".# c #1A0018", -"+# c #1C0019", -"@# c #15001B", -"## c #100120", -"$# c #311F25", -"%# c #E68E28", -"&# c #7A1425", -"*# c #130321", -"=# c #17011E", -"-# c #1A001D", -";# c #19001B", -"># c #16021C", -",# c #130521", -"'# c #6F3123", -")# c #6D3022", -"!# c #C89433", -"~# c #EA7E3E", -"{# c #DB2943", -"]# c #EF7745", -"^# c #FB8544", -"/# c #FD9A43", -"(# c #FE9941", -"_# c #FE9D43", -":# c #FEA548", -"<# c #FEAE49", -"[# c #FCB944", -"}# c #CA9F35", -"|# c #0E0225", -"1# c #11001B", -"2# c #160019", -"3# c #12011B", -"4# c #0F0220", -"5# c #351D26", -"6# c #D85B28", -"7# c #6C0F26", -"8# c #190121", -"9# c #1B001E", -"0# c #1A001C", -"a# c #1D001B", -"b# c #130220", -"c# c #703A23", -"d# c #713A23", -"e# c #140327", -"f# c #411B36", -"g# c #C8713E", -"h# c #7A3A3F", -"i# c #CE2C3C", -"j# c #E77338", -"k# c #9C6535", -"l# c #9C6233", -"m# c #9C6332", -"n# c #9C6A35", -"o# c #C37D3C", -"p# c #FEAC41", -"q# c #FEC23E", -"r# c #826330", -"s# c #100122", -"t# c #120019", -"u# c #150017", -"v# c #190017", -"w# c #1B0018", -"x# c #12001A", -"y# c #10021F", -"z# c #1A0326", -"A# c #5F292A", -"B# c #7B4E29", -"C# c #3C0E25", -"D# c #1A0020", -"E# c #14021F", -"F# c #723B23", -"G# c #14001A", -"H# c #58042A", -"I# c #A28337", -"J# c #C8813B", -"K# c #B14B38", -"L# c #761231", -"M# c #5A132A", -"N# c #0D0726", -"O# c #0C0623", -"P# c #0B0723", -"Q# c #0B0A26", -"R# c #321C2D", -"S# c #C45B33", -"T# c #FEBB33", -"U# c #13052A", -"V# c #13011F", -"W# c #160017", -"X# c #15001A", -"Y# c #12001D", -"Z# c #94062A", -"`# c #630D2C", -" $ c #85292B", -".$ c #AA5E29", -"+$ c #1F0123", -"@$ c #19011F", -"#$ c #1E001C", -"$$ c #15031F", -"%$ c #712122", -"&$ c #712223", -"*$ c #14011B", -"=$ c #110321", -"-$ c #AF0C2B", -";$ c #E7D534", -">$ c #EAC934", -",$ c #84582D", -"'$ c #1B0824", -")$ c #11041E", -"!$ c #10021B", -"~$ c #100119", -"{$ c #100218", -"]$ c #0F041A", -"^$ c #0E0720", -"/$ c #2C1026", -"($ c #D8A328", -"_$ c #140322", -":$ c #160016", -"<$ c #14001F", -"[$ c #120024", -"}$ c #100128", -"|$ c #3C032F", -"1$ c #2C062E", -"2$ c #29022B", -"3$ c #A31D29", -"4$ c #976A25", -"5$ c #1A0321", -"6$ c #17031E", -"7$ c #1B021D", -"8$ c #20001C", -"9$ c #14041F", -"0$ c #703422", -"a$ c #6F3522", -"b$ c #8D0328", -"c$ c #920329", -"d$ c #0F0326", -"e$ c #100321", -"f$ c #11021B", -"g$ c #130117", -"h$ c #140016", -"i$ c #150015", -"j$ c #140015", -"k$ c #130116", -"l$ c #120219", -"m$ c #11031C", -"n$ c #12031D", -"o$ c #170016", -"p$ c #160020", -"q$ c #250029", -"r$ c #670033", -"s$ c #DCA238", -"t$ c #F5C736", -"u$ c #9A732E", -"v$ c #110227", -"w$ c #110324", -"x$ c #811924", -"y$ c #A04323", -"z$ c #250721", -"A$ c #1A041F", -"B$ c #1E011D", -"C$ c #1C011C", -"D$ c #18031D", -"E$ c #130721", -"F$ c #6F3623", -"G$ c #6B3622", -"H$ c #1A001A", -"I$ c #14011F", -"J$ c #12011E", -"K$ c #11011C", -"L$ c #140117", -"M$ c #170015", -"N$ c #150016", -"O$ c #120119", -"P$ c #11011B", -"Q$ c #11001A", -"R$ c #130018", -"S$ c #170118", -"T$ c #170119", -"U$ c #18021E", -"V$ c #1A0126", -"W$ c #6F2332", -"X$ c #E5563B", -"Y$ c #F1B83F", -"Z$ c #F6CC38", -"`$ c #9D7A2D", -" % c #130123", -".% c #130320", -"+% c #2A0721", -"@% c #B00E24", -"#% c #7D0B23", -"$% c #1F0522", -"%% c #1E0220", -"&% c #1D011E", -"*% c #1A031E", -"=% c #15051F", -"-% c #241322", -";% c #A32F23", -">% c #670E21", -",% c #1C001A", -"'% c #19001A", -")% c #180016", -"!% c #160118", -"~% c #140219", -"{% c #11021C", -"]% c #10021E", -"^% c #0F011D", -"/% c #170117", -"(% c #160219", -"_% c #17041D", -":% c #190523", -"<% c #8C042E", -"[% c #B65838", -"}% c #E9D73F", -"|% c #EED43E", -"1% c #D85538", -"2% c #493129", -"3% c #130120", -"4% c #15021D", -"5% c #330822", -"6% c #8A0825", -"7% c #3C0424", -"8% c #1E0322", -"9% c #1C0321", -"0% c #180421", -"a% c #130822", -"b% c #AF2D24", -"c% c #BC5623", -"d% c #2F071F", -"e% c #1A041C", -"f% c #1C031C", -"g% c #1D011C", -"h% c #160117", -"i% c #150419", -"j% c #12081D", -"k% c #0F0923", -"l% c #A77027", -"m% c #A60525", -"n% c #11021A", -"o% c #130218", -"p% c #150319", -"q% c #16061D", -"r% c #180923", -"s% c #9C1D2B", -"t% c #A32636", -"u% c #A66E3B", -"v% c #4B2E3C", -"w% c #412C36", -"x% c #36012D", -"y% c #140123", -"z% c #17001E", -"A% c #19011B", -"B% c #1A0421", -"C% c #340425", -"D% c #9E0326", -"E% c #1F0424", -"F% c #1C0524", -"G% c #180724", -"H% c #A91024", -"I% c #D55D24", -"J% c #90071E", -"K% c #3C051D", -"L% c #1C021C", -"M% c #1C011A", -"N% c #1D001A", -"O% c #160116", -"P% c #150216", -"Q% c #140217", -"R% c #140618", -"S% c #120D1D", -"T% c #231925", -"U% c #B16A2E", -"V% c #FDAC34", -"W% c #D58631", -"X% c #280E2A", -"Y% c #0D0A23", -"Z% c #0F0920", -"`% c #120C21", -" & c #1F1026", -".& c #A3352E", -"+& c #EE9F36", -"@& c #5D2A3C", -"#& c #960D3C", -"$& c #970638", -"%& c #A00330", -"&& c #4D0126", -"*& c #1C001F", -"=& c #280120", -"-& c #290223", -";& c #1F0425", -">& c #260726", -",& c #340A26", -"'& c #850925", -")& c #3A0823", -"!& c #82071D", -"~& c #5E071D", -"{& c #18051C", -"]& c #18021A", -"^& c #190118", -"/& c #160217", -"(& c #150418", -"_& c #130618", -":& c #110718", -"<& c #10081A", -"[& c #110D1D", -"}& c #291C24", -"|& c #A73B2D", -"1& c #FD6B36", -"2& c #FD853C", -"3& c #FD863B", -"4& c #C24A35", -"5& c #6B442F", -"6& c #6D302D", -"7& c #6E252E", -"8& c #8E3B32", -"9& c #DE7739", -"0& c #F48E3F", -"a& c #DD8D41", -"b& c #854F3D", -"c& c #7E2D35", -"d& c #33082B", -"e& c #1C0222", -"f& c #20001F", -"g& c #1F0222", -"h& c #1A0524", -"i& c #440C27", -"j& c #BC1427", -"k& c #20041B", -"l& c #53061C", -"m& c #25071B", -"n& c #11061A", -"o& c #130418", -"p& c #140317", -"q& c #150217", -"r& c #160318", -"s& c #12051B", -"t& c #100C1D", -"u& c #0E101E", -"v& c #0C121F", -"w& c #0C1321", -"x& c #781725", -"y& c #B25D2C", -"z& c #FA6335", -"A& c #FD633C", -"B& c #FE6D42", -"C& c #FE7C42", -"D& c #FE813F", -"E& c #FE873C", -"F& c #FD743B", -"G& c #FB683B", -"H& c #FA7A3E", -"I& c #F98242", -"J& c #F97844", -"K& c #F98943", -"L& c #F79C3D", -"M& c #A25133", -"N& c #280B28", -"O& c #1D021F", -"P& c #1F011C", -"Q& c #280321", -"R& c #1C0724", -"S& c #3F1C27", -"T& c #D33C27", -"U& c #0E061B", -"V& c #0C091C", -"W& c #0C0A1B", -"X& c #0E091A", -"Y& c #11081B", -"Z& c #100A20", -"`& c #0E0D23", -" * c #551227", -".* c #B21829", -"+* c #C42329", -"@* c #C62C29", -"#* c #C55429", -"$* c #E76F2B", -"%* c #F14232", -"&* c #F95E3A", -"** c #FC6740", -"=* c #FE6E45", -"-* c #FE7246", -";* c #FE7545", -">* c #FE7744", -",* c #FD7745", -"'* c #FD7845", -")* c #FD7847", -"!* c #FD7948", -"~* c #FD7B44", -"{* c #FC7C3B", -"]* c #6F3130", -"^* c #140B24", -"/* c #19031D", -"(* c #1C011B", -"_* c #5A011F", -":* c #B70421", -"<* c #380824", -"[* c #3E2626", -"}* c #9F5626", -"|* c #13051E", -"1* c #360A21", -"2* c #361223", -"3* c #371724", -"4* c #381824", -"5* c #3B1524", -"6* c #3E1E26", -"7* c #471A29", -"8* c #DB252E", -"9* c #ED2733", -"0* c #EE5436", -"a* c #F04237", -"b* c #F33934", -"c* c #F53D2F", -"d* c #D7312B", -"e* c #AF212B", -"f* c #3A2C31", -"g* c #F65F39", -"h* c #FB6F41", -"i* c #FD6D45", -"j* c #FE7047", -"k* c #FE7647", -"l* c #FE7847", -"m* c #FE7848", -"n* c #FE7748", -"o* c #FE7948", -"p* c #FE7C48", -"q* c #FE7C47", -"r* c #FE7642", -"s* c #FE7439", -"t* c #6D332C", -"u* c #100B21", -"v* c #16031B", -"w* c #2B001B", -"x* c #22011F", -"y* c #220521", -"z* c #1B0A23", -"A* c #421425", -"B* c #951924", -"C* c #381023", -"D* c #E94028", -"E* c #E7302B", -"F* c #EF432D", -"G* c #F4302E", -"H* c #F32C30", -"I* c #CB4432", -"J* c #DD3235", -"K* c #EF4B3A", -"L* c #F0333E", -"M* c #CC3D3F", -"N* c #E4313C", -"O* c #F34834", -"P* c #D13E2C", -"Q* c #431825", -"R* c #0E1424", -"S* c #3C202C", -"T* c #F15537", -"U* c #F97140", -"V* c #FC6E45", -"W* c #FE7547", -"X* c #FE7947", -"Y* c #FE7B48", -"Z* c #FE7D48", -"`* c #FE8047", -" = c #FE7A42", -".= c #FE7A38", -"+= c #6D442B", -"@= c #0F0B21", -"#= c #15031A", -"$= c #49001B", -"%= c #2F001C", -"&= c #21021E", -"*= c #220620", -"== c #1B0D23", -"-= c #641625", -";= c #951823", -">= c #390F25", -",= c #AC3A2A", -"'= c #B6492E", -")= c #ED7531", -"!= c #F45A34", -"~= c #F54C36", -"{= c #C72D39", -"]= c #DE283C", -"^= c #F33B40", -"/= c #F34142", -"(= c #D0393F", -"_= c #E72E39", -":= c #DB3C2E", -"<= c #461724", -"[= c #0F0D1E", -"}= c #140B1E", -"|= c #341427", -"1= c #CB4834", -"2= c #F7743F", -"3= c #FB7145", -"4= c #FE7747", -"5= c #FE7A47", -"6= c #FF7B48", -"7= c #FF7C48", -"8= c #FE7F47", -"9= c #FE8247", -"0= c #FE8642", -"a= c #FE8439", -"b= c #6D442D", -"c= c #0F0A21", -"d= c #14031A", -"e= c #20031D", -"f= c #210821", -"g= c #191024", -"h= c #CC1C25", -"i= c #961423", -"j= c #2C162C", -"k= c #BD242E", -"l= c #EF2C31", -"m= c #F54C34", -"n= c #F34037", -"o= c #F5353A", -"p= c #F7413D", -"q= c #F8423D", -"r= c #F93A39", -"s= c #F95731", -"t= c #341425", -"u= c #110A1D", -"v= c #140619", -"w= c #18051B", -"x= c #200F26", -"y= c #864833", -"z= c #F8773F", -"A= c #FC7445", -"B= c #FF7E48", -"C= c #FF7E49", -"D= c #FF7D49", -"E= c #FF7D48", -"F= c #FE8347", -"G= c #FE8743", -"H= c #FE893B", -"I= c #6E452F", -"J= c #100E23", -"K= c #14041A", -"L= c #55041D", -"M= c #540921", -"N= c #161124", -"O= c #CE6A25", -"P= c #3F1129", -"Q= c #170A29", -"R= c #0F0F29", -"S= c #15132B", -"T= c #1E182D", -"U= c #A82B3D", -"V= c #CB6633", -"W= c #CC6932", -"X= c #CC3D2D", -"Y= c #331225", -"Z= c #0F091C", -"`= c #120417", -" - c #160216", -".- c #190419", -"+- c #210F26", -"@- c #8C4934", -"#- c #F97A40", -"$- c #FC7545", -"%- c #FF7B49", -"&- c #FE7D46", -"*- c #FE7E43", -"=- c #FD7B3E", -"-- c #FA6934", -";- c #532328", -">- c #130B1D", -",- c #150519", -"'- c #14041C", -")- c #120920", -"!- c #C43624", -"~- c #A21E23", -"{- c #F87C30", -"]- c #C9302D", -"^- c #300F2A", -"/- c #591129", -"(- c #171328", -"_- c #171628", -":- c #141829", -"<- c #101A2B", -"[- c #0F172B", -"}- c #0F1226", -"|- c #0E0C20", -"1- c #100619", -"2- c #140316", -"3- c #19051B", -"4- c #3C1428", -"5- c #E04B36", -"6- c #FA7B41", -"7- c #FD7346", -"8- c #FE7548", -"9- c #FF7849", -"0- c #FF7749", -"a- c #FE7B47", -"b- c #FE7945", -"c- c #FC7740", -"d- c #FA7E39", -"e- c #C1432F", -"f- c #131523", -"g- c #130A1C", -"h- c #420621", -"i- c #D08423", -"j- c #F87739", -"k- c #C03D37", -"l- c #962B34", -"m- c #A14332", -"n- c #E54B30", -"o- c #9E3E2F", -"p- c #7F262E", -"q- c #922D2E", -"r- c #9C4B2E", -"s- c #65212C", -"t- c #101628", -"u- c #101022", -"v- c #11091C", -"w- c #130619", -"x- c #160A1E", -"y- c #43252C", -"z- c #F66439", -"A- c #FA6942", -"B- c #FD6C47", -"C- c #FE6E48", -"D- c #FE6F48", -"E- c #FE7049", -"F- c #FE714A", -"G- c #FE744A", -"H- c #FE7846", -"I- c #FD7243", -"J- c #FC703E", -"K- c #FA6C37", -"L- c #81312B", -"M- c #121123", -"N- c #15071D", -"O- c #16031A", -"P- c #17021B", -"Q- c #8F3D22", -"R- c #F8393E", -"S- c #E42A3D", -"T- c #E7473B", -"U- c #FB503B", -"V- c #FB4F3A", -"W- c #F95439", -"X- c #ED4C38", -"Y- c #F45938", -"Z- c #FB6537", -"`- c #EA5236", -" ; c #CE6232", -".; c #CD392C", -"+; c #181425", -"@; c #120F21", -"#; c #130D20", -"$; c #151225", -"%; c #903431", -"&; c #F8703D", -"*; c #FB6344", -"=; c #FD6748", -"-; c #FE6849", -";; c #FE6949", -">; c #FE6A49", -",; c #FE6C4A", -"'; c #FE704A", -"); c #FE734A", -"!; c #FE7449", -"~; c #FE7347", -"{; c #FE7145", -"]; c #FD6C42", -"^; c #FD753D", -"/; c #F36E35", -"(; c #CB452C", -"_; c #600D24", -":; c #1C061F", -"<; c #1E031F", -"[; c #5B3821", -"}; c #CE9822", -"|; c #FA4341", -"1; c #FB4341", -"2; c #FC4541", -"3; c #FC4542", -"4; c #FC4143", -"5; c #FC4D42", -"6; c #FB5042", -"7; c #FB5342", -"8; c #FC5242", -"9; c #FD4F40", -"0; c #FD503E", -"a; c #FB6339", -"b; c #F45E33", -"c; c #A12A2E", -"d; c #401E2C", -"e; c #452D2F", -"f; c #F74F38", -"g; c #FA5940", -"h; c #FC6245", -"i; c #FE6447", -"j; c #FE6449", -"k; c #FE6549", -"l; c #FE6749", -"m; c #FE6B49", -"n; c #FE6D49", -"o; c #FE6D48", -"p; c #FE6D47", -"q; c #FE6D45", -"r; c #FE6C44", -"s; c #FE6A42", -"t; c #FE663C", -"u; c #FC6233", -"v; c #752129", -"w; c #1F0922", -"x; c #750520", -"y; c #81061F", -"z; c #FA3D42", -"A; c #FB4142", -"B; c #FD4543", -"C; c #FD4844", -"D; c #FD4A45", -"E; c #FD4D45", -"F; c #FD5045", -"G; c #FD5345", -"H; c #FE5346", -"I; c #FE5445", -"J; c #FD5444", -"K; c #FC4F41", -"L; c #FA513D", -"M; c #F95339", -"N; c #F63736", -"O; c #F75737", -"P; c #F95F3B", -"Q; c #FB5840", -"R; c #FD5F43", -"S; c #FE6345", -"T; c #FE6547", -"U; c #FE6548", -"V; c #FE6448", -"W; c #FE6248", -"X; c #FE6348", -"Y; c #FE6748", -"Z; c #FE6848", -"`; c #FE6846", -" > c #FE6A45", -".> c #FE6D43", -"+> c #FE703F", -"@> c #FC6F36", -"#> c #6F302B", -"$> c #140A22", -"%> c #FA3B42", -"&> c #FC4243", -"*> c #FD4744", -"=> c #FE4A45", -"-> c #FE4C47", -";> c #FE4D47", -">> c #FE5047", -",> c #FE5347", -"'> c #FE5447", -")> c #FD5246", -"!> c #FB503F", -"~> c #FA543D", -"{> c #9B3D3B", -"]> c #A3433B", -"^> c #F9683D", -"/> c #FC6940", -"(> c #FE6342", -"_> c #FE6645", -":> c #FE6646", -"<> c #FE6147", -"[> c #FE6048", -"}> c #FE6148", -"|> c #FE6746", -"1> c #FE6A46", -"2> c #FE6F45", -"3> c #FE7441", -"4> c #FC7D39", -"5> c #6C422E", -"6> c #0F0F23", -"7> c #FA4142", -"8> c #FC4643", -"9> c #FE4D46", -"0> c #FE4E47", -"a> c #FE4F48", -"b> c #FE5148", -"c> c #FE5348", -"d> c #FE5548", -"e> c #FE5247", -"f> c #FD5445", -"g> c #FC5544", -"h> c #F96041", -"i> c #D33F3D", -"j> c #392D39", -"k> c #973C38", -"l> c #F94E3A", -"m> c #FD693E", -"n> c #FE6C43", -"o> c #FE6047", -"p> c #FE5D47", -"q> c #FE5E48", -"r> c #FE6948", -"s> c #FE6947", -"t> c #FE6B47", -"u> c #FE6E46", -"v> c #FD6D43", -"w> c #FB723D", -"x> c #D54A33", -"y> c #301C29", -"z> c #FB4A42", -"A> c #FD4B44", -"B> c #FE4F47", -"C> c #FE5048", -"D> c #FE5648", -"E> c #FE5848", -"F> c #FE5747", -"G> c #FE5547", -"H> c #FC5945", -"I> c #F95742", -"J> c #F3543D", -"K> c #A33336", -"L> c #302032", -"M> c #152433", -"N> c #CD3E38", -"O> c #FD5A3F", -"P> c #FE6343", -"Q> c #FE6446", -"R> c #FE6247", -"S> c #FE6A47", -"T> c #FC6542", -"U> c #FB6A3B", -"V> c #FA6D34", -"W> c #D73C2D", -"X> c #442428", -"Y> c #281323", -"Z> c #FD4E42", -"`> c #FD4D43", -" , c #FE4D45", -"., c #FE5248", -"+, c #FE5947", -"@, c #FE5C47", -"#, c #FE5B47", -"$, c #FE5A47", -"%, c #FE5847", -"&, c #FC5C45", -"*, c #F95B43", -"=, c #F3613F", -"-, c #E74F37", -";, c #8C2431", -">, c #161E2F", -",, c #CD4E33", -"', c #FD503A", -"), c #FE5D40", -"!, c #FE6445", -"~, c #FE6946", -"{, c #FE6847", -"], c #FE6747", -"^, c #FD6644", -"/, c #FD6241", -"(, c #FD5B3D", -"_, c #FE6739", -":, c #FE6135", -"<, c #AB4830", -"[, c #733E2A", -"}, c #161224", -"|, c #FC4E42", -"1, c #FE4D44", -"2, c #FE4E46", -"3, c #FE5147", -"4, c #FE5E47", -"5, c #FD5C46", -"6, c #FA5B44", -"7, c #F45441", -"8, c #EB393A", -"9, c #CC3433", -"0, c #47212F", -"a, c #59242F", -"b, c #FC6734", -"c, c #FC6F3A", -"d, c #FC723E", -"e, c #FD6540", -"f, c #FE6442", -"g, c #FE6643", -"h, c #FE6944", -"i, c #FE6546", -"j, c #FE6444", -"k, c #FE6143", -"l, c #FE5E41", -"m, c #FE613F", -"n, c #FE683C", -"o, c #FE7937", -"p, c #A25030", -"q, c #692629", -"r, c #151122", -"s, c #FA573F", -"t, c #FB4D40", -"u, c #FC4F43", -"v, c #FE5246", -"w, c #FF6347", -"x, c #FE5F48", -"y, c #F65942", -"z, c #F0493D", -"A, c #ED3736", -"B, c #73262F", -"C, c #10152C", -"D, c #3B292F", -"E, c #363034", -"F, c #AC3938", -"G, c #FC6B3B", -"H, c #FD763C", -"I, c #FE6D3F", -"J, c #FE6341", -"K, c #FE6642", -"L, c #FE6745", -"M, c #FE6245", -"N, c #FE6244", -"O, c #FE6841", -"P, c #FF683B", -"Q, c #EC7035", -"R, c #D0412D", -"S, c #3A1627", -"T, c #CF3938", -"U, c #F6543C", -"V, c #FB5040", -"W, c #FD5544", -"X, c #FE5A48", -"Y, c #FE5D48", -"Z, c #FE5F47", -"`, c #FF6147", -" ' c #FD5C45", -".' c #FB5B43", -"+' c #FA5A42", -"@' c #F76040", -"#' c #F4623D", -"$' c #F26D38", -"%' c #EC4130", -"&' c #380E2B", -"*' c #13122C", -"=' c #362D31", -"-' c #353435", -";' c #352E37", -">' c #2D3337", -",' c #CC5838", -"'' c #CD6F3A", -")' c #CE6E3D", -"!' c #FE793F", -"~' c #FD7541", -"{' c #FD6243", -"]' c #FE6545", -"^' c #FF6543", -"/' c #FF6240", -"(' c #FE723B", -"_' c #FE8034", -":' c #442D2C", -"<' c #311725", -"[' c #222830", -"}' c #B73B36", -"|' c #F94C3D", -"1' c #FD5543", -"2' c #FE5B48", -"3' c #FF5E47", -"4' c #FE5C48", -"5' c #FC5B44", -"6' c #F95640", -"7' c #C34E3D", -"8' c #A45A3A", -"9' c #F37438", -"0' c #F28935", -"a' c #AF422F", -"b' c #240D2B", -"c' c #88292F", -"d' c #FA8E34", -"e' c #FC7E38", -"f' c #FC5939", -"g' c #694A37", -"h' c #693437", -"i' c #382638", -"j' c #142439", -"k' c #9F483A", -"l' c #C45E3C", -"m' c #FD7240", -"n' c #FF6645", -"o' c #FF6245", -"p' c #FF6045", -"q' c #FF6146", -"r' c #FF6246", -"s' c #FF6446", -"t' c #FF6545", -"u' c #FE763F", -"v' c #FE7237", -"w' c #C65331", -"x' c #3D272A", -"y' c #0D1E2B", -"z' c #683032", -"A' c #F9453A", -"B' c #FD5341", -"C' c #FE5A46", -"D' c #FF5A48", -"E' c #FE5948", -"F' c #FD5A47", -"G' c #FC5D43", -"H' c #F95B3D", -"I' c #713F37", -"J' c #1E2D32", -"K' c #C44531", -"L' c #EF7A2F", -"M' c #6B2E2C", -"N' c #0F0E2C", -"O' c #F56633", -"P' c #FA803A", -"Q' c #FC673E", -"R' c #FD673E", -"S' c #FC6F3C", -"T' c #FA6E3B", -"U' c #C6633A", -"V' c #A06739", -"W' c #835638", -"X' c #381F38", -"Y' c #713B38", -"Z' c #7B503C", -"`' c #FE7741", -" ) c #FE7344", -".) c #FE6D46", -"+) c #FF6946", -"@) c #FF5E46", -"#) c #FF5D46", -"$) c #FF5D47", -"%) c #FF5F48", -"&) c #FF6248", -"*) c #FE6941", -"=) c #FC783C", -"-) c #C46B35", -";) c #892730", -">) c #111629", -",) c #1F2630", -"') c #AD3939", -")) c #FC5D41", -"!) c #FE5946", -"~) c #FF5848", -"{) c #FE5549", -"]) c #FC5E42", -"^) c #FA673B", -"/) c #DB7033", -"() c #392E2B", -"_) c #311A28", -":) c #3C2127", -"<) c #1D1027", -"[) c #92102C", -"}) c #F58336", -"|) c #FA673E", -"1) c #FD6642", -"2) c #FD5A41", -"3) c #FC6D41", -"4) c #FC6D3F", -"5) c #FD683E", -"6) c #F38C39", -"7) c #CE6535", -"8) c #612E34", -"9) c #1D2637", -"0) c #71513E", -"a) c #FF6847", -"b) c #FF5F47", -"c) c #FF5A46", -"d) c #FF5847", -"e) c #FF5748", -"f) c #FF594A", -"g) c #FF5E4B", -"h) c #FE654C", -"i) c #FE694B", -"j) c #FE6B48", -"k) c #FC6A43", -"l) c #F7683E", -"m) c #EC6E39", -" ", -" ", -" ", -" ", -" ", -" ", -" ", -" ", -" . + @ # $ % ", -" & * = - ; > , ' ) ! ~ ", -" { ] ^ / ( _ : < [ } | 1 2 ", -" 3 4 5 6 7 8 9 0 a b c d e f g h i j ", -" k l m n o p q r s t u v w x y z A B C D ", -" E F G H I J K L M N O P Q R S T U V W X Y Z ` ", -" ...+.@.#.$.%.&.*.=.-.;.>.,.S '.).!.~.{.].^./.(._. ", -" :.<.[.}.|.1.2.3.4.5.6.7.8.9.0.a.b.c.d.e.!.S f.g.h.i.j.k. ", -" l.m.n.o.p.q.r.s.t.u.J v.w.x.y.z.A.c.d.d.B.C.D.E.F.G.H.I. ", -" J.K.L.M.N.O.P.Q.R.t S.T.U.V.W.X.Y.Z.`. +d.d..+B.'.++@+#+$+%+ ", -" &+*+=+-+;+>+,+'+)+!+~+{+]+^+/+(+_+:+<+[+}+|+1+d.2+3+4+d.5+6+7+8+9+0+ ", -" a+b+c+d+e+f+g+h+i+j+k+l+m+n+^+o+p+q+r+s+t+u+v+b.w+x+y+z+A+w+B+C+D+E+F+G+ ", -" H+I+J+K+L+M+N+O+P+Q+R+S+T+U+V+W+Q ,.X+Y+Z+`+ @.@+@@@#@$@%@&@*@=@#@-@;@>@,@'@ ", -" )@!@~@{@]@^@/@(@_@:@<@[@}@|@1@2@3@R ,.4@5@6@7@8@9@0@a@#@b@c@=@d@e@f@g@>@h@i@j@ ", -" k@l@m@n@o@p@q@r@s@t@u@v@w@x@y@^+R S z@A@z.q+B@C@D@E@F@G@H@#@e@#@#@f@g@I@J@K@L@ ", -" M@N@O@P@Q@R@S@T@U@V@W@X@Y@Z@`@ #.#+#+#S A@@###$#%#&#*#=#-#f@B+B+B+f@;#>#,#'#)# ", -" !#~#{#]#^#/#(#(#_#:#<#[#}#|#1#^+.#S +#+#z@2#3#4#5#6#7#8#9#0#A.B+B+a#A.@@b#c#d# ", -" e#f#g#h#i#j#k#l#m#n#o#p#q#r#s#t#u#v#.#w#S R ^+x#y#z#A#B#C#D#-#A.a#`.`.b.g@E#d#F# ", -" G#0@H#I#J#K#L#M#N#O#P#Q#R#S#T#U#V#>.W#3@v#R R X+X#Y#s#Z#`# $.$+$@$g@f@5+5+#$6+$$%$&$ ", -" *$=$-$;$>$,$'$)$!$~${$]$^$/$($_$*$u#:$Q 3@,.X+z.<$[$}$|$1$2$3$4$5$6$7$e@8$#$G@9$0$a$ ", -" ,.4@E#b$c$d$e$f$g$h$i$j$k$l$m$n$`@>.:$o$3@,. #a.p$q$r$s$t$u$v$w$x$y$z$A$B$#@C$D$E$F$G$ ", -" R S H$v+I$J$K$n+L$:$o$o$M$N$L$O$P$Q$R$N$o$3@S$T$U$V$W$X$Y$Z$`$ %.%+%@%#%$%%%&%*%=%-%;%>% ", -" E.,%~.'%Z.4@v W#o$)%)%)%Q !%~%{%]%^%Q$u u#/%(%_%:%<%[%}%|%1%2%3%4%=%5%6%7%8%9%0%a%b%c%d% ", -" e%f%g%a#,%,%z@R 3@3@3@)%Q h%i%j%k%l%m%{+n%o%p%q%r%s%t%u%v%w%x%y%z%A%*%B%C%D%E%F%G%H%I% ", -" J%K%L%M%N%D.S v#)%)%O%P%Q%R%S%T%U%V%W%X%Y%Z%`% &.&+&@&#&$&%&&&*&f@a##@=&-&;&>&,&'&)& ", -" !&~&{&]&^&.#w#^&/%/&(&_&:&<&[&}&|&1&2&3&4&5&6&7&8&9&0&a&b&c&d&e&e@1+5+e@f&g&h&i&j& ", -" k&l&m&n&o&p&q&r&i%s&3.t&u&v&w&x&y&z&A&B&C&D&E&F&G&H&I&J&K&L&M&N&O&P&1+`.e@f&Q&R&S&T& ", -" 0 U&V&W&X&<&Y&j%Z&`& *.*+*@*#*$*%*&***=*-*;*>*>*,*'*)*!*~*{*]*^*/*(*a#B+#@_*:*<*[*}* ", -" |*1*2*3*4*5*6*7*8*9*0*a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*q*r*s*t*u*v*E.w*d.e@x*y*z*A*B* ", -" C*D*E*F*G*H*I*J*K*L*M*N*O*P*Q*R*S*T*U*V*W*l*X*o*o*Y*Z*`* =.=+=@=#='%$=%=e@&=*===-=;= ", -" >=,='=)=!=~={=]=^=/=(=_=:=<=[=}=|=1=2=3=4=5=p*6=6=7=8=9=0=a=b=c=d=A@~.b.B+e=f=g=h=i= ", -" j=k=l=m=n=o=p=q=r=s=t=u=v=w=x=y=z=A=5=Z*B=C=D=E=8=F=G=H=I=J=K=S$R z@'%L=M=N=O= ", -" P=Q=R=S=T=U=V=W=X=Y=Z=`= -.-+-@-#-$-5=p*E=D=%-%-q*&-*-=---;->-,-/%3@^+'-)-!-~- ", -" {-]-^-/-(-_-:-<-[-}-|-1-2- -3-4-5-6-7-8-n*m*9-0-9-o*a-b-c-d-e-f-g-(&h%w c h-i- ", -" j-k-l-m-n-o-p-q-r-s-t-u-v-w-,-x-y-z-A-B-C-D-E-E-F-G-_@m*H-I-J-K-L-M-N-O-P-(+Q- ", -" R-S-T-U-V-W-X-Y-Z-`- ;.;+;@;#;$;%;&;*;=;-;-;;;>;,;';);!;~;{;];^;/;(;_;:;<;[;}; ", -" |;1;2;3;4;5;6;7;8;9;0;a;b;c;d;e;f;g;h;i;j;j;k;k;l;m;n;o;p;q;r;s;t;u;v;w;x;y; ", -" z;A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;k;Y;Z;`; >r;.>+>@>#>$> ", -" %>&>*>=>->;>>>,>'>,>)>F;8;!>~>{>]>^>/>(>_>:>i;<>[>X;}>i;|>1>q;2>3>4>5>6> ", -" 7>8>=>9>0>a>b>c>d>,>e>e>f>g>h>i>j>k>l>m>n>:>i;o>p>q>W;r>s>t>p;u>v>w>x>y> ", -" z>A>9>0>B>C>c>D>E>F>G>G>F>H>I>J>K>L>M>N>O>P>Q>R>o>R>T;s>S>S>S>t>1>T>U>V>W>X>Y> ", -" Z>`> ,9>B>.,D>+,@,#,$,%,$,&,*,=,-,;,>,,,',),P>!,!,_>~,t>s>{,],{,],^,/,(,_,:,<,[,}, ", -" |,`>1,2,3,G>+,4,o>o>4,@,@,5,6,7,8,9,0,a,b,c,d,e,f,g,h, >~,|>T;T;T;i,j,k,l,m,n,o,p,q,r, ", -" s,t,u,v,G>%,@,o>w,R>x,p>@,5,6,y,z,A,B,C,D,E,F,G,H,I,J,K,L,L,i,i;i;i;Q>S;M,N,P>O,P,Q,R,S, ", -" T,U,V,W,%,X,Y,Z,`,[>q>@, '.'+'@'#'$'%'&'*'='-';'>',''')'!'~'{'N,i,:>_>]'M,M,Q>_>^'/'('_':'<' ", -" ['}'|'1'$,X,2'p>3'4'2'@,5'6'7'8'9'0'a'b'c'd'e'f'g'h'i'j'k'l'd,m'g, > >n'o'p'q'r's't'.>u'v'w'x' ", -" y'z'A'B'C'X,X,2'D'E'E'F'G'H'I'J'K'L'M'N'O'P'Q'R'S'T'U'V'W'X'Y'Z'`' ).)+)r'@)#)$)%)&)l;1>*)=)-);) ", -" >),)')))!)X,E'X,~){)d>!)])^)/)()_):)<)[)})|)1)f,2)3)4)5)6)7)8)9)0)*--*a)b)c)d)e)f)g)h)i)j)k)l)m) ", -" ", -" ", -" ", -" ", -" ", -" ", -" ", -" "}; diff --git a/ext/json/lib/json/pure.rb b/ext/json/lib/json/pure.rb deleted file mode 100644 index 6af8705c5b..0000000000 --- a/ext/json/lib/json/pure.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'json/common' -require 'json/pure/parser' -require 'json/pure/generator' - -module JSON - # Swap consecutive bytes of _string_ in place. - def self.swap!(string) # :nodoc: - 0.upto(string.size / 2) do |i| - break unless string[2 * i + 1] - string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i] - end - string - end - - # This module holds all the modules/classes that implement JSON's - # functionality in pure ruby. - module Pure - $DEBUG and warn "Using pure library for JSON." - JSON.parser = Parser - JSON.generator = Generator - end -end diff --git a/ext/json/lib/json/pure/generator.rb b/ext/json/lib/json/pure/generator.rb deleted file mode 100644 index 9c7cfac0a2..0000000000 --- a/ext/json/lib/json/pure/generator.rb +++ /dev/null @@ -1,394 +0,0 @@ -module JSON - MAP = { - "\x0" => '\u0000', - "\x1" => '\u0001', - "\x2" => '\u0002', - "\x3" => '\u0003', - "\x4" => '\u0004', - "\x5" => '\u0005', - "\x6" => '\u0006', - "\x7" => '\u0007', - "\b" => '\b', - "\t" => '\t', - "\n" => '\n', - "\xb" => '\u000b', - "\f" => '\f', - "\r" => '\r', - "\xe" => '\u000e', - "\xf" => '\u000f', - "\x10" => '\u0010', - "\x11" => '\u0011', - "\x12" => '\u0012', - "\x13" => '\u0013', - "\x14" => '\u0014', - "\x15" => '\u0015', - "\x16" => '\u0016', - "\x17" => '\u0017', - "\x18" => '\u0018', - "\x19" => '\u0019', - "\x1a" => '\u001a', - "\x1b" => '\u001b', - "\x1c" => '\u001c', - "\x1d" => '\u001d', - "\x1e" => '\u001e', - "\x1f" => '\u001f', - '"' => '\"', - '\\' => '\\\\', - '/' => '\/', - } # :nodoc: - - # Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with - # UTF16 big endian characters as \u????, and return it. - def utf8_to_json(string) # :nodoc: - string = string.dup.force_encoding(Encoding::ASCII_8BIT) - string.gsub!(/["\\\/\x0-\x1f]/) { MAP[$&] } - string.gsub!(/( - (?: - [\xc2-\xdf][\x80-\xbf] | - [\xe0-\xef][\x80-\xbf]{2} | - [\xf0-\xf4][\x80-\xbf]{3} - )+ | - [\x80-\xc1\xf5-\xff] # invalid - )/nx) { |c| - c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'" - c.unpack("U*").map{|c| - c>0xFFFF ? ('\ud%03x\ud%03x'%[0x7C0+c/1024,0xC00+c%1024]) : ('\u%04x'%c) - }.join("") - } - string - end - module_function :utf8_to_json - - module Pure - module Generator - # This class is used to create State instances, that are use to hold data - # while generating a JSON text from a a Ruby data structure. - class State - # Creates a State object from _opts_, which ought to be Hash to create - # a new State instance configured by _opts_, something else to create - # an unconfigured instance. If _opts_ is a State object, it is just - # returned. - def self.from_state(opts) - case opts - when self - opts - when Hash - new(opts) - else - new - end - end - - # Instantiates a new State object, configured by _opts_. - # - # _opts_ can have the following keys: - # - # * *indent*: a string used to indent levels (default: ''), - # * *space*: a string that is put after, a : or , delimiter (default: ''), - # * *space_before*: a string that is put before a : pair delimiter (default: ''), - # * *object_nl*: a string that is put at the end of a JSON object (default: ''), - # * *array_nl*: a string that is put at the end of a JSON array (default: ''), - # * *check_circular*: true if checking for circular data structures - # should be done (the default), false otherwise. - # * *check_circular*: true if checking for circular data structures - # should be done, false (the default) otherwise. - # * *allow_nan*: true if NaN, Infinity, and -Infinity should be - # generated, otherwise an exception is thrown, if these values are - # encountered. This options defaults to false. - def initialize(opts = {}) - @seen = {} - @indent = '' - @space = '' - @space_before = '' - @object_nl = '' - @array_nl = '' - @check_circular = true - @allow_nan = false - configure opts - end - - # This string is used to indent levels in the JSON text. - attr_accessor :indent - - # This string is used to insert a space between the tokens in a JSON - # string. - attr_accessor :space - - # This string is used to insert a space before the ':' in JSON objects. - attr_accessor :space_before - - # This string is put at the end of a line that holds a JSON object (or - # Hash). - attr_accessor :object_nl - - # This string is put at the end of a line that holds a JSON array. - attr_accessor :array_nl - - # This integer returns the maximum level of data structure nesting in - # the generated JSON, max_nesting = 0 if no maximum is checked. - attr_accessor :max_nesting - - def check_max_nesting(depth) # :nodoc: - return if @max_nesting.zero? - current_nesting = depth + 1 - current_nesting > @max_nesting and - raise NestingError, "nesting of #{current_nesting} is too deep" - end - - # Returns true, if circular data structures should be checked, - # otherwise returns false. - def check_circular? - @check_circular - end - - # Returns true if NaN, Infinity, and -Infinity should be considered as - # valid JSON and output. - def allow_nan? - @allow_nan - end - - # Returns _true_, if _object_ was already seen during this generating - # run. - def seen?(object) - @seen.key?(object.__id__) - end - - # Remember _object_, to find out if it was already encountered (if a - # cyclic data structure is if a cyclic data structure is rendered). - def remember(object) - @seen[object.__id__] = true - end - - # Forget _object_ for this generating run. - def forget(object) - @seen.delete object.__id__ - end - - # Configure this State instance with the Hash _opts_, and return - # itself. - def configure(opts) - @indent = opts[:indent] if opts.key?(:indent) - @space = opts[:space] if opts.key?(:space) - @space_before = opts[:space_before] if opts.key?(:space_before) - @object_nl = opts[:object_nl] if opts.key?(:object_nl) - @array_nl = opts[:array_nl] if opts.key?(:array_nl) - @check_circular = !!opts[:check_circular] if opts.key?(:check_circular) - @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan) - if !opts.key?(:max_nesting) # defaults to 19 - @max_nesting = 19 - elsif opts[:max_nesting] - @max_nesting = opts[:max_nesting] - else - @max_nesting = 0 - end - self - end - - # Returns the configuration instance variables as a hash, that can be - # passed to the configure method. - def to_h - result = {} - for iv in %w[indent space space_before object_nl array_nl check_circular allow_nan max_nesting] - result[iv.intern] = instance_variable_get("@#{iv}") - end - result - end - end - - module GeneratorMethods - module Object - # Converts this object to a string (calling #to_s), converts - # it to a JSON string, and returns the result. This is a fallback, if no - # special method #to_json was defined for some object. - def to_json(*) to_s.to_json end - end - - module Hash - # Returns a JSON string containing a JSON object, that is unparsed from - # this Hash instance. - # _state_ is a JSON::State object, that can also be used to configure the - # produced JSON string output further. - # _depth_ is used to find out nesting depth, to indent accordingly. - def to_json(state = nil, depth = 0, *) - if state - state = JSON.state.from_state(state) - state.check_max_nesting(depth) - json_check_circular(state) { json_transform(state, depth) } - else - json_transform(state, depth) - end - end - - private - - def json_check_circular(state) - if state and state.check_circular? - state.seen?(self) and raise JSON::CircularDatastructure, - "circular data structures not supported!" - state.remember self - end - yield - ensure - state and state.forget self - end - - def json_shift(state, depth) - state and not state.object_nl.empty? or return '' - state.indent * depth - end - - def json_transform(state, depth) - delim = ',' - delim << state.object_nl if state - result = '{' - result << state.object_nl if state - result << map { |key,value| - s = json_shift(state, depth + 1) - s << key.to_s.to_json(state, depth + 1) - s << state.space_before if state - s << ':' - s << state.space if state - s << value.to_json(state, depth + 1) - }.join(delim) - result << state.object_nl if state - result << json_shift(state, depth) - result << '}' - result - end - end - - module Array - # Returns a JSON string containing a JSON array, that is unparsed from - # this Array instance. - # _state_ is a JSON::State object, that can also be used to configure the - # produced JSON string output further. - # _depth_ is used to find out nesting depth, to indent accordingly. - def to_json(state = nil, depth = 0, *) - if state - state = JSON.state.from_state(state) - state.check_max_nesting(depth) - json_check_circular(state) { json_transform(state, depth) } - else - json_transform(state, depth) - end - end - - private - - def json_check_circular(state) - if state and state.check_circular? - state.seen?(self) and raise JSON::CircularDatastructure, - "circular data structures not supported!" - state.remember self - end - yield - ensure - state and state.forget self - end - - def json_shift(state, depth) - state and not state.array_nl.empty? or return '' - state.indent * depth - end - - def json_transform(state, depth) - delim = ',' - delim << state.array_nl if state - result = '[' - result << state.array_nl if state - result << map { |value| - json_shift(state, depth + 1) << value.to_json(state, depth + 1) - }.join(delim) - result << state.array_nl if state - result << json_shift(state, depth) - result << ']' - result - end - end - - module Integer - # Returns a JSON string representation for this Integer number. - def to_json(*) to_s end - end - - module Float - # Returns a JSON string representation for this Float number. - def to_json(state = nil, *) - case - when infinite? - if !state || state.allow_nan? - to_s - else - raise GeneratorError, "#{self} not allowed in JSON" - end - when nan? - if !state || state.allow_nan? - to_s - else - raise GeneratorError, "#{self} not allowed in JSON" - end - else - to_s - end - end - end - - module String - # This string should be encoded with UTF-8 A call to this method - # returns a JSON string encoded with UTF16 big endian characters as - # \u????. - def to_json(*) - '"' << JSON.utf8_to_json(self) << '"' - end - - # Module that holds the extinding methods if, the String module is - # included. - module Extend - # Raw Strings are JSON Objects (the raw bytes are stored in an array for the - # key "raw"). The Ruby String can be created by this module method. - def json_create(o) - o['raw'].pack('C*') - end - end - - # Extends _modul_ with the String::Extend module. - def self.included(modul) - modul.extend Extend - end - - # This method creates a raw object hash, that can be nested into - # other data structures and will be unparsed as a raw string. This - # method should be used, if you want to convert raw strings to JSON - # instead of UTF-8 strings, e. g. binary data. - def to_json_raw_object - { - JSON.create_id => self.class.name, - 'raw' => self.unpack('C*'), - } - end - - # This method creates a JSON text from the result of - # a call to to_json_raw_object of this String. - def to_json_raw(*args) - to_json_raw_object.to_json(*args) - end - end - - module TrueClass - # Returns a JSON string for true: 'true'. - def to_json(*) 'true' end - end - - module FalseClass - # Returns a JSON string for false: 'false'. - def to_json(*) 'false' end - end - - module NilClass - # Returns a JSON string for nil: 'null'. - def to_json(*) 'null' end - end - end - end - end -end diff --git a/ext/json/lib/json/pure/parser.rb b/ext/json/lib/json/pure/parser.rb deleted file mode 100644 index 9c3fea91da..0000000000 --- a/ext/json/lib/json/pure/parser.rb +++ /dev/null @@ -1,269 +0,0 @@ -require 'strscan' - -module JSON - module Pure - # This class implements the JSON parser that is used to parse a JSON string - # into a Ruby data structure. - class Parser < StringScanner - STRING = /" ((?:[^\x0-\x1f"\\] | - \\["\\\/bfnrt] | - \\u[0-9a-fA-F]{4} | - \\[\x20-\xff])*) - "/nx - INTEGER = /(-?0|-?[1-9]\d*)/ - FLOAT = /(-? - (?:0|[1-9]\d*) - (?: - \.\d+(?i:e[+-]?\d+) | - \.\d+ | - (?i:e[+-]?\d+) - ) - )/x - NAN = /NaN/ - INFINITY = /Infinity/ - MINUS_INFINITY = /-Infinity/ - OBJECT_OPEN = /\{/ - OBJECT_CLOSE = /\}/ - ARRAY_OPEN = /\[/ - ARRAY_CLOSE = /\]/ - PAIR_DELIMITER = /:/ - COLLECTION_DELIMITER = /,/ - TRUE = /true/ - FALSE = /false/ - NULL = /null/ - IGNORE = %r( - (?: - //[^\n\r]*[\n\r]| # line comments - /\* # c-style comments - (?: - [^*/]| # normal chars - /[^*]| # slashes that do not start a nested comment - \*[^/]| # asterisks that do not end this comment - /(?=\*/) # single slash before this comment's end - )* - \*/ # the End of this comment - |[ \t\r\n]+ # whitespaces: space, horicontal tab, lf, cr - )+ - )mx - - UNPARSED = Object.new - - # Creates a new JSON::Pure::Parser instance for the string _source_. - # - # It will be configured by the _opts_ hash. _opts_ can have the following - # keys: - # * *max_nesting*: The maximum depth of nesting allowed in the parsed data - # structures. Disable depth checking with :max_nesting => false|nil|0, - # it defaults to 19. - # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in - # defiance of RFC 4627 to be parsed by the Parser. This option defaults - # to false. - # * *create_additions*: If set to false, the Parser doesn't create - # additions even if a matchin class and create_id was found. This option - # defaults to true. - def initialize(source, opts = {}) - super - if !opts.key?(:max_nesting) # defaults to 19 - @max_nesting = 19 - elsif opts[:max_nesting] - @max_nesting = opts[:max_nesting] - else - @max_nesting = 0 - end - @allow_nan = !!opts[:allow_nan] - ca = true - ca = opts[:create_additions] if opts.key?(:create_additions) - @create_id = ca ? JSON.create_id : nil - end - - alias source string - - # Parses the current JSON string _source_ and returns the complete data - # structure as a result. - def parse - reset - obj = nil - until eos? - case - when scan(OBJECT_OPEN) - obj and raise ParserError, "source '#{peek(20)}' not in JSON!" - @current_nesting = 1 - obj = parse_object - when scan(ARRAY_OPEN) - obj and raise ParserError, "source '#{peek(20)}' not in JSON!" - @current_nesting = 1 - obj = parse_array - when skip(IGNORE) - ; - else - raise ParserError, "source '#{peek(20)}' not in JSON!" - end - end - obj or raise ParserError, "source did not contain any JSON!" - obj - end - - private - - # Unescape characters in strings. - UNESCAPE_MAP = Hash.new { |h, k| h[k] = k.chr } - UNESCAPE_MAP.update({ - ?" => '"', - ?\\ => '\\', - ?/ => '/', - ?b => "\b", - ?f => "\f", - ?n => "\n", - ?r => "\r", - ?t => "\t", - ?u => nil, - }) - - def parse_string - if scan(STRING) - return '' if self[1].empty? - self[1].gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do |c| - if u = UNESCAPE_MAP[$&[1]] - u - else # \uXXXX - res = [] - stack = nil - [c.delete!('\\\\u')].pack("H*").unpack("n*").each do |c| - case c - when 0xD800..0xDBFF - raise JSON::ParserError, "partial character in source" if stack - stack = c - when 0xDC00..0xDFFF - raise JSON::ParserError, - "partial character in source" unless (0xD800..0xDBFF).include?(stack) - res << (stack << 10) - 0x35fdc00 + c - stack = nil - else - raise JSON::ParserError, "partial character in source" if stack - res << c - end - end - raise JSON::ParserError, "partial character in source" if stack - res.pack("U*") - end - end.force_encoding("UTF-8") - else - UNPARSED - end - end - - def parse_value - case - when scan(FLOAT) - Float(self[1]) - when scan(INTEGER) - Integer(self[1]) - when scan(TRUE) - true - when scan(FALSE) - false - when scan(NULL) - nil - when (string = parse_string) != UNPARSED - string - when scan(ARRAY_OPEN) - @current_nesting += 1 - ary = parse_array - @current_nesting -= 1 - ary - when scan(OBJECT_OPEN) - @current_nesting += 1 - obj = parse_object - @current_nesting -= 1 - obj - when @allow_nan && scan(NAN) - NaN - when @allow_nan && scan(INFINITY) - Infinity - when @allow_nan && scan(MINUS_INFINITY) - MinusInfinity - else - UNPARSED - end - end - - def parse_array - raise NestingError, "nesting of #@current_nesting is to deep" if - @max_nesting.nonzero? && @current_nesting > @max_nesting - result = [] - delim = false - until eos? - case - when (value = parse_value) != UNPARSED - delim = false - result << value - skip(IGNORE) - if scan(COLLECTION_DELIMITER) - delim = true - elsif match?(ARRAY_CLOSE) - ; - else - raise ParserError, "expected ',' or ']' in array at '#{peek(20)}'!" - end - when scan(ARRAY_CLOSE) - if delim - raise ParserError, "expected next element in array at '#{peek(20)}'!" - end - break - when skip(IGNORE) - ; - else - raise ParserError, "unexpected token in array at '#{peek(20)}'!" - end - end - result - end - - def parse_object - raise NestingError, "nesting of #@current_nesting is to deep" if - @max_nesting.nonzero? && @current_nesting > @max_nesting - result = {} - delim = false - until eos? - case - when (string = parse_string) != UNPARSED - skip(IGNORE) - unless scan(PAIR_DELIMITER) - raise ParserError, "expected ':' in object at '#{peek(20)}'!" - end - skip(IGNORE) - unless (value = parse_value).equal? UNPARSED - result[string] = value - delim = false - skip(IGNORE) - if scan(COLLECTION_DELIMITER) - delim = true - elsif match?(OBJECT_CLOSE) - ; - else - raise ParserError, "expected ',' or '}' in object at '#{peek(20)}'!" - end - else - raise ParserError, "expected value in object at '#{peek(20)}'!" - end - when scan(OBJECT_CLOSE) - if delim - raise ParserError, "expected next name, value pair in object at '#{peek(20)}'!" - end - if @create_id and klassname = result[@create_id] - klass = JSON.deep_const_get klassname - break unless klass and klass.json_creatable? - result = klass.json_create(result) - end - break - when skip(IGNORE) - ; - else - raise ParserError, "unexpected token in object at '#{peek(20)}'!" - end - end - result - end - end - end -end diff --git a/ext/json/lib/json/version.rb b/ext/json/lib/json/version.rb index acf8217048..30c0a71d2f 100644 --- a/ext/json/lib/json/version.rb +++ b/ext/json/lib/json/version.rb @@ -1,9 +1,5 @@ +# frozen_string_literal: true + module JSON - # JSON version - VERSION = '1.1.3' - VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc: - VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc: - VERSION_MINOR = VERSION_ARRAY[1] # :nodoc: - VERSION_BUILD = VERSION_ARRAY[2] # :nodoc: - VARIANT_BINARY = false + VERSION = '2.19.8' end |
