Skip to content

Commit 6ab56d0

Browse files
committed
Add gem compat
1 parent 821fe42 commit 6ab56d0

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

lib/rapidjson/json_gem.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module ::Kernel
2+
def JSON(object, opts = {})
3+
if object.respond_to? :to_s
4+
JSON.parse(object.to_s)
5+
else
6+
JSON.generate(object, opts)
7+
end
8+
end
9+
end
10+
11+
module JSON
12+
def self.generate(obj, opts=nil)
13+
RapidJSON.encode(obj)
14+
end
15+
16+
def self.pretty_generate(obj, opts=nil)
17+
RapidJSON.pretty_encode(obj)
18+
end
19+
20+
def self.dump(obj)
21+
RapidJSON.encode(obj)
22+
end
23+
end
24+
25+
# to_json
26+
module RapidJSON
27+
module JSONGemCompact
28+
def to_json(opts=nil)
29+
RapidJSON.encode(self)
30+
end
31+
end
32+
end
33+
34+
[Hash, Array, String, Integer, Float, TrueClass, FalseClass, NilClass].each do |klass|
35+
klass.include RapidJSON::JSONGemCompact
36+
end

0 commit comments

Comments
 (0)