multi_json 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,4 +1,4 @@
1
- = Use X to JSON
1
+ = MultiJSON
2
2
 
3
3
  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:
4
4
 
@@ -13,6 +13,8 @@ Lots of Ruby libraries utilize JSON parsing in some form, and everyone has their
13
13
 
14
14
  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.
15
15
 
16
+ 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.
17
+
16
18
  == Note on Patches/Pull Requests
17
19
 
18
20
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/lib/multi_json.rb CHANGED
@@ -9,13 +9,22 @@ module MultiJson
9
9
  end
10
10
 
11
11
  # The default engine based on what you currently
12
- # have loaded. Tries Yajl first, then JSON gem,
13
- # then ActiveSupport and JSON pure.
12
+ # have loaded and installed. First checks to see
13
+ # if any engines are already loaded, then checks
14
+ # to see which are installed if none are loaded.
14
15
  def default_engine
15
16
  return :yajl if defined?(::Yajl)
16
17
  return :json_gem if defined?(::JSON)
17
18
  return :active_support if defined?(::ActiveSupport::JSON)
18
- :json_pure
19
+
20
+ %w(yajl json active_support json/pure).each do |req|
21
+ begin
22
+ require req
23
+ return req.sub('/','_').to_sym
24
+ rescue LoadError
25
+ next
26
+ end
27
+ end
19
28
  end
20
29
 
21
30
  # Set the JSON parser utilizing a symbol, string, or class.
@@ -2,17 +2,18 @@ require 'active_support' unless defined?(::ActiveSupport::JSON)
2
2
 
3
3
  module MultiJson
4
4
  module Engines
5
- class ActiveSupport
6
- def self.decode(string, options = {})
5
+ # Use ActiveSupport to encode/decode JSON.
6
+ class ActiveSupport
7
+ def self.decode(string, options = {}) #:nodoc:
7
8
  hash = ::ActiveSupport::JSON.decode(string)
8
9
  options[:symbolize_keys] ? symbolize_keys(hash) : hash
9
10
  end
10
11
 
11
- def self.encode(object)
12
+ def self.encode(object) #:nodoc:
12
13
  ::ActiveSupport::JSON.encode(object)
13
14
  end
14
15
 
15
- def self.symbolize_keys(hash)
16
+ def self.symbolize_keys(hash) #:nodoc:
16
17
  hash.inject({}){|result, (key, value)|
17
18
  new_key = case key
18
19
  when String then key.to_sym
@@ -2,14 +2,15 @@ require 'json' unless defined?(::JSON)
2
2
 
3
3
  module MultiJson
4
4
  module Engines
5
+ # Use the JSON gem to encode/decode.
5
6
  class JsonGem
6
- def self.decode(string, options = {})
7
+ def self.decode(string, options = {}) #:nodoc:
7
8
  opts = {}
8
9
  opts[:symbolize_names] = options[:symbolize_keys]
9
10
  ::JSON.parse(string, opts)
10
11
  end
11
12
 
12
- def self.encode(object)
13
+ def self.encode(object) #:nodoc:
13
14
  object.to_json
14
15
  end
15
16
  end
@@ -2,14 +2,15 @@ require 'json/pure' unless defined?(::JSON)
2
2
 
3
3
  module MultiJson
4
4
  module Engines
5
+ # Use JSON pure to encode/decode.
5
6
  class JsonPure
6
- def self.decode(string, options = {})
7
+ def self.decode(string, options = {}) #:nodoc:
7
8
  opts = {}
8
9
  opts[:symbolize_names] = options[:symbolize_keys]
9
10
  ::JSON.parse(string, opts)
10
11
  end
11
12
 
12
- def self.encode(object)
13
+ def self.encode(object) #:nodoc:
13
14
  object.to_json
14
15
  end
15
16
  end
@@ -2,12 +2,13 @@ require 'yajl' unless defined?(Yajl)
2
2
 
3
3
  module MultiJson
4
4
  module Engines
5
+ # Use the Yajl-Ruby library to encode/decode.
5
6
  class Yajl
6
- def self.decode(string, options = {})
7
+ def self.decode(string, options = {}) #:nodoc:
7
8
  ::Yajl::Parser.new(:symbolize_keys => options[:symbolize_keys]).parse(string)
8
9
  end
9
10
 
10
- def self.encode(object)
11
+ def self.encode(object) #:nodoc:
11
12
  ::Yajl::Encoder.new.encode(object)
12
13
  end
13
14
  end
data/multi_json.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{multi_json}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Bleigh"]
12
- s.date = %q{2010-06-13}
12
+ s.date = %q{2010-06-14}
13
13
  s.description = %q{A gem to provide swappable JSON backends utilizing Yajl::Ruby, the JSON gem, ActiveSupport, or JSON pure.}
14
14
  s.email = %q{michael@intridea.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Bleigh
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-13 00:00:00 -04:00
17
+ date: 2010-06-14 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency