rackson 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +100 -0
- data/Rakefile +2 -0
- data/lib/rackson.rb +19 -0
- data/lib/rackson/object_mapper.rb +54 -0
- data/lib/rackson/property.rb +15 -0
- data/lib/rackson/version.rb +3 -0
- data/rackson.gemspec +25 -0
- data/spec/rackson/object_mapper_spec.rb +64 -0
- metadata +123 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Dylan Griffin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# Rackson
|
2
|
+
|
3
|
+
Rackson serves to turn JSON into ruby objects. Where Rackson differs from other
|
4
|
+
libraries is that the classes are expected to be already defined so that if you
|
5
|
+
use the wrong input or try to call a method that doesn't exist it will raise an
|
6
|
+
exception as soon as possible
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'rackson'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install rackson
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Let's just start with a very simple example
|
27
|
+
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class Foo
|
31
|
+
include Rackson
|
32
|
+
json_property :bar, String
|
33
|
+
end
|
34
|
+
|
35
|
+
mapper = Rackson::ObjectMapper.new
|
36
|
+
foo = mapper.deserialize('{ "bar": "value" }', Foo)
|
37
|
+
foo.bar
|
38
|
+
# 'value'
|
39
|
+
```
|
40
|
+
|
41
|
+
As you can see after including `Rackson` the class now has access to a
|
42
|
+
`json_property` method which defines a new property. The first argument is the
|
43
|
+
key, the second is the type. This way if you pass something *not* a string you
|
44
|
+
will get an exception when trying to deserialize instead of later. The class can
|
45
|
+
be whatever you want:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class Inner
|
49
|
+
include Rackson
|
50
|
+
json_property :foo, String
|
51
|
+
end
|
52
|
+
|
53
|
+
class Outer
|
54
|
+
include Rackson
|
55
|
+
json_property :inner, Inner
|
56
|
+
end
|
57
|
+
|
58
|
+
mapper = Rackson::ObjectMapper.new
|
59
|
+
outer = mapper.deserialize('{ "outer": { "foo": "bar" }}', Outer)
|
60
|
+
outer.inner.foo
|
61
|
+
# 'bar'
|
62
|
+
```
|
63
|
+
|
64
|
+
Unless specified otherwise, all properties are required:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class Foo
|
68
|
+
include Rackson
|
69
|
+
json_property :foo, String
|
70
|
+
json_property :bar, String
|
71
|
+
end
|
72
|
+
|
73
|
+
mapper = Rackson::ObjectMapper.new
|
74
|
+
outer = mapper.deserialize('{ "foo": "this is foo" }', Foo)
|
75
|
+
# RuntimeError: missing required key bar
|
76
|
+
```
|
77
|
+
|
78
|
+
However, properties are easily marked as optional:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
class Foo
|
82
|
+
include Rackson
|
83
|
+
json_property :foo, String
|
84
|
+
json_property :bar, String, optional: true
|
85
|
+
end
|
86
|
+
|
87
|
+
mapper = Rackson::ObjectMapper.new
|
88
|
+
outer = mapper.deserialize('{ "foo": "this is foo" }', Foo)
|
89
|
+
outer.bar
|
90
|
+
# nil
|
91
|
+
```
|
92
|
+
|
93
|
+
|
94
|
+
## Contributing
|
95
|
+
|
96
|
+
1. Fork it ( https://github.com/griffindy/rackson/fork )
|
97
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
98
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
99
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
100
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/rackson.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rackson/property'
|
2
|
+
require 'rackson/version'
|
3
|
+
|
4
|
+
module Rackson
|
5
|
+
def self.included(base)
|
6
|
+
base.instance_variable_set(:@json_properties, [])
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def json_property(name, klass, options = {})
|
12
|
+
property = Rackson::Property.new(name, klass, options)
|
13
|
+
@json_properties << property
|
14
|
+
define_method(property.name) do
|
15
|
+
instance_variable_get("@#{property.name}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Rackson
|
4
|
+
class ObjectMapper
|
5
|
+
def deserialize(input, klass)
|
6
|
+
case input
|
7
|
+
when String
|
8
|
+
deserialize(JSON.parse(input), klass)
|
9
|
+
when Hash
|
10
|
+
deserialize_from_hash(input, klass)
|
11
|
+
when Array
|
12
|
+
deserialize_into_array(input, klass)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def deserialize_from_hash(hash, klass)
|
17
|
+
klass.new.tap do |instance|
|
18
|
+
klass.instance_variable_get(:@json_properties).each do |property|
|
19
|
+
value = generate_value property, hash
|
20
|
+
instance.instance_variable_set("@#{property.name}", value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def deserialize_into_array(array, klass)
|
26
|
+
array.map do |value|
|
27
|
+
deserialize(value, klass)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def generate_value(property, json_hash)
|
34
|
+
value_from_json = json_hash.fetch(property.name.to_s) do |key|
|
35
|
+
if property.required?
|
36
|
+
raise "missing required key #{key}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
case value_from_json
|
41
|
+
# already cast to the appropriate class
|
42
|
+
when property.klass
|
43
|
+
value_from_json
|
44
|
+
# needs to be deserialized into its own object
|
45
|
+
when Hash
|
46
|
+
deserialize_from_hash(value_from_json, property.klass)
|
47
|
+
when NilClass
|
48
|
+
nil
|
49
|
+
else
|
50
|
+
raise "type mismatch between #{value_from_json.inspect} (a #{value_from_json.class}) and #{property.klass}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/rackson.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rackson/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rackson"
|
8
|
+
spec.version = Rackson::VERSION
|
9
|
+
spec.authors = ["Dylan Griffin"]
|
10
|
+
spec.email = ["dgriffin@twitter.com"]
|
11
|
+
spec.summary = %q{A library to turn JSON into POROs.}
|
12
|
+
spec.description = %q{A library to turn JSON into POROs in a somewhat typesafe manner.}
|
13
|
+
spec.homepage = "https://github.com/griffindy/rackson"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
spec.add_development_dependency "pry", "~> 0.10.1"
|
25
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require_relative '../../lib/rackson'
|
2
|
+
require_relative '../../lib/rackson/object_mapper'
|
3
|
+
|
4
|
+
describe Rackson::ObjectMapper do
|
5
|
+
let(:mapper) { Rackson::ObjectMapper.new }
|
6
|
+
class DifferentFakeObject
|
7
|
+
include Rackson
|
8
|
+
json_property :baz, String
|
9
|
+
end
|
10
|
+
|
11
|
+
class FakeObject
|
12
|
+
include Rackson
|
13
|
+
json_property :foo, String
|
14
|
+
json_property :bar, DifferentFakeObject
|
15
|
+
json_property :something_optional, String, optional: true
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#deserialize' do
|
19
|
+
let(:json_string) { '{"foo": "bar", "bar": {"baz":"another thing"}}' }
|
20
|
+
let(:deserialized) { mapper.deserialize(json_string, FakeObject) }
|
21
|
+
|
22
|
+
it 'returns an object of the given class' do
|
23
|
+
expect(deserialized).to be_a FakeObject
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'sets the appropriate values' do
|
27
|
+
expect(deserialized.foo).to eq 'bar'
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'understands non JSON POROs' do
|
31
|
+
expect(deserialized.bar.baz).to eq 'another thing'
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'understands optional properties' do
|
35
|
+
expect(deserialized.something_optional).to be nil
|
36
|
+
with_optional = mapper.deserialize '{"foo": "bar", "bar": {"baz":"another thing"}, "something_optional": "this now exists"}', FakeObject
|
37
|
+
expect(with_optional.something_optional).to eq 'this now exists'
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'exposes type mismatches' do
|
41
|
+
expect do
|
42
|
+
# DifferentFakeObject#baz is declared as a String above
|
43
|
+
mapper.deserialize '{ "baz": 1 }', DifferentFakeObject
|
44
|
+
end.to raise_error(/type mismatch between/)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'yells about missing keys' do
|
48
|
+
expect do
|
49
|
+
# FakeObject also requires `bar`
|
50
|
+
mapper.deserialize '{ "foo": "bar" }', FakeObject
|
51
|
+
end.to raise_error(/missing required key/)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#deserialize_into_array' do
|
56
|
+
let(:json_string) { '[{ "baz": "foo" }]' }
|
57
|
+
it 'deserializes into an array with #deserialize' do
|
58
|
+
deserialized = mapper.deserialize(json_string, DifferentFakeObject)
|
59
|
+
expect(deserialized).to be_a Array
|
60
|
+
expect(deserialized.length).to eq 1
|
61
|
+
expect(deserialized.first.baz).to eq 'foo'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rackson
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dylan Griffin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-02-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.7'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.7'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '10.0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '10.0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: pry
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.10.1
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 0.10.1
|
78
|
+
description: A library to turn JSON into POROs in a somewhat typesafe manner.
|
79
|
+
email:
|
80
|
+
- dgriffin@twitter.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- lib/rackson.rb
|
91
|
+
- lib/rackson/object_mapper.rb
|
92
|
+
- lib/rackson/property.rb
|
93
|
+
- lib/rackson/version.rb
|
94
|
+
- rackson.gemspec
|
95
|
+
- spec/rackson/object_mapper_spec.rb
|
96
|
+
homepage: https://github.com/griffindy/rackson
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.8.23.2
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: A library to turn JSON into POROs.
|
121
|
+
test_files:
|
122
|
+
- spec/rackson/object_mapper_spec.rb
|
123
|
+
has_rdoc:
|