Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #123 from librato/fix/smartjson-ruby-2-3-1
Browse files Browse the repository at this point in the history
Fix SmartJSON
  • Loading branch information
Chance Feick authored Oct 5, 2016
2 parents 035120e + 5ab02a0 commit 5034481
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ rvm:
- 2.1.7
- 2.2.3
- 2.3.0
- 2.3.1
- jruby-19mode
# - rbx
- ruby-head
Expand Down
35 changes: 21 additions & 14 deletions lib/librato/metrics/smart_json.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
module Librato
module Metrics
class SmartJSON
extend SingleForwardable

if defined?(::MultiJson)
# MultiJSON >= 1.3.0
if MultiJson.respond_to?(:load)
def_delegator MultiJson, :load, :read
else
def_delegator MultiJson, :decode, :read
def self.read(json)
# MultiJSON >= 1.3.0
if MultiJson.respond_to?(:load)
MultiJson.load(json)
else
MultiJson.decode(json)
end
end

# MultiJSON <= 1.2.0
if MultiJson.respond_to?(:dump)
def_delegator MultiJson, :dump, :write
else
def_delegator MultiJson, :encode, :write
def self.write(json)
# MultiJSON <= 1.2.0
if MultiJson.respond_to?(:dump)
MultiJson.dump(json)
else
MultiJson.encode(json)
end
end

def self.handler
Expand All @@ -24,8 +26,13 @@ def self.handler
else
require "json"

def_delegator JSON, :parse, :read
def_delegator JSON, :generate, :write
def self.read(json)
JSON.parse(json)
end

def self.write(json)
JSON.generate(json)
end

def self.handler
:json
Expand Down

0 comments on commit 5034481

Please sign in to comment.