json 1.7.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -0
  3. data/.travis.yml +18 -10
  4. data/{CHANGES → CHANGES.md} +217 -69
  5. data/Gemfile +11 -12
  6. data/{COPYING-json-jruby → LICENSE} +5 -6
  7. data/{README-json-jruby.markdown → README-json-jruby.md} +0 -0
  8. data/{README.rdoc → README.md} +188 -137
  9. data/Rakefile +52 -37
  10. data/VERSION +1 -1
  11. data/ext/json/ext/fbuffer/fbuffer.h +38 -7
  12. data/ext/json/ext/generator/depend +1 -0
  13. data/ext/json/ext/generator/extconf.rb +1 -10
  14. data/ext/json/ext/generator/generator.c +239 -133
  15. data/ext/json/ext/generator/generator.h +35 -26
  16. data/ext/json/ext/parser/depend +1 -0
  17. data/ext/json/ext/parser/extconf.rb +2 -9
  18. data/ext/json/ext/parser/parser.c +446 -514
  19. data/ext/json/ext/parser/parser.h +23 -9
  20. data/ext/json/ext/parser/parser.rl +177 -208
  21. data/ext/json/extconf.rb +2 -0
  22. data/java/src/json/ext/ByteListTranscoder.java +1 -2
  23. data/java/src/json/ext/Generator.java +49 -20
  24. data/java/src/json/ext/GeneratorMethods.java +1 -2
  25. data/java/src/json/ext/GeneratorService.java +1 -2
  26. data/java/src/json/ext/GeneratorState.java +25 -57
  27. data/java/src/json/ext/OptionsReader.java +5 -5
  28. data/java/src/json/ext/Parser.java +141 -419
  29. data/java/src/json/ext/Parser.rl +57 -128
  30. data/java/src/json/ext/ParserService.java +1 -2
  31. data/java/src/json/ext/RuntimeInfo.java +1 -6
  32. data/java/src/json/ext/StringDecoder.java +1 -2
  33. data/java/src/json/ext/StringEncoder.java +5 -0
  34. data/java/src/json/ext/Utils.java +1 -2
  35. data/json-java.gemspec +17 -2
  36. data/json.gemspec +0 -0
  37. data/json_pure.gemspec +25 -26
  38. data/lib/json.rb +3 -2
  39. data/lib/json/add/bigdecimal.rb +10 -2
  40. data/lib/json/add/complex.rb +9 -2
  41. data/lib/json/add/core.rb +1 -0
  42. data/lib/json/add/date.rb +1 -1
  43. data/lib/json/add/date_time.rb +1 -1
  44. data/lib/json/add/exception.rb +1 -1
  45. data/lib/json/add/ostruct.rb +3 -3
  46. data/lib/json/add/range.rb +1 -1
  47. data/lib/json/add/rational.rb +8 -2
  48. data/lib/json/add/regexp.rb +3 -3
  49. data/lib/json/add/set.rb +29 -0
  50. data/lib/json/add/struct.rb +1 -1
  51. data/lib/json/add/symbol.rb +1 -1
  52. data/lib/json/add/time.rb +6 -3
  53. data/lib/json/common.rb +51 -66
  54. data/lib/json/ext.rb +0 -6
  55. data/lib/json/generic_object.rb +36 -4
  56. data/lib/json/pure.rb +2 -8
  57. data/lib/json/pure/generator.rb +106 -119
  58. data/lib/json/pure/parser.rb +48 -88
  59. data/lib/json/version.rb +2 -1
  60. data/references/rfc7159.txt +899 -0
  61. data/tests/fixtures/fail18.json +1 -1
  62. data/tests/fixtures/obsolete_fail1.json +1 -0
  63. data/tests/{test_json_addition.rb → json_addition_test.rb} +53 -38
  64. data/tests/json_common_interface_test.rb +126 -0
  65. data/tests/json_encoding_test.rb +107 -0
  66. data/tests/json_ext_parser_test.rb +15 -0
  67. data/tests/{test_json_fixtures.rb → json_fixtures_test.rb} +5 -8
  68. data/tests/json_generator_test.rb +421 -0
  69. data/tests/json_generic_object_test.rb +82 -0
  70. data/tests/json_parser_test.rb +472 -0
  71. data/tests/json_string_matching_test.rb +38 -0
  72. data/tests/{setup_variant.rb → test_helper.rb} +6 -0
  73. data/tools/diff.sh +18 -0
  74. data/tools/fuzz.rb +1 -9
  75. metadata +49 -72
  76. data/COPYING +0 -58
  77. data/GPL +0 -340
  78. data/TODO +0 -1
  79. data/data/example.json +0 -1
  80. data/data/index.html +0 -38
  81. data/data/prototype.js +0 -4184
  82. data/tests/fixtures/fail1.json +0 -1
  83. data/tests/test_json.rb +0 -539
  84. data/tests/test_json_encoding.rb +0 -65
  85. data/tests/test_json_generate.rb +0 -251
  86. data/tests/test_json_generic_object.rb +0 -35
  87. data/tests/test_json_string_matching.rb +0 -40
  88. data/tests/test_json_unicode.rb +0 -72
