rails-patch-json-encode 0.0.1 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f6e4506779f59734a8f7e0de0e32a59972e963e
4
- data.tar.gz: 8b0556765ad8e5ecd0ec80d379cf004c5e85a604
3
+ metadata.gz: cf3ccc8ec566598e89ac1b31698f4ec0d3292c93
4
+ data.tar.gz: 8e7e3f808aee6ad5ff41f6c526e3a86d85460abe
5
5
  SHA512:
6
- metadata.gz: d2248a53df54b7dec6839db35aa736adb78772cb220a29295ecf73edb3b75bb2aae10f89b6c5c0ebf7c8d47e8ab535f548e965a5ac84c783d1a9fdf2176645ae
7
- data.tar.gz: debf84761c18bc0c9d78aabccae3993146bc3959fee74738120d32778cf65e9c2ec11163d9e2d66c5e8676d47146e84d35075dafb0ac0b1820a059b646dbbddc
6
+ metadata.gz: 019d87cceca6d75fa582bfa7cb65975a2a8eac7fe4151dbb354e92b26b09ce3789b573232374ab7f2d89a09162904149f7b265f0b34b4ee2c202d4884da5f1e5
7
+ data.tar.gz: 7fe82b3585ac197c9f5f4ba923caa6ba90724cc67b08ba0a7a0bcdcfd08d4fa5e7676dc8609c3f26448194cb459be4745e4b9a9e25039bab7a562318fb1ee324
data/README.md CHANGED
@@ -6,7 +6,7 @@ For full details please read Jason Hutchens' [blog post](http://devblog.agworld.
6
6
 
7
7
  All credits goes to [Jason Hutchens](https://github.com/jasonhutchens) for discovering the issue and providing the code for this monkey patch.
8
8
 
9
- ## Usage
9
+ ## Installation
10
10
 
11
11
  First, go to your Rails console and type:
12
12
 
@@ -17,14 +17,23 @@ First, go to your Rails console and type:
17
17
 
18
18
  See how Rails performs before the patch.
19
19
 
20
- Then bundle install this gem with a fast JSON encoding gem in your Rails' Gemfile.
20
+ Second, bundle install this gem with a fast JSON encoding gem in your Rails' Gemfile.
21
21
 
22
22
  gem 'rails-patch-json-encode'
23
23
  gem 'oj'
24
24
 
25
- In this case I choose the oj gem, but you can [choose a JSON gem that multi_json supports](https://github.com/intridea/multi_json#supported-json-engines).
25
+ In this case I choose the oj gem, but you can [choose a json-encoder gem that multi_json supports](https://github.com/intridea/multi_json#supported-json-engines).
26
26
 
27
- Rails should now use the faster encoder. Now restart your console again and re-run the test to see how the performance changes.
27
+ Last, there are two levels of patch available. You have to choose one and call it explictly:
28
+
29
+ * `Rails::Patch::Json::Encode.patch_base_classes` patches all Ruby base classes.
30
+ * `Rails::Patch::Json::Encode.patch_renderers` patches Rails' ActionController::Renderers only. This is for those who had issue with the JSON gem, as patching base classes cause infinite recursive loop.
31
+
32
+ Place one of them in Rails' initializers like config/initializers/rails_patch_json_encode.rb, and Rails should now use the faster encoder.
33
+
34
+ ## Benchmark
35
+
36
+ For console benchmark comparison, restart console after the above installation. Call `Rails::Patch::Json::Encode.patch_base_classes` in console, then re-run the test to see how the performance changes.
28
37
 
29
38
  The actual performance boost on real-world applications will probably be less than that. For one of my page I see the rendering time dropped by 25%.
30
39
 
@@ -1,11 +1,34 @@
1
1
  require "rails/patch/json/encode/version"
2
2
 
3
- # Code from http://devblog.agworld.com.au/post/42586025923/the-performance-of-to-json-in-rails-sucks-and-theres
4
- # essentially reversing Rails' hard-coded call to ActiveSupport::JSON.encode
5
- [Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].each do |klass|
6
- klass.class_eval do
7
- def to_json(opts = {})
8
- MultiJson::dump(self.as_json(opts), opts)
3
+ module Rails::Patch::Json::Encode
4
+ # Use multi_json instead of Rails' to_json method (which calls ActiveSupport::JSON)
5
+ # when `render :json => @obj` is called.
6
+ def self.patch_renderers
7
+ ::ActionController::Renderers.module_eval do
8
+ # Override
9
+ add :json do |json, options|
10
+ json = MultiJson::dump(json.as_json(options), options) unless json.kind_of?(String)
11
+
12
+ if options[:callback].present?
13
+ self.content_type ||= Mime::JS
14
+ "#{options[:callback]}(#{json})"
15
+ else
16
+ self.content_type ||= Mime::JSON
17
+ json
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ # Code from http://devblog.agworld.com.au/post/42586025923/the-performance-of-to-json-in-rails-sucks-and-theres
24
+ # essentially reversing Rails' hard-coded call to ActiveSupport::JSON.encode
25
+ def self.patch_base_classes
26
+ [Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].each do |klass|
27
+ klass.class_eval do
28
+ def to_json(opts = {})
29
+ MultiJson::dump(self.as_json(opts), opts)
30
+ end
31
+ end
9
32
  end
10
33
  end
11
34
  end
@@ -2,7 +2,7 @@ module Rails
2
2
  module Patch
3
3
  module Json
4
4
  module Encode
5
- VERSION = "0.0.1"
5
+ VERSION = "0.1.0"
6
6
  end
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-patch-json-encode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - lulalala
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-19 00:00:00.000000000 Z
12
+ date: 2014-03-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -88,9 +88,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  version: '0'
89
89
  requirements: []
90
90
  rubyforge_project:
91
- rubygems_version: 2.0.5
91
+ rubygems_version: 2.0.14
92
92
  signing_key:
93
93
  specification_version: 4
94
94
  summary: A monkey patch to speed up Rails' JSON generation time.
95
95
  test_files: []
96
- has_rdoc: