rufus-json 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.txt +3 -0
- data/LICENSE.txt +1 -1
- data/README.rdoc +1 -0
- data/Rakefile +1 -1
- data/lib/rufus/json.rb +46 -34
- data/lib/rufus-json/automatic.rb +1 -1
- data/rufus-json.gemspec +6 -7
- data/test/test.rb +6 -1
- metadata +6 -8
data/CHANGELOG.txt
CHANGED
data/LICENSE.txt
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
|
2
|
-
Copyright (c) 2009-
|
2
|
+
Copyright (c) 2009-2012, John Mettraux, jmettraux@gmail.com
|
3
3
|
|
4
4
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
5
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.rdoc
CHANGED
data/Rakefile
CHANGED
data/lib/rufus/json.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c) 2009-
|
2
|
+
# Copyright (c) 2009-2012, John Mettraux, jmettraux@gmail.com
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
5
|
# of this software and associated documentation files (the "Software"), to deal
|
@@ -22,16 +22,18 @@
|
|
22
22
|
# Made in Japan.
|
23
23
|
#++
|
24
24
|
|
25
|
+
require 'ostruct'
|
26
|
+
|
25
27
|
|
26
28
|
module Rufus
|
27
29
|
module Json
|
28
30
|
|
29
|
-
VERSION = '1.0.
|
31
|
+
VERSION = '1.0.2'
|
30
32
|
|
31
33
|
# The JSON / JSON pure decoder
|
32
34
|
#
|
33
|
-
JSON =
|
34
|
-
lambda { |o, opts|
|
35
|
+
JSON = OpenStruct.new(
|
36
|
+
:encode => lambda { |o, opts|
|
35
37
|
opts[:max_nesting] = false unless opts.has_key?(:max_nesting)
|
36
38
|
if o.is_a?(Hash) or o.is_a?(Array)
|
37
39
|
::JSON.generate(o, opts)
|
@@ -39,35 +41,52 @@ module Json
|
|
39
41
|
::JSON.generate([ o ], opts).strip[1..-2]
|
40
42
|
end
|
41
43
|
},
|
42
|
-
lambda { |
|
43
|
-
|
44
|
-
|
44
|
+
:pretty_encode => lambda { |o|
|
45
|
+
encode(
|
46
|
+
o,
|
47
|
+
:indent => ' ', :object_nl => "\n", :array_nl => "\n", :space => ' ')
|
48
|
+
},
|
49
|
+
:decode => lambda { |s|
|
50
|
+
::JSON.parse("[#{s}]", :max_nesting => nil).first },
|
51
|
+
:error => lambda {
|
52
|
+
::JSON::ParserError }
|
53
|
+
)
|
45
54
|
|
46
55
|
# The Rails ActiveSupport::JSON decoder
|
47
56
|
#
|
48
|
-
ACTIVE_SUPPORT =
|
49
|
-
lambda { |o, opts|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
57
|
+
ACTIVE_SUPPORT = OpenStruct.new(
|
58
|
+
:encode => lambda { |o, opts|
|
59
|
+
ActiveSupport::JSON.encode(o, opts) },
|
60
|
+
:pretty_encode => lambda { |o|
|
61
|
+
ActiveSupport::JSON.encode(o) },
|
62
|
+
:decode => lambda { |s|
|
63
|
+
decode_e(s) || ActiveSupport::JSON.decode(s) },
|
64
|
+
:error => lambda {
|
65
|
+
RuntimeError }
|
66
|
+
)
|
54
67
|
ACTIVE = ACTIVE_SUPPORT
|
55
68
|
|
56
69
|
# http://github.com/brianmario/yajl-ruby/
|
57
70
|
#
|
58
|
-
YAJL =
|
59
|
-
lambda { |o, opts|
|
60
|
-
|
61
|
-
lambda {
|
62
|
-
|
71
|
+
YAJL = OpenStruct.new(
|
72
|
+
:encode => lambda { |o, opts|
|
73
|
+
Yajl::Encoder.encode(o, opts) },
|
74
|
+
:pretty_encode => lambda { |o|
|
75
|
+
Yajl::Encoder.encode(o, :pretty => true, :indent => ' ') },
|
76
|
+
:decode => lambda { |s|
|
77
|
+
Yajl::Parser.parse(s) },
|
78
|
+
:error => lambda {
|
79
|
+
::Yajl::ParseError }
|
80
|
+
)
|
63
81
|
|
64
82
|
# The "raise an exception because there's no backend" backend
|
65
83
|
#
|
66
|
-
NONE =
|
67
|
-
lambda { |o, opts| raise 'no JSON backend found' },
|
68
|
-
lambda { |
|
69
|
-
lambda { raise 'no JSON backend found' }
|
70
|
-
|
84
|
+
NONE = OpenStruct.new(
|
85
|
+
:encode => lambda { |o, opts| raise 'no JSON backend found' },
|
86
|
+
:pretty_encode => lambda { |o| raise 'no JSON backend found' },
|
87
|
+
:decode => lambda { |s| raise 'no JSON backend found' },
|
88
|
+
:error => lambda { raise 'no JSON backend found' }
|
89
|
+
)
|
71
90
|
|
72
91
|
# In the given order, attempts to load a json lib and sets it as the
|
73
92
|
# backend of rufus-json.
|
@@ -151,21 +170,14 @@ module Json
|
|
151
170
|
#
|
152
171
|
def self.encode(o, opts={})
|
153
172
|
|
154
|
-
@backend[
|
173
|
+
@backend.encode[o, opts]
|
155
174
|
end
|
156
175
|
|
157
176
|
# Pretty encoding
|
158
177
|
#
|
159
178
|
def self.pretty_encode(o)
|
160
179
|
|
161
|
-
|
162
|
-
when JSON
|
163
|
-
encode(o, :indent => ' ', :object_nl => "\n", :array_nl => "\n", :space => ' ')
|
164
|
-
when YAJL
|
165
|
-
encode(o, :pretty => true, :indent => ' ')
|
166
|
-
else
|
167
|
-
encode(o)
|
168
|
-
end
|
180
|
+
@backend.pretty_encode[o]
|
169
181
|
end
|
170
182
|
|
171
183
|
# An alias for .encode
|
@@ -179,9 +191,9 @@ module Json
|
|
179
191
|
#
|
180
192
|
def self.decode(s)
|
181
193
|
|
182
|
-
@backend[
|
194
|
+
@backend.decode[s]
|
183
195
|
|
184
|
-
rescue @backend[
|
196
|
+
rescue @backend.error[] => e
|
185
197
|
raise ParserError.new(e.message)
|
186
198
|
end
|
187
199
|
|
data/lib/rufus-json/automatic.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c) 2009-
|
2
|
+
# Copyright (c) 2009-2012, John Mettraux, jmettraux@gmail.com
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
5
|
# of this software and associated documentation files (the "Software"), to deal
|
data/rufus-json.gemspec
CHANGED
@@ -1,19 +1,18 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require File.join(File.dirname(__FILE__), 'lib/rufus/json')
|
4
|
-
# bundler wants absolute path
|
5
|
-
|
6
1
|
|
7
2
|
Gem::Specification.new do |s|
|
8
3
|
|
9
4
|
s.name = 'rufus-json'
|
10
|
-
|
5
|
+
|
6
|
+
s.version = File.read(
|
7
|
+
File.expand_path('../lib/rufus/json.rb', __FILE__)
|
8
|
+
).match(/ VERSION *= *['"]([^'"]+)/)[1]
|
9
|
+
|
11
10
|
s.platform = Gem::Platform::RUBY
|
12
11
|
s.authors = [ 'John Mettraux', 'Torsten Schoenebaum' ]
|
13
12
|
s.email = [ 'jmettraux@gmail.com' ]
|
14
13
|
s.homepage = 'http://github.com/jmettraux/rufus-json'
|
15
14
|
s.rubyforge_project = 'rufus'
|
16
|
-
s.summary = 'One interface to various JSON ruby libs
|
15
|
+
s.summary = 'One interface to various JSON ruby libs, with a preference for yajl.'
|
17
16
|
|
18
17
|
s.description = %{
|
19
18
|
One interface to various JSON ruby libs (yajl, json, json_pure, json-jruby, active_support). Has a preference for yajl.
|
data/test/test.rb
CHANGED
@@ -21,7 +21,10 @@ def do_test(command)
|
|
21
21
|
$result << ($?.exitstatus == 0 ? 'o' : 'X')
|
22
22
|
end
|
23
23
|
|
24
|
-
%w[ json active_support
|
24
|
+
LIBS = %w[ json active_support json/pure ]
|
25
|
+
LIBS << 'yajl' if RUBY_PLATFORM != 'java'
|
26
|
+
|
27
|
+
LIBS.each do |lib|
|
25
28
|
do_test "#{R} #{P}/do_test.rb #{lib}"
|
26
29
|
end
|
27
30
|
|
@@ -29,6 +32,8 @@ do_test "#{R} #{P}/backend_test.rb"
|
|
29
32
|
|
30
33
|
puts
|
31
34
|
puts
|
35
|
+
puts '-' * 80
|
32
36
|
puts $result
|
37
|
+
puts '-' * 80
|
33
38
|
puts
|
34
39
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rufus-json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 2
|
10
|
+
version: 1.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- John Mettraux
|
@@ -16,8 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date:
|
20
|
-
default_executable:
|
19
|
+
date: 2012-05-24 00:00:00 Z
|
21
20
|
dependencies:
|
22
21
|
- !ruby/object:Gem::Dependency
|
23
22
|
name: json
|
@@ -113,7 +112,6 @@ files:
|
|
113
112
|
- CREDITS.txt
|
114
113
|
- LICENSE.txt
|
115
114
|
- README.rdoc
|
116
|
-
has_rdoc: true
|
117
115
|
homepage: http://github.com/jmettraux/rufus-json
|
118
116
|
licenses: []
|
119
117
|
|
@@ -143,9 +141,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
143
141
|
requirements: []
|
144
142
|
|
145
143
|
rubyforge_project: rufus
|
146
|
-
rubygems_version: 1.
|
144
|
+
rubygems_version: 1.8.15
|
147
145
|
signing_key:
|
148
146
|
specification_version: 3
|
149
|
-
summary: One interface to various JSON ruby libs
|
147
|
+
summary: One interface to various JSON ruby libs, with a preference for yajl.
|
150
148
|
test_files: []
|
151
149
|
|