diff --git a/.travis.yml b/.travis.yml index e2eeda56e..1ac58d828 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,12 +8,16 @@ rvm: - 2.0.0 - 2.1 - 2.2 - - 2.3.3 - - 2.4.1 - - jruby + - 2.3 + - 2.4 + - 2.5 + - 2.6 - ruby-head + - jruby matrix: allow_failures: + - rvm: 1.9.3 - rvm: ruby-head + - rvm: jruby script: "bundle exec rake" sudo: false diff --git a/CHANGES.md b/CHANGES.md index d96e9ae9d..92a192529 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,8 @@ # Changes +## 2019-02-21 (2.2.0) + * Adds support for 2.6 BigDecimal and ruby standard library Set datetype. + ## 2017-04-18 (2.1.0) * Allow passing of `decimal_class` option to specify a class as which to parse JSON float numbers. diff --git a/Gemfile b/Gemfile index 64f98d6c9..8fe591e03 100644 --- a/Gemfile +++ b/Gemfile @@ -12,5 +12,3 @@ when 'ext', nil when 'pure' gemspec :name => 'json_pure' end - -gem 'simplecov' diff --git a/README.md b/README.md index f9cd95d56..1b15cf104 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ -# JSON implementation for Ruby ![Travis Widget] -[Travis Widget]: http://travis-ci.org/flori/json.svg?branch=master +# JSON implementation for Ruby + +[](https://travis-ci.org/flori/json) ## Description @@ -150,6 +151,18 @@ require 'json/add/rails' Both of the additions attempt to require `'json'` (like above) first, if it has not been required yet. +## Serializing exceptions + +The JSON module doesn't extend `Exception` by default. If you convert an `Exception` +object to JSON, it will by default only include the exception message. + +To include the full details, you must either load the `json/add/core` mentioned +above, or specifically load the exception addition: + +```ruby +require 'json/add/exception' +``` + ## More Examples To create a JSON document from a ruby data structure, you can call @@ -179,14 +192,14 @@ should return a JSON object (a hash converted to JSON with `#to_json`) like this (don't forget the `*a` for all the arguments): ```ruby - class Range - def to_json(*a) - { - 'json_class' => self.class.name, # = 'Range' - 'data' => [ first, last, exclude_end? ] - }.to_json(*a) - end - end +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 @@ -194,26 +207,30 @@ 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 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: ```ruby - class Range - def self.json_create(o) - new(*o['data']) - end - end +class Range + def self.json_create(o) + new(*o['data']) + end +end ``` Now it possible to serialise/deserialise ranges as well: ```ruby - 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 = 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 = 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, :create_additions => true +# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10] ``` `JSON.generate` always creates the shortest possible string representation of a diff --git a/Rakefile b/Rakefile index c6c195f9f..ce390952d 100644 --- a/Rakefile +++ b/Rakefile @@ -23,8 +23,13 @@ class UndocumentedTestTask < Rake::TestTask def desc(*) end end -MAKE = ENV['MAKE'] || %w[gmake make].find { |c| system(c, '-v') } -BUNDLE = ENV['BUNDLE'] || %w[bundle].find { |c| system(c, '-v') } +which = lambda { |c| + w = `which #{c}` + break w.chomp unless w.empty? +} + +MAKE = ENV['MAKE'] || %w[gmake make].find(&which) +BUNDLE = ENV['BUNDLE'] || %w[bundle].find(&which) PKG_NAME = 'json' PKG_TITLE = 'JSON Implementation for Ruby' PKG_VERSION = File.read('VERSION').chomp @@ -47,8 +52,8 @@ JAVA_CLASSES = [] JRUBY_PARSER_JAR = File.expand_path("lib/json/ext/parser.jar") JRUBY_GENERATOR_JAR = File.expand_path("lib/json/ext/generator.jar") -RAGEL_CODEGEN = %w[rlcodegen rlgen-cd ragel].find { |c| system(c, '-v') } -RAGEL_DOTGEN = %w[rlgen-dot rlgen-cd ragel].find { |c| system(c, '-v') } +RAGEL_CODEGEN = %w[rlcodegen rlgen-cd ragel].find(&which) +RAGEL_DOTGEN = %w[rlgen-dot rlgen-cd ragel].find(&which) desc "Installing library (pure)" task :install_pure => :version do @@ -238,7 +243,7 @@ if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby' classpath = (Dir['java/lib/*.jar'] << 'java/src' << JRUBY_JAR) * ':' obj = src.sub(/\.java\Z/, '.class') file obj => src do - sh 'javac', '-classpath', classpath, '-source', '1.5', '-target', '1.5', src + sh 'javac', '-classpath', classpath, '-source', '1.6', '-target', '1.6', src end JAVA_CLASSES << obj end diff --git a/VERSION b/VERSION index 7ec1d6db4..ccbccc3dc 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.1.0 +2.2.0 diff --git a/data/example.json b/data/example.json deleted file mode 100644 index 88b4e8265..000000000 --- a/data/example.json +++ /dev/null @@ -1 +0,0 @@ -{"a":2,"b":3.141,"TIME":"2007-03-14T11:52:40","c":"c","d":[1,"b",3.14],"COUNT":666,"e":{"foo":"bar"},"foo":"B\u00e4r","g":"\u677e\u672c\u884c\u5f18","h":1000.0,"bar":"\u00a9 \u2260 \u20ac!","i":0.001,"j":"\ud840\udc01"} diff --git a/data/index.html b/data/index.html deleted file mode 100644 index abe6fdbfa..000000000 --- a/data/index.html +++ /dev/null @@ -1,38 +0,0 @@ - - -
-