Skip to content

Commit

Permalink
Drop MultiJson in favor of stdlib JSON
Browse files Browse the repository at this point in the history
Closes #409, #419.
  • Loading branch information
georgeclaghorn committed May 13, 2019
1 parent 2afb881 commit b952ae0
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 22 deletions.
17 changes: 1 addition & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ json.partial! partial: 'posts/post', collection: @posts, as: :post
json.comments @post.comments, partial: 'comments/comment', as: :comment
```

The `as: :some_symbol` is used with partials. It will take care of mapping the passed in object to a variable for the partial. If the value is a collection (either implicitly or explicitly by using the `collection:` option, then each value of the collection is passed to the partial as the variable `some_symbol`. If the value is a singular object, then the object is passed to the partial as the variable `some_symbol`.
The `as: :some_symbol` is used with partials. It will take care of mapping the passed in object to a variable for the partial. If the value is a collection (either implicitly or explicitly by using the `collection:` option, then each value of the collection is passed to the partial as the variable `some_symbol`. If the value is a singular object, then the object is passed to the partial as the variable `some_symbol`.

Be sure not to confuse the `as:` option to mean nesting of the partial. For example:

Expand Down Expand Up @@ -274,21 +274,6 @@ environment.rb for example):
Jbuilder.key_format camelize: :lower
```

Faster JSON backends
--------------------

Jbuilder uses MultiJson, which by default will use the JSON gem. That gem is
currently tangled with ActiveSupport's all-Ruby `#to_json` implementation,
which is slow (fixed in Rails >= 4.1). For faster Jbuilder rendering, you can
specify something like the Yajl JSON generator instead. You'll need to include
the `yajl-ruby` gem in your Gemfile and then set the following configuration
for MultiJson:

``` ruby
require 'multi_json'
MultiJson.use :yajl
```

## Contributing to Jbuilder

Jbuilder is the work of many contributors. You're encouraged to submit pull requests, propose
Expand Down
1 change: 0 additions & 1 deletion jbuilder.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Gem::Specification.new do |s|
s.required_ruby_version = '>= 1.9.3'

s.add_dependency 'activesupport', '>= 4.2.0'
s.add_dependency 'multi_json', '>= 1.2'

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- test/*`.split("\n")
Expand Down
4 changes: 2 additions & 2 deletions lib/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'jbuilder/blank'
require 'jbuilder/key_formatter'
require 'jbuilder/errors'
require 'multi_json'
require 'json'
require 'ostruct'

class Jbuilder
Expand Down Expand Up @@ -247,7 +247,7 @@ def merge!(hash_or_array)

# Encodes the current builder as JSON.
def target!
::MultiJson.dump(@attributes)
::JSON.dump(@attributes)
end

private
Expand Down
6 changes: 3 additions & 3 deletions test/jbuilder_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class JbuilderTemplateTest < ActiveSupport::TestCase
end
JBUILDER

assert_equal MultiJson.dump(name: "Hit"), Rails.cache.read("jbuilder/root/cache-key")
assert_equal JSON.dump(name: "Hit"), Rails.cache.read("jbuilder/root/cache-key")

result = render(<<-JBUILDER)
json.cache_root! "cache-key" do
Expand Down Expand Up @@ -223,7 +223,7 @@ class JbuilderTemplateTest < ActiveSupport::TestCase
end
JBUILDER

assert_equal MultiJson.dump(%w[ a b c ]), Rails.cache.read("jbuilder/root/cache-key")
assert_equal JSON.dump(%w[ a b c ]), Rails.cache.read("jbuilder/root/cache-key")

assert_equal %w[ a b c ], render(<<-JBUILDER)
json.cache_root! "cache-key" do
Expand Down Expand Up @@ -285,7 +285,7 @@ class JbuilderTemplateTest < ActiveSupport::TestCase

private
def render(*args)
MultiJson.load render_without_parsing(*args)
JSON.load render_without_parsing(*args)
end

def render_without_parsing(source, assigns = {})
Expand Down

5 comments on commit b952ae0

@jankmet
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to use jbuilder with oj without multi_json?

@aelkoussy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use OJ instead of standard JSON? in the core jbuilder, or at least with some flag
As far as I know OJ is much faster than JSON library

@jankmet
Copy link

@jankmet jankmet commented on b952ae0 Oct 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should work:

config/initializers/oj.rb

require 'oj'

Oj.optimize_rails

@aelkoussy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Jan, 2 comments:
1- I think the require line is not needed, as bundler requires the gem by default, right?
2- I am still very curious to know why we switched to the ruby native JSON encoder instead of the multiJson or Oj if someone knows the reason

@pboling
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of multi_json is to allow the implementer to decide which JSON library to use, and avoids this exact issue. I think the documentation removed in this PR was a bit misleading about what multi_json is for. Half a billion downloads speak pretty loudly.

Please sign in to comment.