Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove multi_json completely, simplify json handling

  • Loading branch information...
commit 9bfb1a44b95d1bed65f2818c533dbd38f054ba7f 1 parent 210d282
@nesquena authored
View
4 CHANGELOG.md
@@ -1,6 +1,8 @@
# CHANGELOG
-## 0.7.11
+## 0.8.0
+
+ * Remove multi_json dependency, simpler JSON handling
## 0.7.10
View
16 README.md
@@ -23,6 +23,10 @@ For a breakdown of common misconceptions about RABL, please check out our guide
## Breaking Changes ##
+ * v0.8.0 (released Feb 14, 2012) removes multi_json dependency and
+ relies on Oj (or JSON) as the json parser. Simplifies code, removes a dependency
+ but you might want to remove any references to MultiJson.
+
* v0.6.14 (released June 28, 2012) requires the use of render_views
with RSpec to test templates. Otherwise, the controller will simply
pass through the render command as it does with ERB templates.
@@ -117,7 +121,7 @@ Rabl.configure do |config|
# config.cache_engine = Rabl::CacheEngine.new # Defaults to Rails cache
# config.perform_caching = false
# config.escape_all_output = false
- # config.json_engine = nil # Any multi_json engines or a Class with #encode method
+ # config.json_engine = nil # Class with #dump class method (defaults JSON)
# config.msgpack_engine = nil # Defaults to ::MessagePack
# config.bson_engine = nil # Defaults to ::BSON
# config.plist_engine = nil # Defaults to ::Plist::Emit
@@ -165,8 +169,7 @@ attempts to render an attribute that does not exist. Otherwise, the attribute wi
Setting this to true during development may help increase the robustness of your code, but using `true` in
production code is not recommended.
-Note that the `json_engine` option uses [multi_json](http://intridea.com/2010/6/14/multi-json-the-swappable-json-handler) engine
-defaults so that in most cases you **don't need to configure this** directly. For example, if you wish to use [oj](https://github.com/ohler55/oj) as
+If you wish to use [oj](https://github.com/ohler55/oj) as
the primary JSON encoding engine simply add that to your Gemfile:
```ruby
@@ -174,10 +177,9 @@ the primary JSON encoding engine simply add that to your Gemfile:
gem 'oj'
```
-and RABL will automatically start using that engine for encoding your JSON responses!
-
-To use RABL with JSON engine not supported by `multi_json`, ensure that JSON engine
-supports `encode` method and set `json_engine` option to the engine's Class name:
+and RABL will use that engine automatically for encoding your JSON responses.
+Set your own custom json_engine which define a `dump` or `encode`
+method for converting to JSON from ruby data:
```ruby
config.json_engine = ActiveSupport::JSON
View
44 fixtures/padrino_test/app/app.rb
@@ -6,21 +6,6 @@ class PadrinoTest < Padrino::Application
enable :sessions
##
- # Caching support
- #
- # register Padrino::Cache
- # enable :caching
- #
- # You can customize caching store engines:
- #
- # set :cache, Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
- # set :cache, Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1))
- # set :cache, Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
- # set :cache, Padrino::Cache::Store::Memory.new(50)
- # set :cache, Padrino::Cache::Store::File.new(Padrino.root('tmp', app_name.to_s, 'cache')) # default choice
- #
-
- ##
# Application configuration options
#
# set :raise_errors, true # Raise exceptions (will stop application) (default for test)
@@ -35,25 +20,12 @@ class PadrinoTest < Padrino::Application
# disable :flash # Disables rack-flash (enabled by default if Rack::Flash is defined)
# layout :my_layout # Layout can be in views/layouts/foo.ext or views/foo.ext (default :application)
#
-
- ##
- # You can configure for a specified environment like:
- #
- # configure :development do
- # set :foo, :bar
- # disable :asset_stamp # no asset timestamping for dev
- # end
- #
-
- ##
- # You can manage errors like:
- #
- # error 404 do
- # render 'errors/404'
- # end
- #
- # error 505 do
- # render 'errors/505'
- # end
- #
end
+
+# Patch times to return as iso8601
+class Time
+ alias_method :old_to_s, :to_s
+ def to_s(format=nil)
+ format ? old_to_s(format) : iso8601
+ end
+end
View
2  fixtures/rails3_2/Gemfile
@@ -36,5 +36,5 @@ gem 'jquery-rails'
# gem 'capistrano'
# To use debugger
-gem 'debugger'
+# gem 'debugger'
View
8 fixtures/sinatra_test/app.rb
@@ -41,4 +41,12 @@ class SinatraTest < Sinatra::Application
@user = User.find(params[:id])
render :rabl, :"users/show.json", :format => "json"
end
+end
+
+# Patch times to return as iso8601
+class Time
+ alias_method :old_to_s, :to_s
+ def to_s(format=nil)
+ format ? old_to_s(format) : iso8601
+ end
end
View
1  lib/rabl.rb
@@ -13,7 +13,6 @@
require 'rabl/configuration'
require 'rabl/renderer'
require 'rabl/cache_engine'
-require 'rabl/json_engine'
require 'rabl/railtie' if defined?(Rails) && Rails.version =~ /^[34]/
# Rabl.register!
View
17 lib/rabl/configuration.rb
@@ -16,6 +16,13 @@
rescue LoadError
end
+# Set default options for Oj json parser (if exists)
+begin
+ require 'oj'
+ Oj.default_options = { :mode => :compat, :time_format => :ruby }
+rescue LoadError
+end
+
module Rabl
# Rabl.host
class Configuration
@@ -28,6 +35,7 @@ class Configuration
attr_accessor :enable_json_callbacks
attr_accessor :bson_check_keys
attr_accessor :bson_move_id
+ attr_writer :json_engine
attr_writer :msgpack_engine
attr_writer :bson_engine
attr_writer :plist_engine
@@ -65,16 +73,9 @@ def initialize
@perform_caching = false
end
- # @param [Symbol, Class] engine_name The name of a JSON engine,
- # or class that responds to `encode`, to use to encode Rabl templates
- # into JSON. For more details, see the MultiJson gem.
- def json_engine=(engine_name_or_class)
- JsonEngine.instance.set(engine_name_or_class)
- end
-
# @return The JSON engine used to encode Rabl templates into JSON
def json_engine
- JsonEngine.instance.current_engine
+ @json_engine || (defined?(::Oj) ? ::Oj : ::JSON)
end
##
View
2  lib/rabl/engine.rb
@@ -232,7 +232,7 @@ def request_params
# format_json("{ foo : "bar" }") => "test({ foo : 'bar' })"
def format_json(json_output)
json_engine = Rabl.configuration.json_engine
- json_method = json_engine.respond_to?(:dump) ? 'dump' : 'encode' # multi_json compatibility TODO
+ json_method = json_engine.respond_to?(:dump) ? 'dump' : 'encode'
json_output = json_engine.send(json_method, json_output) unless json_output.is_a?(String)
use_callback = Rabl.configuration.enable_json_callbacks && request_params[:callback].present?
use_callback ? "#{request_params[:callback]}(#{json_output})" : json_output
View
33 lib/rabl/json_engine.rb
@@ -1,33 +0,0 @@
-# Defines the default JSON engine for RABL when rendering JSON is invoked on a template.
-# You can define your own json engine by creating an object that responds to the `encode` method
-# and setting the corresponding configuration option:
-#
-# config.json_engine = ActiveSupport::JSON
-#
-
-require 'multi_json'
-require 'singleton'
-
-module Rabl
- class JsonEngine
- include Singleton
-
- attr_reader :current_engine
-
- def initialize
- @current_engine = MultiJson.respond_to?(:adapter) ? MultiJson.adapter : MultiJson.engine
- end
-
- def set(engine_name_or_class)
- @current_engine = begin
- MultiJson.respond_to?(:use) ?
- MultiJson.use(engine_name_or_class) :
- MultiJson.engine = engine_name_or_class
- rescue RuntimeError => e #
- # Re-raise if engine_name_or_class is invalid
- raise e unless engine_name_or_class.respond_to?(:encode)
- engine_name_or_class
- end
- end
- end # JsonEngine
-end # Rabl
View
1  rabl.gemspec
@@ -20,7 +20,6 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.add_dependency 'activesupport', '>= 2.3.14'
- s.add_dependency 'multi_json', '~> 1.0'
s.add_development_dependency 'riot', '~> 0.12.3'
s.add_development_dependency 'rr', '~> 1.0.2'
View
10 test/configuration_test.rb
@@ -3,7 +3,7 @@
context 'Rabl::Configuration' do
context 'defaults' do
# multi_json compatibility TODO
- helper(:json_engine) { MultiJson.respond_to?(:adapter) ? MultiJson.adapter : MultiJson.engine }
+ helper(:json_engine) { ::Oj }
setup { Rabl.configuration }
asserts(:include_json_root).equals true
@@ -35,14 +35,6 @@
asserts('uses a custom JSON engine') { topic.json_engine.to_s == 'ActiveSupport::JSON' }
end # custom JSON, class
- context 'invalid JSON engine configured' do
- asserts {
- Rabl.configure do |c|
- c.json_engine = Kernel
- end
- }.raises(RuntimeError)
- end # invalid
-
context 'raise on missing attributes' do
setup do
Rabl.configure do |c|
Please sign in to comment.
Something went wrong with that request. Please try again.