rufus-json 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.txt ADDED
@@ -0,0 +1,6 @@
1
+
2
+ = rufus-json CHANGELOG.txt
3
+
4
+
5
+ == rufus-json - 0.1.0 released 2009/12/25
6
+
data/CREDITS.txt ADDED
@@ -0,0 +1,5 @@
1
+
2
+ == authors
3
+
4
+ John Mettraux - http://github.com/jmettraux
5
+
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+
2
+ Copyright (c) 2009-2010, John Mettraux, jmettraux@gmail.com
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+
data/README.rdoc ADDED
@@ -0,0 +1,79 @@
1
+
2
+ = rufus-json
3
+
4
+ One interface for various JSON Ruby backends.
5
+
6
+ # load your favourite JSON backend
7
+ require 'yajl'
8
+ #require 'json'
9
+ #require 'active_support'
10
+
11
+ p Rufus::Json.decode('{"a":2,"b":true}')
12
+ # => { 'a' => 2, 'b' => true }
13
+
14
+ p Rufus::Json.encode({ 'a' => 2, 'b' => true })
15
+ # => '{"a":2,"b":true}'
16
+
17
+
18
+ If multiple libs are present, it will favour yajl-ruby and json, and then active_support. It's OK to force a backend.
19
+
20
+ Rufus::Json.backend = :yajl
21
+ #Rufus::Json.backend = :json
22
+ #Rufus::Json.backend = :active
23
+
24
+
25
+ To know if there is currently a backend set :
26
+
27
+ Rufus::Json.has_backend?
28
+
29
+
30
+ It's OK to load a lib and force detection :
31
+
32
+ require 'json'
33
+ Rufus::Json.detect_backend
34
+
35
+ p Rufus::Json.backend
36
+ # => :json
37
+
38
+
39
+ There is a dup method, it may be useful in an all JSON system (flattening stuff that will anyway get flattened later).
40
+
41
+ o = Rufus::Json.dup(o)
42
+
43
+
44
+ == rdoc
45
+
46
+ http://rufus.rubyforge.org/rufus-json/
47
+
48
+
49
+ == mailing list
50
+
51
+ On the rufus-ruby list :
52
+
53
+ http://groups.google.com/group/rufus-ruby
54
+
55
+
56
+ == issue tracker
57
+
58
+ http://github.com/jmettraux/rufus-json/issues
59
+
60
+
61
+ == irc
62
+
63
+ irc.freenode.net #ruote
64
+
65
+
66
+ == the rest of Rufus
67
+
68
+ http://rufus.rubyforge.org
69
+
70
+
71
+ == authors
72
+
73
+ * John Mettraux, http://jmettraux.wordpress.com/
74
+
75
+
76
+ == license
77
+
78
+ MIT
79
+
data/Rakefile ADDED
@@ -0,0 +1,83 @@
1
+
2
+
3
+ require 'lib/rufus/json.rb'
4
+
5
+ require 'rubygems'
6
+ require 'rake'
7
+
8
+
9
+ #
10
+ # CLEAN
11
+
12
+ require 'rake/clean'
13
+ CLEAN.include('pkg', 'tmp', 'html')
14
+ task :default => [ :clean ]
15
+
16
+
17
+ #
18
+ # GEM
19
+
20
+ require 'jeweler'
21
+
22
+ Jeweler::Tasks.new do |gem|
23
+
24
+ gem.version = Rufus::Json::VERSION
25
+ gem.name = 'rufus-json'
26
+
27
+ gem.summary = 'One interface to various JSON ruby libs (yajl, json, json_pure, json-jruby, active_support). Has a preference for yajl.'
28
+
29
+ gem.description = %{
30
+ One interface to various JSON ruby libs (yajl, json, json_pure, json-jruby, active_support). Has a preference for yajl.
31
+ }
32
+ gem.email = 'jmettraux@gmail.com'
33
+ gem.homepage = 'http://github.com/jmettraux/rufus-json/'
34
+ gem.authors = [ 'John Mettraux' ]
35
+ gem.rubyforge_project = 'rufus'
36
+
37
+ gem.test_file = 'test/test.rb'
38
+
39
+ gem.add_development_dependency 'json'
40
+ gem.add_development_dependency 'yajl-ruby'
41
+ gem.add_development_dependency 'activesupport'
42
+ #gem.add_development_dependency 'json_pure'
43
+ #gem.add_development_dependency 'json-jruby'
44
+ gem.add_development_dependency 'yard', '>= 0'
45
+
46
+ # gemspec spec : http://www.rubygems.org/read/chapter/20
47
+ end
48
+ Jeweler::GemcutterTasks.new
49
+
50
+
51
+ #
52
+ # DOC
53
+
54
+ begin
55
+
56
+ require 'yard'
57
+
58
+ YARD::Rake::YardocTask.new do |doc|
59
+ doc.options = [
60
+ '-o', 'html/rufus-json', '--title',
61
+ "rufus-json #{Rufus::Json::VERSION}"
62
+ ]
63
+ end
64
+
65
+ rescue LoadError
66
+
67
+ task :yard do
68
+ abort "YARD is not available : sudo gem install yard"
69
+ end
70
+ end
71
+
72
+
73
+ #
74
+ # TO THE WEB
75
+
76
+ task :upload_website => [ :clean, :yard ] do
77
+
78
+ account = 'jmettraux@rubyforge.org'
79
+ webdir = '/var/www/gforge-projects/rufus'
80
+
81
+ sh "rsync -azv -e ssh html/rufus-json #{account}:#{webdir}/"
82
+ end
83
+
data/lib/rufus/json.rb ADDED
@@ -0,0 +1,126 @@
1
+ #--
2
+ # Copyright (c) 2009-2010, John Mettraux, jmettraux@gmail.com
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+ # Made in Japan.
23
+ #++
24
+
25
+
26
+ module Rufus
27
+ module Json
28
+
29
+ VERSION = '0.1.0'
30
+
31
+ # The JSON / JSON pure decoder
32
+ #
33
+ JSON = [
34
+ lambda { |o| o.to_json },
35
+ lambda { |s| ::JSON.parse(s) }
36
+ ]
37
+
38
+ # The Rails ActiveSupport::JSON decoder
39
+ #
40
+ ACTIVE_SUPPORT = [
41
+ lambda { |o| o.to_json },
42
+ lambda { |s| ActiveSupport::JSON.decode(s) }
43
+ ]
44
+ ACTIVE = ACTIVE_SUPPORT
45
+
46
+ # http://github.com/brianmario/yajl-ruby/
47
+ #
48
+ YAJL = [
49
+ lambda { |o| Yajl::Encoder.encode(o) },
50
+ lambda { |s| Yajl::Parser.parse(s) }
51
+ ]
52
+
53
+ # The "raise an exception because there's no backend" backend
54
+ #
55
+ NONE = [ lambda { |s| raise 'no JSON backend found' } ] * 2
56
+
57
+ # [Re-]Attempts to detect a JSON backend
58
+ #
59
+ def self.detect_backend
60
+
61
+ @backend = if defined?(::Yajl)
62
+ YAJL
63
+ elsif defined?(::JSON)
64
+ JSON
65
+ elsif defined?(ActiveSupport::JSON)
66
+ ACTIVE_SUPPORT
67
+ else
68
+ NONE
69
+ end
70
+ end
71
+
72
+ detect_backend
73
+ # run it right now
74
+
75
+ # Returns true if there is a backend set for parsing/encoding JSON
76
+ #
77
+ def self.has_backend?
78
+
79
+ (@backend != NONE)
80
+ end
81
+
82
+ # Returns :yajl|:json|:active|:none (an identifier for the current backend)
83
+ #
84
+ def self.backend
85
+
86
+ %w[ yajl json active none ].find { |b|
87
+ Rufus::Json.const_get(b.upcase) == @backend
88
+ }.to_sym
89
+ end
90
+
91
+ # Forces a decoder JSON/ACTIVE_SUPPORT or any lambda pair that knows
92
+ # how to deal with JSON.
93
+ #
94
+ # It's OK to pass a symbol as well, :yajl, :json, :active (or :none).
95
+ #
96
+ def self.backend= (b)
97
+
98
+ b = { :yajl => YAJL, :json => JSON, :active => ACTIVE, :none => NONE }[b] \
99
+ if b.is_a?(Symbol)
100
+
101
+ @backend = b
102
+ end
103
+
104
+ def self.encode (o)
105
+
106
+ @backend[0].call(o)
107
+ end
108
+
109
+ # Decodes the given JSON string.
110
+ #
111
+ def self.decode (s)
112
+
113
+ @backend[1].call(s)
114
+ end
115
+
116
+ # Duplicates an object by turning it into JSON and back.
117
+ #
118
+ # Don't laugh, yajl-ruby makes that faster than a Marshal copy.
119
+ #
120
+ def self.dup (o)
121
+
122
+ (@backend == NONE) ? Marshal.load(Marshal.dump(o)) : decode(encode(o))
123
+ end
124
+ end
125
+ end
126
+
data/lib/rufus-json.rb ADDED
@@ -0,0 +1,3 @@
1
+
2
+ require 'rufus/json'
3
+
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rufus-json}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["John Mettraux"]
12
+ s.date = %q{2009-12-25}
13
+ s.description = %q{
14
+ One interface to various JSON ruby libs (yajl, json, json_pure, json-jruby, active_support). Has a preference for yajl.
15
+ }
16
+ s.email = %q{jmettraux@gmail.com}
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ "CHANGELOG.txt",
23
+ "CREDITS.txt",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "lib/rufus-json.rb",
28
+ "lib/rufus/json.rb",
29
+ "rufus-json.gemspec",
30
+ "test/test.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/jmettraux/rufus-json/}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubyforge_project = %q{rufus}
36
+ s.rubygems_version = %q{1.3.5}
37
+ s.summary = %q{One interface to various JSON ruby libs (yajl, json, json_pure, json-jruby, active_support). Has a preference for yajl.}
38
+ s.test_files = [
39
+ "test/test.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<json>, [">= 0"])
48
+ s.add_development_dependency(%q<yajl-ruby>, [">= 0"])
49
+ s.add_development_dependency(%q<activesupport>, [">= 0"])
50
+ s.add_development_dependency(%q<yard>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<json>, [">= 0"])
53
+ s.add_dependency(%q<yajl-ruby>, [">= 0"])
54
+ s.add_dependency(%q<activesupport>, [">= 0"])
55
+ s.add_dependency(%q<yard>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<json>, [">= 0"])
59
+ s.add_dependency(%q<yajl-ruby>, [">= 0"])
60
+ s.add_dependency(%q<activesupport>, [">= 0"])
61
+ s.add_dependency(%q<yard>, [">= 0"])
62
+ end
63
+ end
64
+
data/test/test.rb ADDED
@@ -0,0 +1,133 @@
1
+
2
+ #
3
+ # testing rufus-json
4
+ #
5
+ # Fri Jul 31 13:05:37 JST 2009
6
+ #
7
+
8
+ require 'test/unit'
9
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+ require 'rufus/json'
11
+ require 'rubygems'
12
+
13
+
14
+ class JsonTest < Test::Unit::TestCase
15
+
16
+ def setup
17
+ Rufus::Json.backend = Rufus::Json::NONE
18
+ end
19
+ #def teardown
20
+ #end
21
+
22
+ def test_no_backend
23
+
24
+ assert_raise RuntimeError do
25
+ Rufus::Json.decode('nada')
26
+ end
27
+ end
28
+
29
+ def test_get_backend
30
+
31
+ assert_equal :none, Rufus::Json.backend
32
+
33
+ require 'json'
34
+
35
+ Rufus::Json.detect_backend
36
+
37
+ assert_not_equal :none, Rufus::Json.backend
38
+ end
39
+
40
+ def test_set_backend
41
+
42
+ require 'json'
43
+
44
+ Rufus::Json.backend = :json
45
+
46
+ assert_equal :json, Rufus::Json.backend
47
+ end
48
+
49
+ def test_decode_json
50
+
51
+ do_test_decode('json', Rufus::Json::JSON)
52
+ end
53
+
54
+ def test_encode_json
55
+
56
+ do_test_encode('json', Rufus::Json::JSON)
57
+ end
58
+
59
+ def test_dup_json
60
+
61
+ do_test_dup('json', Rufus::Json::JSON)
62
+ end
63
+
64
+ def test_decode_yajl
65
+
66
+ do_test_decode('yajl', Rufus::Json::YAJL)
67
+ end
68
+
69
+ def test_encode_yajl
70
+
71
+ do_test_encode('yajl', Rufus::Json::YAJL)
72
+ end
73
+
74
+ def test_dup_yajl
75
+
76
+ do_test_dup('yajl', Rufus::Json::YAJL)
77
+ end
78
+
79
+ def test_decode_as
80
+
81
+ do_test_decode('active_support', Rufus::Json::ACTIVE)
82
+ end
83
+
84
+ def test_encode_as
85
+
86
+ do_test_encode('active_support', Rufus::Json::ACTIVE)
87
+ end
88
+
89
+ def test_dup_as
90
+
91
+ do_test_dup('active_support', Rufus::Json::ACTIVE)
92
+ end
93
+
94
+ protected
95
+
96
+ def do_test_decode (lib, cons)
97
+
98
+ require lib
99
+
100
+ assert_raise RuntimeError do
101
+ Rufus::Json.decode('nada')
102
+ end
103
+
104
+ Rufus::Json.backend = cons
105
+ assert_equal [ 1, 2, 3 ], Rufus::Json.decode("[ 1, 2, 3 ]")
106
+ end
107
+
108
+ def do_test_encode (lib, cons)
109
+
110
+ require lib
111
+
112
+ assert_raise RuntimeError do
113
+ Rufus::Json.encode('nada')
114
+ end
115
+
116
+ Rufus::Json.backend = cons
117
+ assert_equal "[1,2,3]", Rufus::Json.encode([ 1, 2, 3 ])
118
+ end
119
+
120
+ def do_test_dup (lib, cons)
121
+
122
+ require lib
123
+
124
+ d0 = { 'id' => 'nada' }
125
+ d1 = { :id => :nada }
126
+
127
+ Rufus::Json.backend = cons
128
+
129
+ assert_equal({ 'id' => 'nada' }, Rufus::Json.dup(d0))
130
+ assert_equal({ 'id' => 'nada' }, Rufus::Json.dup(d1))
131
+ end
132
+ end
133
+
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rufus-json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Mettraux
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-25 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: yajl-ruby
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: activesupport
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: yard
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ description: "\n\
56
+ One interface to various JSON ruby libs (yajl, json, json_pure, json-jruby, active_support). Has a preference for yajl.\n "
57
+ email: jmettraux@gmail.com
58
+ executables: []
59
+
60
+ extensions: []
61
+
62
+ extra_rdoc_files:
63
+ - LICENSE.txt
64
+ - README.rdoc
65
+ files:
66
+ - CHANGELOG.txt
67
+ - CREDITS.txt
68
+ - LICENSE.txt
69
+ - README.rdoc
70
+ - Rakefile
71
+ - lib/rufus-json.rb
72
+ - lib/rufus/json.rb
73
+ - rufus-json.gemspec
74
+ - test/test.rb
75
+ has_rdoc: true
76
+ homepage: http://github.com/jmettraux/rufus-json/
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --charset=UTF-8
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ requirements: []
97
+
98
+ rubyforge_project: rufus
99
+ rubygems_version: 1.3.5
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: One interface to various JSON ruby libs (yajl, json, json_pure, json-jruby, active_support). Has a preference for yajl.
103
+ test_files:
104
+ - test/test.rb