data/json.gemspec CHANGED
Binary file
data/json_pure.gemspec CHANGED
@@ -1,39 +1,38 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ # stub: json_pure 2.3.0 ruby lib
2
3
 
3
4
  Gem::Specification.new do |s|
4
- s.name = "json_pure"
5
- s.version = "1.7.0"
5
+ s.name = "json_pure".freeze
6
+ s.version = "2.3.0"
6
7
 
7
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Florian Frank"]
9
- s.date = "2012-04-28"
10
- s.description = "This is a JSON implementation in pure Ruby."
11
- s.email = "flori@ping.de"
12
- s.extra_rdoc_files = ["README.rdoc"]
13
- s.files = [".gitignore", ".travis.yml", "CHANGES", "COPYING", "COPYING-json-jruby", "GPL", "Gemfile", "README-json-jruby.markdown", "README.rdoc", "Rakefile", "TODO", "VERSION", "data/example.json", "data/index.html", "data/prototype.js", "diagrams/.keep", "ext/json/ext/fbuffer/fbuffer.h", "ext/json/ext/generator/extconf.rb", "ext/json/ext/generator/generator.c", "ext/json/ext/generator/generator.h", "ext/json/ext/parser/extconf.rb", "ext/json/ext/parser/parser.c", "ext/json/ext/parser/parser.h", "ext/json/ext/parser/parser.rl", "install.rb", "java/src/json/ext/ByteListTranscoder.java", "java/src/json/ext/Generator.java", "java/src/json/ext/GeneratorMethods.java", "java/src/json/ext/GeneratorService.java", "java/src/json/ext/GeneratorState.java", "java/src/json/ext/OptionsReader.java", "java/src/json/ext/Parser.java", "java/src/json/ext/Parser.rl", "java/src/json/ext/ParserService.java", "java/src/json/ext/RuntimeInfo.java", "java/src/json/ext/StringDecoder.java", "java/src/json/ext/StringEncoder.java", "java/src/json/ext/Utils.java", "json-java.gemspec", "json.gemspec", "json_pure.gemspec", "lib/json.rb", "lib/json/add/bigdecimal.rb", "lib/json/add/complex.rb", "lib/json/add/core.rb", "lib/json/add/date.rb", "lib/json/add/date_time.rb", "lib/json/add/exception.rb", "lib/json/add/ostruct.rb", "lib/json/add/range.rb", "lib/json/add/rational.rb", "lib/json/add/regexp.rb", "lib/json/add/struct.rb", "lib/json/add/symbol.rb", "lib/json/add/time.rb", "lib/json/common.rb", "lib/json/ext.rb", "lib/json/ext/.keep", "lib/json/generic_object.rb", "lib/json/pure.rb", "lib/json/pure/generator.rb", "lib/json/pure/parser.rb", "lib/json/version.rb", "tests/fixtures/fail1.json", "tests/fixtures/fail10.json", "tests/fixtures/fail11.json", "tests/fixtures/fail12.json", "tests/fixtures/fail13.json", "tests/fixtures/fail14.json", "tests/fixtures/fail18.json", "tests/fixtures/fail19.json", "tests/fixtures/fail2.json", "tests/fixtures/fail20.json", "tests/fixtures/fail21.json", "tests/fixtures/fail22.json", "tests/fixtures/fail23.json", "tests/fixtures/fail24.json", "tests/fixtures/fail25.json", "tests/fixtures/fail27.json", "tests/fixtures/fail28.json", "tests/fixtures/fail3.json", "tests/fixtures/fail4.json", "tests/fixtures/fail5.json", "tests/fixtures/fail6.json", "tests/fixtures/fail7.json", "tests/fixtures/fail8.json", "tests/fixtures/fail9.json", "tests/fixtures/pass1.json", "tests/fixtures/pass15.json", "tests/fixtures/pass16.json", "tests/fixtures/pass17.json", "tests/fixtures/pass2.json", "tests/fixtures/pass26.json", "tests/fixtures/pass3.json", "tests/setup_variant.rb", "tests/test_json.rb", "tests/test_json_addition.rb", "tests/test_json_encoding.rb", "tests/test_json_fixtures.rb", "tests/test_json_generate.rb", "tests/test_json_generic_object.rb", "tests/test_json_string_matching.rb", "tests/test_json_unicode.rb", "tools/fuzz.rb", "tools/server.rb", "./tests/test_json_string_matching.rb", "./tests/test_json_fixtures.rb", "./tests/test_json_unicode.rb", "./tests/test_json_addition.rb", "./tests/test_json_generate.rb", "./tests/test_json_encoding.rb", "./tests/test_json_generic_object.rb", "./tests/test_json.rb"]
14
- s.homepage = "http://flori.github.com/json"
15
- s.rdoc_options = ["--title", "JSON implemention for ruby", "--main", "README.rdoc"]
16
- s.require_paths = ["lib"]
17
- s.rubyforge_project = "json"
18
- s.rubygems_version = "1.8.23"
19
- s.summary = "JSON Implementation for Ruby"
20
- s.test_files = ["./tests/test_json_string_matching.rb", "./tests/test_json_fixtures.rb", "./tests/test_json_unicode.rb", "./tests/test_json_addition.rb", "./tests/test_json_generate.rb", "./tests/test_json_encoding.rb", "./tests/test_json_generic_object.rb", "./tests/test_json.rb"]
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
+ s.require_paths = ["lib".freeze]
10
+ s.authors = ["Florian Frank".freeze]
11
+ s.date = "2019-12-11"
12
+ s.description = "This is a JSON implementation in pure Ruby.".freeze
13
+ s.email = "flori@ping.de".freeze
14
+ s.extra_rdoc_files = ["README.md".freeze]
15
+ s.files = ["./tests/test_helper.rb".freeze, ".gitignore".freeze, ".travis.yml".freeze, "CHANGES.md".freeze, "Gemfile".freeze, "LICENSE".freeze, "README-json-jruby.md".freeze, "README.md".freeze, "Rakefile".freeze, "VERSION".freeze, "diagrams/.keep".freeze, "ext/json/ext/fbuffer/fbuffer.h".freeze, "ext/json/ext/generator/depend".freeze, "ext/json/ext/generator/extconf.rb".freeze, "ext/json/ext/generator/generator.c".freeze, "ext/json/ext/generator/generator.h".freeze, "ext/json/ext/parser/depend".freeze, "ext/json/ext/parser/extconf.rb".freeze, "ext/json/ext/parser/parser.c".freeze, "ext/json/ext/parser/parser.h".freeze, "ext/json/ext/parser/parser.rl".freeze, "ext/json/extconf.rb".freeze, "install.rb".freeze, "java/src/json/ext/ByteListTranscoder.java".freeze, "java/src/json/ext/Generator.java".freeze, "java/src/json/ext/GeneratorMethods.java".freeze, "java/src/json/ext/GeneratorService.java".freeze, "java/src/json/ext/GeneratorState.java".freeze, "java/src/json/ext/OptionsReader.java".freeze, "java/src/json/ext/Parser.java".freeze, "java/src/json/ext/Parser.rl".freeze, "java/src/json/ext/ParserService.java".freeze, "java/src/json/ext/RuntimeInfo.java".freeze, "java/src/json/ext/StringDecoder.java".freeze, "java/src/json/ext/StringEncoder.java".freeze, "java/src/json/ext/Utils.java".freeze, "json-java.gemspec".freeze, "json.gemspec".freeze, "json_pure.gemspec".freeze, "lib/json.rb".freeze, "lib/json/add/bigdecimal.rb".freeze, "lib/json/add/complex.rb".freeze, "lib/json/add/core.rb".freeze, "lib/json/add/date.rb".freeze, "lib/json/add/date_time.rb".freeze, "lib/json/add/exception.rb".freeze, "lib/json/add/ostruct.rb".freeze, "lib/json/add/range.rb".freeze, "lib/json/add/rational.rb".freeze, "lib/json/add/regexp.rb".freeze, "lib/json/add/set.rb".freeze, "lib/json/add/struct.rb".freeze, "lib/json/add/symbol.rb".freeze, "lib/json/add/time.rb".freeze, "lib/json/common.rb".freeze, "lib/json/ext.rb".freeze, "lib/json/ext/.keep".freeze, "lib/json/generic_object.rb".freeze, "lib/json/pure.rb".freeze, "lib/json/pure/generator.rb".freeze, "lib/json/pure/parser.rb".freeze, "lib/json/version.rb".freeze, "references/rfc7159.txt".freeze, "tests/fixtures/fail10.json".freeze, "tests/fixtures/fail11.json".freeze, "tests/fixtures/fail12.json".freeze, "tests/fixtures/fail13.json".freeze, "tests/fixtures/fail14.json".freeze, "tests/fixtures/fail18.json".freeze, "tests/fixtures/fail19.json".freeze, "tests/fixtures/fail2.json".freeze, "tests/fixtures/fail20.json".freeze, "tests/fixtures/fail21.json".freeze, "tests/fixtures/fail22.json".freeze, "tests/fixtures/fail23.json".freeze, "tests/fixtures/fail24.json".freeze, "tests/fixtures/fail25.json".freeze, "tests/fixtures/fail27.json".freeze, "tests/fixtures/fail28.json".freeze, "tests/fixtures/fail3.json".freeze, "tests/fixtures/fail4.json".freeze, "tests/fixtures/fail5.json".freeze, "tests/fixtures/fail6.json".freeze, "tests/fixtures/fail7.json".freeze, "tests/fixtures/fail8.json".freeze, "tests/fixtures/fail9.json".freeze, "tests/fixtures/obsolete_fail1.json".freeze, "tests/fixtures/pass1.json".freeze, "tests/fixtures/pass15.json".freeze, "tests/fixtures/pass16.json".freeze, "tests/fixtures/pass17.json".freeze, "tests/fixtures/pass2.json".freeze, "tests/fixtures/pass26.json".freeze, "tests/fixtures/pass3.json".freeze, "tests/json_addition_test.rb".freeze, "tests/json_common_interface_test.rb".freeze, "tests/json_encoding_test.rb".freeze, "tests/json_ext_parser_test.rb".freeze, "tests/json_fixtures_test.rb".freeze, "tests/json_generator_test.rb".freeze, "tests/json_generic_object_test.rb".freeze, "tests/json_parser_test.rb".freeze, "tests/json_string_matching_test.rb".freeze, "tests/test_helper.rb".freeze, "tools/diff.sh".freeze, "tools/fuzz.rb".freeze, "tools/server.rb".freeze]
16
+ s.homepage = "http://flori.github.com/json".freeze
17
+ s.licenses = ["Ruby".freeze]
18
+ s.rdoc_options = ["--title".freeze, "JSON implemention for ruby".freeze, "--main".freeze, "README.md".freeze]
19
+ s.required_ruby_version = Gem::Requirement.new(">= 1.9".freeze)
20
+ s.rubygems_version = "3.0.3".freeze
21
+ s.summary = "JSON Implementation for Ruby".freeze
22
+ s.test_files = ["./tests/test_helper.rb".freeze]
21
23
 
