x_to_json 0.0.1
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/.document +5 -0
- data/.gitignore +22 -0
- data/Gemfile +8 -0
- data/LICENSE +20 -0
- data/README.rdoc +28 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/x_to_json.rb +53 -0
- data/lib/x_to_json/engines/active_support.rb +31 -0
- data/lib/x_to_json/engines/json_gem.rb +17 -0
- data/lib/x_to_json/engines/json_pure.rb +17 -0
- data/lib/x_to_json/engines/yajl.rb +15 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/x_to_json_spec.rb +59 -0
- data/x_to_json.gemspec +60 -0
- metadata +92 -0
data/.document
ADDED
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Michael Bleigh and Intridea, Inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
= Use X to JSON
|
2
|
+
|
3
|
+
Lots of Ruby libraries utilize JSON parsing in some form, and everyone has their favorite JSON library. In order to best support multiple JSON parsers and libraries, <tt>x_to_json</tt> is a general-purpose swappable JSON backend library. You use it like so:
|
4
|
+
|
5
|
+
require 'x_to_json'
|
6
|
+
|
7
|
+
XToJson.engine = :yajl
|
8
|
+
XToJson.decode('{"abc":"def"}') # decoded using Yajl
|
9
|
+
|
10
|
+
XToJson.engine = :json_gem
|
11
|
+
XToJson.engine = XToJson::Engines::JsonGem # equivalent to previous line
|
12
|
+
XToJson.encode({:abc => 'def'}) # encoded using the JSON gem
|
13
|
+
|
14
|
+
The <tt>engine</tt> setter takes either a symbol or a class (to allow for custom JSON parsers) that responds to both <tt>.decode</tt> and <tt>.encode</tt> at the class level.
|
15
|
+
|
16
|
+
== Note on Patches/Pull Requests
|
17
|
+
|
18
|
+
* Fork the project.
|
19
|
+
* Make your feature addition or bug fix.
|
20
|
+
* Add tests for it. This is important so I don't break it in a
|
21
|
+
future version unintentionally.
|
22
|
+
* Commit, do not mess with rakefile, version, or history.
|
23
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
24
|
+
* Send me a pull request. Bonus points for topic branches.
|
25
|
+
|
26
|
+
== Copyright
|
27
|
+
|
28
|
+
Copyright (c) 2010 Michael Bleigh and Intridea, Inc. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "x_to_json"
|
8
|
+
gem.summary = %Q{A gem to provide swappable JSON backends.}
|
9
|
+
gem.description = %Q{A gem to provide swappable JSON backends utilizing Yajl::Ruby, the JSON gem, ActiveSupport, or JSON pure.}
|
10
|
+
gem.email = "michael@intridea.com"
|
11
|
+
gem.homepage = "http://github.com/intridea/x_to_json"
|
12
|
+
gem.authors = ["Michael Bleigh"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "x_to_json #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/x_to_json.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
module XToJson
|
2
|
+
module_function
|
3
|
+
|
4
|
+
# Get the current engine class.
|
5
|
+
def engine
|
6
|
+
return @engine if @engine
|
7
|
+
self.engine = self.default_engine
|
8
|
+
@engine
|
9
|
+
end
|
10
|
+
|
11
|
+
# The default engine based on what you currently
|
12
|
+
# have loaded. Tries Yajl first, then JSON gem,
|
13
|
+
# then ActiveSupport and JSON pure.
|
14
|
+
def default_engine
|
15
|
+
return :yajl if defined?(::Yajl)
|
16
|
+
return :json_gem if defined?(::JSON)
|
17
|
+
return :active_support if defined?(::ActiveSupport::JSON)
|
18
|
+
:json_pure
|
19
|
+
end
|
20
|
+
|
21
|
+
# Set the JSON parser utilizing a symbol, string, or class.
|
22
|
+
# Supported by default are:
|
23
|
+
#
|
24
|
+
# * <tt>:json_gem</tt>
|
25
|
+
# * <tt>:json_pure</tt>
|
26
|
+
# * <tt>:active_support</tt> (useful for inside Rails apps)
|
27
|
+
# * <tt>:yajl</tt>
|
28
|
+
def engine=(new_engine)
|
29
|
+
case new_engine
|
30
|
+
when String, Symbol
|
31
|
+
require "x_to_json/engines/#{new_engine}"
|
32
|
+
@engine = XToJson::Engines.const_get("#{new_engine.to_s.split('_').map{|s| s.capitalize}.join('')}")
|
33
|
+
when Class
|
34
|
+
@engine = new_engine
|
35
|
+
else
|
36
|
+
raise "Did not recognize your engine specification. Please specify either a symbol or a class."
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Decode a JSON string into Ruby.
|
41
|
+
#
|
42
|
+
# <b>Options</b>
|
43
|
+
#
|
44
|
+
# <tt>:symbolize_keys</tt> :: If true, will use symbols instead of strings for the keys.
|
45
|
+
def decode(string, options = {})
|
46
|
+
engine.decode(string, options)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Encodes a Ruby object as JSON.
|
50
|
+
def encode(object)
|
51
|
+
engine.encode(object)
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'active_support' unless defined?(::ActiveSupport::JSON)
|
2
|
+
|
3
|
+
module XToJson
|
4
|
+
module Engines
|
5
|
+
class ActiveSupport
|
6
|
+
def self.decode(string, options = {})
|
7
|
+
hash = ::ActiveSupport::JSON.decode(string)
|
8
|
+
options[:symbolize_keys] ? symbolize_keys(hash) : hash
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.encode(object)
|
12
|
+
::ActiveSupport::JSON.encode(object)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.symbolize_keys(hash)
|
16
|
+
hash.inject({}){|result, (key, value)|
|
17
|
+
new_key = case key
|
18
|
+
when String then key.to_sym
|
19
|
+
else key
|
20
|
+
end
|
21
|
+
new_value = case value
|
22
|
+
when Hash then symbolize_keys(value)
|
23
|
+
else value
|
24
|
+
end
|
25
|
+
result[new_key] = new_value
|
26
|
+
result
|
27
|
+
}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'json' unless defined?(::JSON)
|
2
|
+
|
3
|
+
module XToJson
|
4
|
+
module Engines
|
5
|
+
class JsonGem
|
6
|
+
def self.decode(string, options = {})
|
7
|
+
opts = {}
|
8
|
+
opts[:symbolize_names] = options[:symbolize_keys]
|
9
|
+
::JSON.parse(string, opts)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.encode(object)
|
13
|
+
object.to_json
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'json/pure' unless defined?(::JSON)
|
2
|
+
|
3
|
+
module XToJson
|
4
|
+
module Engines
|
5
|
+
class JsonPure
|
6
|
+
def self.decode(string, options = {})
|
7
|
+
opts = {}
|
8
|
+
opts[:symbolize_names] = options[:symbolize_keys]
|
9
|
+
::JSON.parse(string, opts)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.encode(object)
|
13
|
+
object.to_json
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'yajl' unless defined?(Yajl)
|
2
|
+
|
3
|
+
module XToJson
|
4
|
+
module Engines
|
5
|
+
class Yajl
|
6
|
+
def self.decode(string, options = {})
|
7
|
+
::Yajl::Parser.new(:symbolize_keys => options[:symbolize_keys]).parse(string)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.encode(object)
|
11
|
+
::Yajl::Encoder.new.encode(object)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'x_to_json'
|
4
|
+
require 'spec'
|
5
|
+
require 'spec/autorun'
|
6
|
+
require 'rubygems'
|
7
|
+
require 'bundler'
|
8
|
+
Bundler.setup
|
9
|
+
|
10
|
+
Spec::Runner.configure do |config|
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class MockDecoder
|
4
|
+
def self.decode(string, options = {})
|
5
|
+
{'abc' => 'def'}
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.encode(string)
|
9
|
+
'{"abc":"def"}'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "XToJson" do
|
14
|
+
context 'engines' do
|
15
|
+
it 'should default to the best available gem' do
|
16
|
+
require 'yajl'
|
17
|
+
XToJson.engine.name.should == 'XToJson::Engines::Yajl'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should be settable via a symbol' do
|
21
|
+
XToJson.engine = :yajl
|
22
|
+
XToJson.engine.name.should == 'XToJson::Engines::Yajl'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should be settable via a class' do
|
26
|
+
XToJson.engine = MockDecoder
|
27
|
+
XToJson.engine.name.should == 'MockDecoder'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
%w(active_support json_gem json_pure yajl).each do |engine|
|
32
|
+
context engine do
|
33
|
+
before do
|
34
|
+
XToJson.engine = engine
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '.encode' do
|
38
|
+
it 'should write decodable JSON' do
|
39
|
+
[
|
40
|
+
{'abc' => 'def'},
|
41
|
+
[1, 2, 3, "4"]
|
42
|
+
].each do |example|
|
43
|
+
XToJson.decode(XToJson.encode(example)).should == example
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '.decode' do
|
49
|
+
it 'should properly decode some json' do
|
50
|
+
XToJson.decode('{"abc":"def"}').should == {'abc' => 'def'}
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should allow for symbolization of keys' do
|
54
|
+
XToJson.decode('{"abc":{"def":"hgi"}}', :symbolize_keys => true).should == {:abc => {:def => 'hgi'}}
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/x_to_json.gemspec
ADDED
@@ -0,0 +1,60 @@
|
|
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{x_to_json}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Michael Bleigh"]
|
12
|
+
s.date = %q{2010-06-13}
|
13
|
+
s.description = %q{A gem to provide swappable JSON backends utilizing Yajl::Ruby, the JSON gem, ActiveSupport, or JSON pure.}
|
14
|
+
s.email = %q{michael@intridea.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/x_to_json.rb",
|
28
|
+
"lib/x_to_json/engines/active_support.rb",
|
29
|
+
"lib/x_to_json/engines/json_gem.rb",
|
30
|
+
"lib/x_to_json/engines/json_pure.rb",
|
31
|
+
"lib/x_to_json/engines/yajl.rb",
|
32
|
+
"spec/spec.opts",
|
33
|
+
"spec/spec_helper.rb",
|
34
|
+
"spec/x_to_json_spec.rb",
|
35
|
+
"x_to_json.gemspec"
|
36
|
+
]
|
37
|
+
s.homepage = %q{http://github.com/intridea/x_to_json}
|
38
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = %q{1.3.6}
|
41
|
+
s.summary = %q{A gem to provide swappable JSON backends.}
|
42
|
+
s.test_files = [
|
43
|
+
"spec/spec_helper.rb",
|
44
|
+
"spec/x_to_json_spec.rb"
|
45
|
+
]
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: x_to_json
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Michael Bleigh
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-13 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: A gem to provide swappable JSON backends utilizing Yajl::Ruby, the JSON gem, ActiveSupport, or JSON pure.
|
35
|
+
email: michael@intridea.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- LICENSE
|
42
|
+
- README.rdoc
|
43
|
+
files:
|
44
|
+
- .document
|
45
|
+
- .gitignore
|
46
|
+
- Gemfile
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/x_to_json.rb
|
52
|
+
- lib/x_to_json/engines/active_support.rb
|
53
|
+
- lib/x_to_json/engines/json_gem.rb
|
54
|
+
- lib/x_to_json/engines/json_pure.rb
|
55
|
+
- lib/x_to_json/engines/yajl.rb
|
56
|
+
- spec/spec.opts
|
57
|
+
- spec/spec_helper.rb
|
58
|
+
- spec/x_to_json_spec.rb
|
59
|
+
- x_to_json.gemspec
|
60
|
+
has_rdoc: true
|
61
|
+
homepage: http://github.com/intridea/x_to_json
|
62
|
+
licenses: []
|
63
|
+
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options:
|
66
|
+
- --charset=UTF-8
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.3.6
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: A gem to provide swappable JSON backends.
|
90
|
+
test_files:
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
- spec/x_to_json_spec.rb
|