Permalink
Browse files

Merge pull request #10 from sstephenson/multi_json

---

This patch:

1. Removes the ActiveSupport::JSON engine so that Rails 3.1 can avoid a circular dependency when the Sprockets and ExecJS libraries require MultiJSON. (As the original author of ActiveSupport::JSON, Ill also be pushing for Rails to remove it and defer to MultiJSON instead.)
2. Bundles the [OkJson](https://github.com/kr/okjson) library and adds an Okjson engine that will be used if no other engines are available.
  • Loading branch information...
joshk committed Apr 14, 2011
2 parents fdad79f + e89093c commit 4d24e4ca4ba6caa704f43615b56381e287932a27
View
@@ -3,20 +3,20 @@
Lots of Ruby libraries utilize JSON parsing in some form, and everyone has their favorite JSON library. In order to best support multiple JSON parsers and libraries, <tt>multi_json</tt> is a general-purpose swappable JSON backend library. You use it like so:
require 'multi_json'
-
+
MultiJson.engine = :yajl
MultiJson.decode('{"abc":"def"}') # decoded using Yajl
-
+
MultiJson.engine = :json_gem
MultiJson.engine = MultiJson::Engines::JsonGem # equivalent to previous line
MultiJson.encode({:abc => 'def'}) # encoded using the JSON gem
-
+
The <tt>engine</tt> setter takes either a symbol or a class (to allow for custom JSON parsers) that responds to both <tt>.decode</tt> and <tt>.encode</tt> at the class level.
-MultiJSON tries to have intelligent defaulting. That is, if you have any of the supported engines already loaded, it will utilize them before attempting to load any. When loading, libraries are ordered by speed. First Yajl-Ruby, then the JSON gem, then ActiveSupport, then JSON pure.
+MultiJSON tries to have intelligent defaulting. That is, if you have any of the supported engines already loaded, it will utilize them before attempting to load any. When loading, libraries are ordered by speed. First Yajl-Ruby, then the JSON gem, then JSON pure. If no JSON library is available, MultiJSON falls back to a bundled version of <a href="https://github.com/kr/okjson">OkJson</a>.
== Note on Patches/Pull Requests
-
+
* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a
@@ -27,4 +27,4 @@ MultiJSON tries to have intelligent defaulting. That is, if you have any of the
== Copyright
-Copyright (c) 2010 Michael Bleigh and Intridea, Inc. See LICENSE for details.
+Copyright (c) 2010 Michael Bleigh and Intridea, Inc. See LICENSE for details.
View
@@ -12,8 +12,8 @@ def engine
REQUIREMENT_MAP = [
["yajl", :yajl],
["json", :json_gem],
- ["active_support", :active_support],
- ["json/pure", :json_pure]
+ ["json/pure", :json_pure],
+ ["okjson", :okjson]
]
# The default engine based on what you currently
@@ -23,7 +23,6 @@ def engine
def default_engine
return :yajl if defined?(::Yajl)
return :json_gem if defined?(::JSON)
- return :active_support if defined?(::ActiveSupport::JSON)
REQUIREMENT_MAP.each do |(library, engine)|
begin
@@ -40,7 +39,7 @@ def default_engine
#
# * <tt>:json_gem</tt>
# * <tt>:json_pure</tt>
- # * <tt>:active_support</tt> (useful for inside Rails apps)
+ # * <tt>:okjson</tt>
# * <tt>:yajl</tt>
def engine=(new_engine)
case new_engine
@@ -1,32 +0,0 @@
-require 'active_support' unless defined?(::ActiveSupport::JSON)
-
-module MultiJson
- module Engines
- # Use ActiveSupport to encode/decode JSON.
- class ActiveSupport
- def self.decode(string, options = {}) #:nodoc:
- hash = ::ActiveSupport::JSON.decode(string)
- options[:symbolize_keys] ? symbolize_keys(hash) : hash
- end
-
- def self.encode(object) #:nodoc:
- ::ActiveSupport::JSON.encode(object)
- end
-
- def self.symbolize_keys(hash) #:nodoc:
- hash.inject({}){|result, (key, value)|
- new_key = case key
- when String then key.to_sym
- else key
- end
- new_value = case value
- when Hash then symbolize_keys(value)
- else value
- end
- result[new_key] = new_value
- result
- }
- end
- end
- end
-end
@@ -0,0 +1,34 @@
+require "multi_json/vendor/okjson" unless defined?(::OkJson)
+
+module MultiJson
+ module Engines
+ class Okjson
+ def self.decode(string, options = {}) #:nodoc:
+ result = OkJson.decode(string)
+ options[:symbolize_keys] ? symbolize_keys(result) : result
+ end
+
+ def self.encode(object) #:nodoc:
+ OkJson.encode(stringify_keys(object))
+ end
+
+ def self.symbolize_keys(object) #:nodoc:
+ return object unless object.is_a?(Hash)
+ object.inject({}) do |result, (key, value)|
+ new_key = key.is_a?(String) ? key.to_sym : key
+ new_value = value.is_a?(Hash) ? symbolize_keys(value) : value
+ result.merge! new_key => new_value
+ end
+ end
+
+ def self.stringify_keys(object) #:nodoc:
+ return object unless object.is_a?(Hash)
+ object.inject({}) do |result, (key, value)|
+ new_key = key.is_a?(Symbol) ? key.to_s : key
+ new_value = value.is_a?(Hash) ? stringify_keys(value) : value
+ result.merge! new_key => new_value
+ end
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 4d24e4c

Please sign in to comment.