22
24
  if s.respond_to? :specification_version then
23
- s.specification_version = 3
25
+ s.specification_version = 4
24
26
 
25
27
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
- s.add_development_dependency(%q<permutation>, [">= 0"])
27
- s.add_development_dependency(%q<sdoc>, [">= 0"])
28
- s.add_development_dependency(%q<rake>, ["~> 0.9.2"])
28
+ s.add_development_dependency(%q<rake>.freeze, [">= 0"])
29
+ s.add_development_dependency(%q<test-unit>.freeze, ["~> 2.0"])
29
30
  else
30
- s.add_dependency(%q<permutation>, [">= 0"])
31
- s.add_dependency(%q<sdoc>, [">= 0"])
32
- s.add_dependency(%q<rake>, ["~> 0.9.2"])
31
+ s.add_dependency(%q<rake>.freeze, [">= 0"])
32
+ s.add_dependency(%q<test-unit>.freeze, ["~> 2.0"])
33
33
  end
34
34
  else
35
- s.add_dependency(%q<permutation>, [">= 0"])
36
- s.add_dependency(%q<sdoc>, [">= 0"])
37
- s.add_dependency(%q<rake>, ["~> 0.9.2"])
35
+ s.add_dependency(%q<rake>.freeze, [">= 0"])
36
+ s.add_dependency(%q<test-unit>.freeze, ["~> 2.0"])
38
37
  end
39
38
  end
data/lib/json.rb CHANGED
@@ -1,3 +1,6 @@
1
+ #frozen_string_literal: false
2
+ require 'json/common'
3
+
1
4
  ##
2
5
  # = JavaScript Object Notation (JSON)
3
6
  #
@@ -49,8 +52,6 @@
49
52
  #
50
53
  # 1.to_json => "1"
51
54
  #
52
-
53
- require 'json/common'
54
55
  module JSON
55
56
  require 'json/version'
56
57
 
@@ -1,13 +1,20 @@
1
+ #frozen_string_literal: false
1
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
3
  require 'json'
3
4
  end
4
5
  defined?(::BigDecimal) or require 'bigdecimal'
5
6
 
6
7
  class BigDecimal
8
+ # Import a JSON Marshalled object.
9
+ #
10
+ # method used for JSON marshalling support.
7
11
  def self.json_create(object)
8
12
  BigDecimal._load object['b']
9
13
  end
10
14
 
15
+ # Marshal the object to JSON.
16
+ #
17
+ # method used for JSON marshalling support.
11
18
  def as_json(*)
12
19
  {
13
20
  JSON.create_id => self.class.name,
@@ -15,7 +22,8 @@ class BigDecimal
15
22
  }
16
23
  end
17
24
 
18
- def to_json(*)
19
- as_json.to_json
25
+ # return the JSON value
26
+ def to_json(*args)
27
+ as_json.to_json(*args)
20
28
  end
21
29
  end
@@ -1,13 +1,19 @@
1
+ #frozen_string_literal: false
1
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
3
  require 'json'
3
4
  end
4
5
  defined?(::Complex) or require 'complex'
5
6
 
6
7
  class Complex
8
+
9
+ # Deserializes JSON string by converting Real value <tt>r</tt>, imaginary
10
+ # value <tt>i</tt>, to a Complex object.
7
11
  def self.json_create(object)
8
12
  Complex(object['r'], object['i'])
9
13
  end
10
14
 
15
+ # Returns a hash, that will be turned into a JSON object and represent this
16
+ # object.
11
17
  def as_json(*)
12
18
  {
13
19
  JSON.create_id => self.class.name,
@@ -16,7 +22,8 @@ class Complex
16
22
  }
17
23
  end
18
24
 
19
- def to_json(*)
20
- as_json.to_json
25
+ # Stores class name (Complex) along with real value <tt>r</tt> and imaginary value <tt>i</tt> as JSON string
26
+ def to_json(*args)
27
+ as_json.to_json(*args)
21
28
  end
22
29
  end
data/lib/json/add/core.rb CHANGED
@@ -1,3 +1,4 @@
1
+ #frozen_string_literal: false
1
2
  # This file requires the implementations of ruby core's custom objects for
2
3
  # serialisation/deserialisation.
3
4
 
data/lib/json/add/date.rb CHANGED
@@ -1,9 +1,9 @@
1
+ #frozen_string_literal: false
1
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
3
  require 'json'
3
4
  end
4
5
  require 'date'
5
6
 
6
- # Date serialization/deserialization
7
7
  class Date
8
8
 
9
9
  # Deserializes JSON string by converting Julian year <tt>y</tt>, month
@@ -1,9 +1,9 @@
1
+ #frozen_string_literal: false
1
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
3
  require 'json'
3
4
  end
4
5
  require 'date'
5
6
 
6
- # DateTime serialization/deserialization
7
7
  class DateTime
8
8
 
9
9
  # Deserializes JSON string by converting year <tt>y</tt>, month <tt>m</tt>,
@@ -1,8 +1,8 @@
1
+ #frozen_string_literal: false
1
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
3
  require 'json'
3
4
  end
4
5
 
5
- # Exception serialization/deserialization
6
6
  class Exception
7
7
 
8
8
  # Deserializes JSON string by constructing new Exception object with message
@@ -1,13 +1,13 @@
1
+ #frozen_string_literal: false
1
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
3
  require 'json'
3
4
  end
4
5
  require 'ostruct'
5
6
 
6
- # OpenStruct serialization/deserialization
7
7
  class OpenStruct
8
8
 
9
9
  # Deserializes JSON string by constructing new Struct object with values
10
- # <tt>v</tt> serialized by <tt>to_json</tt>.
10
+ # <tt>t</tt> serialized by <tt>to_json</tt>.
11
11
  def self.json_create(object)
12
12
  new(object['t'] || object[:t])
13
13
  end
@@ -23,7 +23,7 @@ class OpenStruct
23
23
  }
24
24
  end
25
25
 
26
- # Stores class name (OpenStruct) with this struct's values <tt>v</tt> as a
26
+ # Stores class name (OpenStruct) with this struct's values <tt>t</tt> as a
27
27
  # JSON string.
28
28
  def to_json(*args)
29
29
  as_json.to_json(*args)
@@ -1,8 +1,8 @@
1
+ #frozen_string_literal: false
1
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
3
  require 'json'
3
4
  end
4
5
 
5
- # Range serialization/deserialization
6
6
  class Range
7
7
 
8
8
  # Deserializes JSON string by constructing new Range object with arguments
@@ -1,13 +1,18 @@
1
+ #frozen_string_literal: false
1
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
3
  require 'json'
3
4
  end
4
5
  defined?(::Rational) or require 'rational'
5
6
 
6
7
  class Rational
8
+ # Deserializes JSON string by converting numerator value <tt>n</tt>,
9
+ # denominator value <tt>d</tt>, to a Rational object.
7
10
  def self.json_create(object)
8
11
  Rational(object['n'], object['d'])
9
12
  end
10
13
 
14
+ # Returns a hash, that will be turned into a JSON object and represent this
15
+ # object.
11
16
  def as_json(*)
12
17
  {
13
18
  JSON.create_id => self.class.name,
@@ -16,7 +21,8 @@ class Rational
16
21
  }
