Permalink
Browse files

Options can be passed to an engine on encode

- Pass options straight thru to Yajl
- OkJson ignores options
- Pre-process options for Json/Json-Pure
  For these if :pretty => true is passed, the
  option is converted to the default set of options
  used by these libraries when pretty_generate is invoked
- Common code for json and json-pure refactored into the
  JsonCommon module
- Specs for all above behaviors
  • Loading branch information...
bsiggelkow committed Sep 23, 2011
1 parent 6a04dc5 commit e0a7ff5d5ff621ffccc61617ed8aeec5816e81f7
View
@@ -79,7 +79,7 @@ def decode(string, options = {})
end
# Encodes a Ruby object as JSON.
- def encode(object)
- engine.encode(object)
+ def encode(object, options = {})
+ engine.encode(object, options)
end
end
@@ -0,0 +1,27 @@
+module MultiJson
+ module Engines
+ module JsonCommon
+
+ def decode(string, options = {}) #:nodoc:
+ opts = {}
+ opts[:symbolize_names] = options[:symbolize_keys]
+ string = string.read if string.respond_to?(:read)
+ ::JSON.parse(string, opts)
+ end
+
+ def encode(object, options = {}) #:nodoc:
+ object.to_json( process_options(options) )
+ end
+
+ protected
+
+ def process_options(options={})
+ return options if options.empty?
+ opts = {}
+ opts.merge!(JSON::PRETTY_STATE_PROTOTYPE.to_h) if options.delete(:pretty)
+ opts.merge! options
+ end
+
+ end
+ end
+end
@@ -1,21 +1,12 @@
require 'json' unless defined?(::JSON)
+require File.join(File.dirname(__FILE__),'json_common')
module MultiJson
module Engines
# Use the JSON gem to encode/decode.
class JsonGem
ParseError = ::JSON::ParserError
-
- def self.decode(string, options = {}) #:nodoc:
- opts = {}
- opts[:symbolize_names] = options[:symbolize_keys]
- string = string.read if string.respond_to?(:read)
- ::JSON.parse(string, opts)
- end
-
- def self.encode(object) #:nodoc:
- object.to_json
- end
+ extend JsonCommon
end
end
end
@@ -1,21 +1,12 @@
require 'json/pure' unless defined?(::JSON)
+require File.join(File.dirname(__FILE__),'json_common')
module MultiJson
module Engines
# Use JSON pure to encode/decode.
class JsonPure
ParseError = ::JSON::ParserError
-
- def self.decode(string, options = {}) #:nodoc:
- opts = {}
- opts[:symbolize_names] = options[:symbolize_keys]
- string = string.read if string.respond_to?(:read)
- ::JSON.parse(string, opts)
- end
-
- def self.encode(object) #:nodoc:
- object.to_json
- end
+ extend JsonCommon
end
end
end
@@ -11,7 +11,7 @@ def self.decode(string, options = {}) #:nodoc:
options[:symbolize_keys] ? symbolize_keys(result) : result
end
- def self.encode(object) #:nodoc:
+ def self.encode(object, options = {}) #:nodoc:
::OkJson.valenc(stringify_keys(object))
end
@@ -10,8 +10,8 @@ def self.decode(string, options = {}) #:nodoc:
::Yajl::Parser.new(:symbolize_keys => options[:symbolize_keys]).parse(string)
end
- def self.encode(object) #:nodoc:
- ::Yajl::Encoder.new.encode(object)
+ def self.encode(object, options = {}) #:nodoc:
+ ::Yajl::Encoder.encode(object, options)
end
end
end
View
@@ -103,6 +103,22 @@
MultiJson.encode("random rootless string").should == "\"random rootless string\""
MultiJson.encode(123).should == "123"
end
+
+ it 'passes options to the engine' do
+ MultiJson.engine.should_receive(:encode).with('foo', {:bar => :baz})
+ MultiJson.encode('foo', :bar => :baz)
+ end
+
+ if engine == 'json_gem' || engine == 'json_pure'
+ describe 'with :pretty option set to true' do
+ it 'passes default pretty options' do
+ object = 'foo'
+ object.should_receive(:to_json).with(JSON::PRETTY_STATE_PROTOTYPE.to_h)
+ MultiJson.encode(object,:pretty => true)
+ end
+ end
+ end
+
end
describe '.decode' do

0 comments on commit e0a7ff5

Please sign in to comment.