Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: fast_jsonparser adapter for multi_json #201

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ tmtags
## VIM
*.swp

## IDEA
.idea

## PROJECT::GENERAL
.yardoc
coverage
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ end

task :spec => %w[
base_spec
adapters:fast_jsonparser
adapters:oj
adapters:yajl
adapters:json_gem
Expand Down
14 changes: 13 additions & 1 deletion benchmark.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
libx = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(libx) unless $LOAD_PATH.include?(libx)

require 'oj'
require 'fast_jsonparser'
require 'multi_json'
require 'benchmark/ips'

Expand All @@ -7,5 +11,13 @@
Benchmark.ips do |x|
x.time = 10
x.warmup = 1
x.report { MultiJson.load(MultiJson.dump(a: 1, b: 2, c: 3)) }
x.report("oj") { MultiJson.load(MultiJson.dump(a: 1, b: 2, c: 3)) }
end

MultiJson.use :fast_jsonparser

Benchmark.ips do |x|
x.time = 10
x.warmup = 1
x.report("fast_jsonparser") { MultiJson.load(MultiJson.dump(a: 1, b: 2, c: 3)) }
end
3 changes: 3 additions & 0 deletions lib/multi_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def default_options
ALIASES = {'jrjackson' => 'jr_jackson'}

REQUIREMENT_MAP = [
[:fast_jsonparser, 'fast_jsonparser'],
[:oj, 'oj'],
[:yajl, 'yajl'],
[:jr_jackson, 'jrjackson'],
Expand All @@ -44,6 +45,7 @@ def default_options
# if any adapters are already loaded, then checks
# to see which are installed if none are loaded.
def default_adapter
return :fast_jsonparser if defined?(::FastJsonparser)
return :oj if defined?(::Oj)
return :yajl if defined?(::Yajl)
return :jr_jackson if defined?(::JrJackson)
Expand Down Expand Up @@ -79,6 +81,7 @@ def adapter
# Set the JSON parser utilizing a symbol, string, or class.
# Supported by default are:
#
# * <tt>:fast_jsonparser</tt>
# * <tt>:oj</tt>
# * <tt>:json_gem</tt>
# * <tt>:json_pure</tt>
Expand Down
44 changes: 44 additions & 0 deletions lib/multi_json/adapters/fast_jsonparser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'fast_jsonparser'
require 'oj'
require 'multi_json/adapter'

module MultiJson
module Adapters
# Use the Oj library to dump/load.
class FastJsonparser < Adapter
defaults :load, :symbolize_keys => false
defaults :dump, :mode => :compat, :time_format => :ruby, :use_to_json => true

ParseError = ::FastJsonparser::Error

def load(string, options = {})
::FastJsonparser.parse(string, options)
end

case ::Oj::VERSION
when /\A2\./
def dump(object, options = {})
options.merge!(:indent => 2) if options[:pretty]
options[:indent] = options[:indent].to_i if options[:indent]
::Oj.dump(object, options)
end
when /\A3\./
PRETTY_STATE_PROTOTYPE = {
:indent => " ",
:space => " ",
:space_before => "",
:object_nl => "\n",
:array_nl => "\n",
:ascii_only => false,
}

def dump(object, options = {})
options.merge!(PRETTY_STATE_PROTOTYPE.dup) if options.delete(:pretty)
::Oj.dump(object, options)
end
else
fail "Unsupported Oj version: #{::Oj::VERSION}"
end
end
end
end
10 changes: 10 additions & 0 deletions spec/fast_jsonparser_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'spec_helper'

exit 0 if skip_adapter?('fast_jsonparser')

require 'shared/adapter'
require 'multi_json/adapters/fast_jsonparser'

describe MultiJson::Adapters::FastJsonparser do
it_behaves_like 'an adapter', described_class
end