17
22
  end
18
23
 
19
- def to_json(*)
20
- as_json.to_json
24
+ # Stores class name (Rational) along with numerator value <tt>n</tt> and denominator value <tt>d</tt> as JSON string
25
+ def to_json(*args)
26
+ as_json.to_json(*args)
21
27
  end
22
28
  end
@@ -1,8 +1,8 @@
1
+ #frozen_string_literal: false
1
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
3
  require 'json'
3
4
  end
4
5
 
5
- # Regexp serialization/deserialization
6
6
  class Regexp
7
7
 
8
8
  # Deserializes JSON string by constructing new Regexp object with source
@@ -24,7 +24,7 @@ class Regexp
24
24
 
25
25
  # Stores class name (Regexp) with options <tt>o</tt> and source <tt>s</tt>
26
26
  # (Regexp or String) as JSON string
27
- def to_json(*)
28
- as_json.to_json
27
+ def to_json(*args)
28
+ as_json.to_json(*args)
29
29
  end
30
30
  end
@@ -0,0 +1,29 @@
1
+ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
+ require 'json'
3
+ end
4
+ defined?(::Set) or require 'set'
5
+
6
+ class Set
7
+ # Import a JSON Marshalled object.
8
+ #
9
+ # method used for JSON marshalling support.
10
+ def self.json_create(object)
11
+ new object['a']
12
+ end
13
+
14
+ # Marshal the object to JSON.
15
+ #
16
+ # method used for JSON marshalling support.
17
+ def as_json(*)
18
+ {
19
+ JSON.create_id => self.class.name,
20
+ 'a' => to_a,
21
+ }
22
+ end
23
+
24
+ # return the JSON value
25
+ def to_json(*args)
26
+ as_json.to_json(*args)
27
+ end
28
+ end
29
+
@@ -1,8 +1,8 @@
1
+ #frozen_string_literal: false
1
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
3
  require 'json'
3
4
  end
4
5
 
5
- # Struct serialization/deserialization
6
6
  class Struct
7
7
 
8
8
  # Deserializes JSON string by constructing new Struct object with values
@@ -1,8 +1,8 @@
1
+ #frozen_string_literal: false
1
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
3
  require 'json'
3
4
  end
4
5
 
5
- # Symbol serialization/deserialization
6
6
  class Symbol
7
7
  # Returns a hash, that will be turned into a JSON object and represent this
8
8
  # object.
data/lib/json/add/time.rb CHANGED
@@ -1,8 +1,8 @@
1
+ #frozen_string_literal: false
1
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
3
  require 'json'
3
4
  end
4
5
 
5
- # Time serialization/deserialization
6
6
  class Time
7
7
 
8
8
  # Deserializes JSON string by converting time since epoch to Time
@@ -10,7 +10,7 @@ class Time
10
10
  if usec = object.delete('u') # used to be tv_usec -> tv_nsec
11
11
  object['n'] = usec * 1000
12
12
  end
13
- if instance_methods.include?(:tv_nsec)
13
+ if method_defined?(:tv_nsec)
14
14
  at(object['s'], Rational(object['n'], 1000))
15
15
  else
16
16
  at(object['s'], object['n'] / 1000)
@@ -20,10 +20,13 @@ class Time
20
20
  # Returns a hash, that will be turned into a JSON object and represent this
21
21
  # object.
22
22
  def as_json(*)
23
+ nanoseconds = [ tv_usec * 1000 ]
24
+ respond_to?(:tv_nsec) and nanoseconds << tv_nsec
25
+ nanoseconds = nanoseconds.max
23
26
  {
24
27
  JSON.create_id => self.class.name,
25
28
  's' => tv_sec,
26
- 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
29
+ 'n' => nanoseconds,
27
30
  }
28
31
  end
29
32
 
data/lib/json/common.rb CHANGED
@@ -1,14 +1,15 @@
1
+ #frozen_string_literal: false
1
2
  require 'json/version'
2
3
  require 'json/generic_object'
3
4
 
4
5
  module JSON
5
6
  class << self
6
- # If _object_ is string-like, parse the string and return the parsed result
7
- # as a Ruby data structure. Otherwise generate a JSON text from the Ruby
8
- # data structure object and return it.
7
+ # If _object_ is string-like, parse the string and return the parsed
8
+ # result as a Ruby data structure. Otherwise generate a JSON text from the
9
+ # Ruby data structure object and return it.
9
10
  #
10
- # The _opts_ argument is passed through to generate/parse respectively. See
11
- # generate and parse for their documentation.
11
+ # The _opts_ argument is passed through to generate/parse respectively.
12
+ # See generate and parse for their documentation.
12
13
  def [](object, opts = {})
