Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow the user to decide which json library to use by using MultiJson #4

Merged
merged 1 commit into from

2 participants

@jeroenvandijk

No description provided.

@bryckbost
Owner

I'm not familiar with MultiJson. Does it default to JSON if you don't explicitly set an engine?

@jeroenvandijk

Yes, so MultiJson falls back on a bundled version if no JSON library is present (called OkJson). For 1.9 it will use the standard lib JSON by default. Projects like Rails already use it: https://github.com/rails/rails/blob/master/activesupport/activesupport.gemspec#L20

The advantage is that users are not forced to use a particular JSON library just because the library developer chose a particular JSON gem. And you as a library developer do not need to worry about choosing the right one :)

Hope this helps.

Jeroen

@bryckbost
Owner

Sounds good to me. Wish I had known about it sooner.

@jeroenvandijk

Cool! Spread the word :)

@bryckbost bryckbost merged commit d48842c into collectiveidea:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  json_spec.gemspec
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f) }
s.require_paths = ["lib"]
- s.add_dependency "json", "~> 1.0"
+ s.add_dependency "multi_json", "~> 1.0.0"
s.add_dependency "rspec", "~> 2.0"
s.add_development_dependency "rake", "~> 0.9"
View
8 lib/json_spec/helpers.rb
@@ -1,12 +1,14 @@
+require 'multi_json'
+
module JsonSpec
module Helpers
extend self
def parse_json(json, path = nil)
- ruby = JSON.parse(%([#{json}])).first
+ ruby = MultiJson.decode(%([#{json}])).first
value_at_json_path(ruby, path)
- rescue JSON::ParserError
- JSON.parse(json)
+ rescue MultiJson::DecodeError
+ MultiJson.decode(json)
end
def normalize_json(json, path = nil)
View
2  spec/json_spec/helpers_spec.rb
@@ -13,7 +13,7 @@
end
it "raises a parser error for invalid JSON" do
- expect{ parse_json("json_spec") }.to raise_error(JSON::ParserError)
+ expect{ parse_json("json_spec") }.to raise_error(MultiJson::DecodeError)
end
it "parses at a path if given" do
Something went wrong with that request. Please try again.