Skip to content
This repository

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

Merged
merged 1 commit into from almost 3 years ago

2 participants

Jeroen van Dijk Brian Ryckbost
Jeroen van Dijk

No description provided.

Brian Ryckbost
Owner

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

Jeroen van Dijk

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

Brian Ryckbost
Owner

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

Jeroen van Dijk

Cool! Spread the word :)

Brian Ryckbost bryckbost merged commit d48842c into from
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.
2  json_spec.gemspec
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18 18 s.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f) }
19 19 s.require_paths = ["lib"]
20 20
21   - s.add_dependency "json", "~> 1.0"
  21 + s.add_dependency "multi_json", "~> 1.0.0"
22 22 s.add_dependency "rspec", "~> 2.0"
23 23
24 24 s.add_development_dependency "rake", "~> 0.9"
8 lib/json_spec/helpers.rb
... ... @@ -1,12 +1,14 @@
  1 +require 'multi_json'
  2 +
1 3 module JsonSpec
2 4 module Helpers
3 5 extend self
4 6
5 7 def parse_json(json, path = nil)
6   - ruby = JSON.parse(%([#{json}])).first
  8 + ruby = MultiJson.decode(%([#{json}])).first
7 9 value_at_json_path(ruby, path)
8   - rescue JSON::ParserError
9   - JSON.parse(json)
  10 + rescue MultiJson::DecodeError
  11 + MultiJson.decode(json)
10 12 end
11 13
12 14 def normalize_json(json, path = nil)
2  spec/json_spec/helpers_spec.rb
@@ -13,7 +13,7 @@
13 13 end
14 14
15 15 it "raises a parser error for invalid JSON" do
16   - expect{ parse_json("json_spec") }.to raise_error(JSON::ParserError)
  16 + expect{ parse_json("json_spec") }.to raise_error(MultiJson::DecodeError)
17 17 end
18 18
19 19 it "parses at a path if given" do

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.