13
14
  if object.respond_to? :to_str
14
15
  JSON.parse(object.to_str, opts)
@@ -24,7 +25,7 @@ module JSON
24
25
  # Set the JSON parser class _parser_ to be used by JSON.
25
26
  def parser=(parser) # :nodoc:
26
27
  @parser = parser
27
- remove_const :Parser if JSON.const_defined_in?(self, :Parser)
28
+ remove_const :Parser if const_defined?(:Parser, false)
28
29
  const_set :Parser, parser
29
30
  end
30
31
 
@@ -35,8 +36,8 @@ module JSON
35
36
  def deep_const_get(path) # :nodoc:
36
37
  path.to_s.split(/::/).inject(Object) do |p, c|
37
38
  case
38
- when c.empty? then p
39
- when JSON.const_defined_in?(p, c) then p.const_get(c)
39
+ when c.empty? then p
40
+ when p.const_defined?(c, true) then p.const_get(c)
40
41
  else
41
42
  begin
42
43
  p.const_missing(c)
@@ -103,7 +104,13 @@ module JSON
103
104
  MinusInfinity = -Infinity
104
105
 
105
106
  # The base exception for JSON errors.
106
- class JSONError < StandardError; end
107
+ class JSONError < StandardError
108
+ def self.wrap(exception)
109
+ obj = new("Wrapped(#{exception.class}): #{exception.message.inspect}")
110
+ obj.set_backtrace exception.backtrace
111
+ obj
112
+ end
113
+ end
107
114
 
108
115
  # This exception is raised if a parser error occurs.
109
116
  class ParserError < JSONError; end
@@ -132,21 +139,21 @@ module JSON
132
139
  # _opts_ can have the following
133
140
  # keys:
134
141
  # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
135
- # structures. Disable depth checking with :max_nesting => false. It defaults
136
- # to 19.
142
+ # structures. Disable depth checking with :max_nesting => false. It
143
+ # defaults to 100.
137
144
  # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
138
- # defiance of RFC 4627 to be parsed by the Parser. This option defaults
145
+ # defiance of RFC 7159 to be parsed by the Parser. This option defaults
139
146
  # to false.
140
147
  # * *symbolize_names*: If set to true, returns symbols for the names
141
148
  # (keys) in a JSON object. Otherwise strings are returned. Strings are
142
149
  # the default.
143
150
  # * *create_additions*: If set to false, the Parser doesn't create
144
151
  # additions even if a matching class and create_id was found. This option
145
- # defaults to true.
152
+ # defaults to false.
146
153
  # * *object_class*: Defaults to Hash
147
154
  # * *array_class*: Defaults to Array
148
155
  def parse(source, opts = {})
149
- Parser.new(source, opts).parse
156
+ Parser.new(source, **(opts||{})).parse
150
157
  end
151
158
 
152
159
  # Parse the JSON document _source_ into a Ruby data structure and return it.
@@ -155,21 +162,21 @@ module JSON
155
162
  #
156
163
  # _opts_ can have the following keys:
157
164
  # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
158
- # structures. Enable depth checking with :max_nesting => anInteger. The parse!
159
- # methods defaults to not doing max depth checking: This can be dangerous
160
- # if someone wants to fill up your stack.
165
+ # structures. Enable depth checking with :max_nesting => anInteger. The
166
+ # parse! methods defaults to not doing max depth checking: This can be
167
+ # dangerous if someone wants to fill up your stack.
161
168
  # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
162
- # defiance of RFC 4627 to be parsed by the Parser. This option defaults
169
+ # defiance of RFC 7159 to be parsed by the Parser. This option defaults
163
170
  # to true.
164
171
  # * *create_additions*: If set to false, the Parser doesn't create
165
172
  # additions even if a matching class and create_id was found. This option
166
- # defaults to true.
173
+ # defaults to false.
167
174
  def parse!(source, opts = {})
168
175
  opts = {
169
176
  :max_nesting => false,
170
177
  :allow_nan => true
171
- }.update(opts)
172
- Parser.new(source, opts).parse
178
+ }.merge(opts)
179
+ Parser.new(source, **(opts||{})).parse
173
180
  end
174
181
 
175
182
  # Generate a JSON document from the Ruby data structure _obj_ and return
@@ -193,7 +200,7 @@ module JSON
193
200
  # encountered. This options defaults to false.
194
201
  # * *max_nesting*: The maximum depth of nesting allowed in the data
195
202
  # structures from which JSON is to be generated. Disable depth checking
196
- # with :max_nesting => false, it defaults to 19.
203
+ # with :max_nesting => false, it defaults to 100.
197
204
  #
198
205
  # See also the fast_generate for the fastest creation method with the least
199
206
  # amount of sanity checks, and the pretty_generate method for some
