jsonable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +33 -0
- data/Rakefile +28 -0
- data/VERSION.yml +5 -0
- data/jsonable.gemspec +44 -0
- data/lib/jsonable.rb +45 -0
- data/test/Gemfile +4 -0
- data/test/my_class.rb +9 -0
- data/test/test_base.rb +19 -0
- data/test/test_json_class.rb +32 -0
- metadata +54 -0
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
Makes your json classes serializable.
|
2
|
+
|
3
|
+
Getting Started
|
4
|
+
==============
|
5
|
+
|
6
|
+
Install the gem:
|
7
|
+
|
8
|
+
sudo gem install jsonable
|
9
|
+
|
10
|
+
Include Jsonable in your classes:
|
11
|
+
|
12
|
+
require 'jsonable'
|
13
|
+
class MyClass
|
14
|
+
include Jsonable
|
15
|
+
....
|
16
|
+
|
17
|
+
Then you can serialize and deserialize:
|
18
|
+
|
19
|
+
c = MyClass.new
|
20
|
+
c.x = "abc"
|
21
|
+
c.y = 123
|
22
|
+
|
23
|
+
# Serialize to json
|
24
|
+
json = c.to_json
|
25
|
+
|
26
|
+
# Deserialize back to object
|
27
|
+
c2 = JSON.parse(json)
|
28
|
+
|
29
|
+
# now this holds true:
|
30
|
+
assert_equal c2.class.name, c.class.name
|
31
|
+
assert_equal c2.x, c.x
|
32
|
+
assert_equal c2.y, c.y
|
33
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler2'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "jsonable"
|
8
|
+
gem.summary = "Makes your json classes serializable."
|
9
|
+
gem.description = "Makes your json classes serializable."
|
10
|
+
gem.email = "travis@iron.io"
|
11
|
+
gem.homepage = "http://www.iron.io"
|
12
|
+
gem.authors = ["Travis Reeder"]
|
13
|
+
#gem.add_dependency 'rest', '>= 0.1.2'
|
14
|
+
gem.required_ruby_version = '>= 1.9'
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler2 (or a dependency) not available. Install it with: gem install jeweler2"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
task :default => :test
|
data/VERSION.yml
ADDED
data/jsonable.gemspec
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "jsonable"
|
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 = ["Travis Reeder"]
|
12
|
+
s.date = "2012-03-27"
|
13
|
+
s.description = "Makes your json classes serializable."
|
14
|
+
s.email = "travis@iron.io"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.md"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"README.md",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION.yml",
|
22
|
+
"jsonable.gemspec",
|
23
|
+
"lib/jsonable.rb",
|
24
|
+
"test/Gemfile",
|
25
|
+
"test/my_class.rb",
|
26
|
+
"test/test_base.rb",
|
27
|
+
"test/test_json_class.rb"
|
28
|
+
]
|
29
|
+
s.homepage = "http://www.iron.io"
|
30
|
+
s.require_paths = ["lib"]
|
31
|
+
s.required_ruby_version = Gem::Requirement.new(">= 1.9")
|
32
|
+
s.rubygems_version = "1.8.21"
|
33
|
+
s.summary = "Makes your json classes serializable."
|
34
|
+
|
35
|
+
if s.respond_to? :specification_version then
|
36
|
+
s.specification_version = 3
|
37
|
+
|
38
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
39
|
+
else
|
40
|
+
end
|
41
|
+
else
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
data/lib/jsonable.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'json'
|
2
|
+
module Jsonable
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def json_create(object)
|
11
|
+
puts 'object=' + object.inspect
|
12
|
+
obj = new
|
13
|
+
object.each_pair do |key, value|
|
14
|
+
next if key == 'json_class'
|
15
|
+
#puts "setting #{key}: #{value}"
|
16
|
+
obj.instance_variable_set key, value
|
17
|
+
#puts "x = #{obj.x}"
|
18
|
+
end
|
19
|
+
obj
|
20
|
+
end
|
21
|
+
|
22
|
+
def from_json(json_string)
|
23
|
+
return JSON.parse(json_string)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
def from_json!(string)
|
29
|
+
JSON.parse(string).each do |var, val|
|
30
|
+
self.instance_variable_set var, val
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_json(options={})
|
35
|
+
puts 'SimpleRecord as_json called with options: ' + options.inspect
|
36
|
+
result = {}
|
37
|
+
result['json_class'] = self.class.name unless options && options[:exclude_json_class]
|
38
|
+
|
39
|
+
self.instance_variables.each do |var|
|
40
|
+
result[var] = self.instance_variable_get var
|
41
|
+
end
|
42
|
+
result.to_json
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/test/Gemfile
ADDED
data/test/my_class.rb
ADDED
data/test/test_base.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
gem 'test-unit'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require File.join(File.dirname(__FILE__), '../lib/jsonable')
|
7
|
+
rescue Exception => ex
|
8
|
+
puts "Could NOT load current iron_cache: " + ex.message
|
9
|
+
raise ex
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
class TestBase < Test::Unit::TestCase
|
14
|
+
|
15
|
+
def setup
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
gem 'test-unit'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'yaml'
|
4
|
+
require_relative 'test_base'
|
5
|
+
require_relative 'my_class'
|
6
|
+
|
7
|
+
class IronCacheTests < TestBase
|
8
|
+
def setup
|
9
|
+
super
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_basics
|
14
|
+
c = MyClass.new
|
15
|
+
c.x = "abc"
|
16
|
+
c.y = 123
|
17
|
+
|
18
|
+
json = c.to_json
|
19
|
+
p json
|
20
|
+
|
21
|
+
c2 = JSON.parse(json)
|
22
|
+
p c2
|
23
|
+
|
24
|
+
assert_equal c2.class.name, c.class.name
|
25
|
+
assert_equal c2.x, c.x
|
26
|
+
assert_equal c2.y, c.y
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
end
|
32
|
+
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsonable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Travis Reeder
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-27 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Makes your json classes serializable.
|
15
|
+
email: travis@iron.io
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files:
|
19
|
+
- README.md
|
20
|
+
files:
|
21
|
+
- README.md
|
22
|
+
- Rakefile
|
23
|
+
- VERSION.yml
|
24
|
+
- jsonable.gemspec
|
25
|
+
- lib/jsonable.rb
|
26
|
+
- test/Gemfile
|
27
|
+
- test/my_class.rb
|
28
|
+
- test/test_base.rb
|
29
|
+
- test/test_json_class.rb
|
30
|
+
homepage: http://www.iron.io
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.9'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.21
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Makes your json classes serializable.
|
54
|
+
test_files: []
|