@@ -289,25 +296,32 @@ module JSON
289
296
  # The global default options for the JSON.load method:
290
297
  # :max_nesting: false
291
298
  # :allow_nan: true
292
- # :quirks_mode: true
299
+ # :allow_blank: true
293
300
  attr_accessor :load_default_options
294
301
  end
295
302
  self.load_default_options = {
296
- :max_nesting => false,
297
- :allow_nan => true,
298
- :quirks_mode => true,
303
+ :max_nesting => false,
304
+ :allow_nan => true,
305
+ :allow_blank => true,
306
+ :create_additions => true,
299
307
  }
300
308
 
301
309
  # Load a ruby data structure from a JSON _source_ and return it. A source can
302
310
  # either be a string-like object, an IO-like object, or an object responding
303
311
  # to the read method. If _proc_ was given, it will be called with any nested
304
- # Ruby object as an argument recursively in depth first order. The default
305
- # options for the parser can be changed via the load_default_options method.
312
+ # Ruby object as an argument recursively in depth first order. To modify the
313
+ # default options pass in the optional _options_ argument as well.
314
+ #
315
+ # BEWARE: This method is meant to serialise data from trusted user input,
316
+ # like from your own database server or clients under your control, it could
317
+ # be dangerous to allow untrusted users to pass JSON sources into it. The
318
+ # default options for the parser can be changed via the load_default_options
319
+ # method.
306
320
  #
307
321
  # This method is part of the implementation of the load/dump interface of
308
322
  # Marshal and YAML.
309
- def load(source, proc = nil)
310
- opts = load_default_options
323
+ def load(source, proc = nil, options = {})
324
+ opts = load_default_options.merge options
311
325
  if source.respond_to? :to_str
312
326
  source = source.to_str
313
327
  elsif source.respond_to? :to_io
@@ -315,7 +329,7 @@ module JSON
315
329
  elsif source.respond_to?(:read)
316
330
  source = source.read
317
331
  end
318
- if opts[:quirks_mode] && (source.nil? || source.empty?)
332
+ if opts[:allow_blank] && (source.nil? || source.empty?)
319
333
  source = 'null'
320
334
  end
321
335
  result = parse(source, opts)
@@ -344,13 +358,12 @@ module JSON
344
358
  # The global default options for the JSON.dump method:
345
359
  # :max_nesting: false
346
360
  # :allow_nan: true
347
- # :quirks_mode: true
361
+ # :allow_blank: true
348
362
  attr_accessor :dump_default_options
349
363
  end
350
364
  self.dump_default_options = {
351
365
  :max_nesting => false,
352
366
  :allow_nan => true,
353
- :quirks_mode => true,
354
367
  }
355
368
 
356
369
  # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
@@ -377,7 +390,7 @@ module JSON
377
390
  end
378
391
  end
379
392
  opts = JSON.dump_default_options
380
- limit and opts.update(:max_nesting => limit)
393
+ opts = opts.merge(:max_nesting => limit) if limit
381
394
  result = generate(obj, opts)
382
395
  if anIO
383
396
  anIO.write result
@@ -389,37 +402,9 @@ module JSON
389
402
  raise ArgumentError, "exceed depth limit"
390
403
  end
391
404
 
392
- # Swap consecutive bytes of _string_ in place.
393
- def self.swap!(string) # :nodoc:
394
- 0.upto(string.size / 2) do |i|
395
- break unless string[2 * i + 1]
396
- string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
397
- end
398
- string
399
- end
400
-
401
- # Shortuct for iconv.
402
- if ::String.method_defined?(:encode)
403
- # Encodes string using Ruby's _String.encode_
404
- def self.iconv(to, from, string)
405
- string.encode(to, from)
406
- end
407
- else
408
- require 'iconv'
409
- # Encodes string using _iconv_ library
410
- def self.iconv(to, from, string)
411
- Iconv.conv(to, from, string)
412
- end
413
- end
414
-
415
- if ::Object.method(:const_defined?).arity == 1
416
- def self.const_defined_in?(modul, constant)
417
- modul.const_defined?(constant)
418
- end
419
- else
420
- def self.const_defined_in?(modul, constant)
421
- modul.const_defined?(constant, false)
422
- end
405
+ # Encodes string using Ruby's _String.encode_
406
+ def self.iconv(to, from, string)
407
+ string.encode(to, from)
423
408
  end
424
409
  end
425
410
 
@@ -435,7 +420,7 @@ module ::Kernel
435
420
  nil
436
421
  end
437
422
 
438
- # Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
423
+ # Outputs _objs_ to STDOUT as JSON strings in a pretty format, with
439
424
  # indentation and over many lines.
440
425
  def jj(*objs)
441
426
  objs.each do |obj|