fast_jsonapi 1.0.17 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +106 -38
- data/lib/extensions/has_one.rb +19 -13
- data/lib/fast_jsonapi.rb +2 -0
- data/lib/fast_jsonapi/instrumentation.rb +2 -0
- data/lib/fast_jsonapi/instrumentation/serializable_hash.rb +15 -0
- data/lib/fast_jsonapi/instrumentation/serialized_json.rb +15 -0
- data/lib/fast_jsonapi/instrumentation/skylight.rb +2 -0
- data/lib/fast_jsonapi/instrumentation/skylight/normalizers/serializable_hash.rb +22 -0
- data/lib/fast_jsonapi/instrumentation/skylight/normalizers/serialized_json.rb +22 -0
- data/lib/fast_jsonapi/multi_to_json.rb +92 -0
- data/lib/fast_jsonapi/object_serializer.rb +120 -91
- data/lib/fast_jsonapi/serialization_core.rb +44 -32
- data/lib/generators/serializer/USAGE +8 -0
- data/lib/generators/serializer/serializer_generator.rb +19 -0
- data/lib/generators/serializer/templates/serializer.rb.tt +6 -0
- metadata +48 -88
- data/.document +0 -5
- data/.rspec +0 -1
- data/Gemfile +0 -4
- data/Gemfile.lock +0 -158
- data/README.rdoc +0 -231
- data/Rakefile +0 -55
- data/VERSION +0 -1
- data/docs/collection_serializer_output.json +0 -35
- data/docs/object_serializer.json +0 -30
- data/fast_jsonapi.gemspec +0 -108
- data/spec/lib/extensions/active_record_spec.rb +0 -67
- data/spec/lib/object_serializer_caching_spec.rb +0 -68
- data/spec/lib/object_serializer_class_methods_spec.rb +0 -69
- data/spec/lib/object_serializer_hyphen_spec.rb +0 -40
- data/spec/lib/object_serializer_performance_spec.rb +0 -87
- data/spec/lib/object_serializer_spec.rb +0 -126
- data/spec/lib/object_serializer_struct_spec.rb +0 -31
- data/spec/lib/serialization_core_spec.rb +0 -84
- data/spec/shared/contexts/ams_context.rb +0 -83
- data/spec/shared/contexts/movie_context.rb +0 -192
- data/spec/spec_helper.rb +0 -15
data/README.rdoc
DELETED
@@ -1,231 +0,0 @@
|
|
1
|
-
# Fast JSON API
|
2
|
-
|
3
|
-
[![NetflixOSS Lifecycle](https://img.shields.io/osslifecycle/Netflix/osstracker.svg)]()
|
4
|
-
|
5
|
-
A lightning fast [JSON:API](http://jsonapi.org/) serializer for Ruby Objects.
|
6
|
-
|
7
|
-
# Performance Comparision
|
8
|
-
|
9
|
-
We compare serialization times with Active Model Serializer as part of RSpec performance tests included on this library. We want to ensure that with every change on this library, serialization time is at least `25 times` faster than Active Model Serializers on up to current benchmark of 1000 records.
|
10
|
-
|
11
|
-
## Benchmark times for 250 records
|
12
|
-
|
13
|
-
```bash
|
14
|
-
$ rspec
|
15
|
-
Active Model Serializer serialized 250 records in 138.71 ms
|
16
|
-
Fast JSON API serialized 250 records in 3.01 ms
|
17
|
-
```
|
18
|
-
|
19
|
-
# Table of Contents
|
20
|
-
|
21
|
-
* [Features](#features)
|
22
|
-
* [Installation](#installation)
|
23
|
-
* [Usage](#usage)
|
24
|
-
* [Model Definition](#model-definition)
|
25
|
-
* [Serializer Definition](#serializer-definition)
|
26
|
-
* [Object Serialization](#object-serialization)
|
27
|
-
* [Compound Document](#compound-document)
|
28
|
-
* [Collection Serialization](#collection-serialization)
|
29
|
-
* [Caching](#caching)
|
30
|
-
* [Contributing](#contributing)
|
31
|
-
|
32
|
-
|
33
|
-
## Features
|
34
|
-
|
35
|
-
* Declaration syntax similar to Active Model serializer
|
36
|
-
* Support for belongs_to, has_many and has_one
|
37
|
-
* Support for compound documents (included)
|
38
|
-
* Optimized serialization of compound documents
|
39
|
-
* Caching
|
40
|
-
|
41
|
-
## Installation
|
42
|
-
|
43
|
-
Add this line to your application's Gemfile:
|
44
|
-
|
45
|
-
```ruby
|
46
|
-
gem 'fast_jsonapi'
|
47
|
-
```
|
48
|
-
|
49
|
-
Execute:
|
50
|
-
|
51
|
-
```ruby
|
52
|
-
$ bundle install
|
53
|
-
```
|
54
|
-
|
55
|
-
## Usage
|
56
|
-
|
57
|
-
### Model Definition
|
58
|
-
|
59
|
-
```ruby
|
60
|
-
class Movie
|
61
|
-
attr_accessor :id, :name, :year, :actor_ids, :owner_id
|
62
|
-
end
|
63
|
-
```
|
64
|
-
|
65
|
-
### Serializer Definition
|
66
|
-
|
67
|
-
```ruby
|
68
|
-
class MovieSerializer
|
69
|
-
include FastJsonapi::ObjectSerializer
|
70
|
-
set_type :movie # optional
|
71
|
-
attributes :name, :year
|
72
|
-
has_many :actors
|
73
|
-
belongs_to :owner, record_type: :user
|
74
|
-
belongs_to :movie_type
|
75
|
-
end
|
76
|
-
```
|
77
|
-
|
78
|
-
### Sample Object
|
79
|
-
|
80
|
-
```ruby
|
81
|
-
m = Movie.new
|
82
|
-
m.id = 232
|
83
|
-
m.name = 'test movie'
|
84
|
-
m.actor_ids = [1, 2, 3]
|
85
|
-
m.owner_id = 3
|
86
|
-
m.movie_type_id = 1
|
87
|
-
m
|
88
|
-
```
|
89
|
-
|
90
|
-
### Object Serialization
|
91
|
-
|
92
|
-
#### Return a hash
|
93
|
-
```ruby
|
94
|
-
hash = MovieSerializer.new(movie).serializable_hash
|
95
|
-
```
|
96
|
-
|
97
|
-
#### Return Serialized JSON
|
98
|
-
```ruby
|
99
|
-
json_string = MovieSerializer.new(movie).serialized_json
|
100
|
-
```
|
101
|
-
|
102
|
-
#### Serialized Output
|
103
|
-
|
104
|
-
```json
|
105
|
-
{
|
106
|
-
"data": {
|
107
|
-
"id": "232",
|
108
|
-
"type": "movie",
|
109
|
-
"attributes": {
|
110
|
-
"name": "test movie",
|
111
|
-
"year": null
|
112
|
-
},
|
113
|
-
"relationships": {
|
114
|
-
"actors": {
|
115
|
-
"data": [
|
116
|
-
{
|
117
|
-
"id": "1",
|
118
|
-
"type": "actor"
|
119
|
-
},
|
120
|
-
{
|
121
|
-
"id": "2",
|
122
|
-
"type": "actor"
|
123
|
-
}
|
124
|
-
]
|
125
|
-
},
|
126
|
-
"owner": {
|
127
|
-
"data": {
|
128
|
-
"id": "3",
|
129
|
-
"type": "user"
|
130
|
-
}
|
131
|
-
}
|
132
|
-
}
|
133
|
-
}
|
134
|
-
}
|
135
|
-
|
136
|
-
```
|
137
|
-
### Compound Document
|
138
|
-
|
139
|
-
Support for top-level included member through ` options[:include] `.
|
140
|
-
|
141
|
-
```ruby
|
142
|
-
options = {}
|
143
|
-
options[:meta] = { total: 2 }
|
144
|
-
options[:include] = [:actors]
|
145
|
-
MovieSerializer.new([movie, movie], options).serialized_json
|
146
|
-
```
|
147
|
-
|
148
|
-
### Collection Serialization
|
149
|
-
|
150
|
-
```ruby
|
151
|
-
options[:meta] = { total: 2 }
|
152
|
-
hash = MovieSerializer.new([movie, movie], options).serializable_hash
|
153
|
-
json_string = MovieSerializer.new([movie, movie], options).serialized_json
|
154
|
-
```
|
155
|
-
|
156
|
-
### Caching
|
157
|
-
|
158
|
-
```ruby
|
159
|
-
class MovieSerializer
|
160
|
-
include FastJsonapi::ObjectSerializer
|
161
|
-
set_type :movie # optional
|
162
|
-
cache_options enabled: true cache_length: 12.hours
|
163
|
-
attributes :name, :year
|
164
|
-
end
|
165
|
-
```
|
166
|
-
|
167
|
-
### Customizable Options
|
168
|
-
|
169
|
-
Option | Purpose | Example
|
170
|
-
------------ | ------------- | -------------
|
171
|
-
set_type | Type name of Object | ```set_type :movie ```
|
172
|
-
cache_options | Hash to enable caching and set cache length | ```cache_options enabled: true cache_length: 12.hours```
|
173
|
-
id_method_name | Set custom method name to get ID of an object | ```has_many :locations, id_method_name: :place_ids ```
|
174
|
-
object_method_name | Set custom method name to get related objects | ```has_many :locations, object_method_name: :places ```
|
175
|
-
record_type | Set custom Object Type for a relationship | ```belongs_to :owner, record_type: :user```
|
176
|
-
serializer | Set custom Serializer for a relationship | ```has_many :actors, serializer: :custom_actor```
|
177
|
-
cache_options | Hash to enable caching and set cache length | ```cache_options enabled: true cache_length: 12.hours ```
|
178
|
-
|
179
|
-
|
180
|
-
## Contributing
|
181
|
-
|
182
|
-
This gem is built using a gem building gem called [juwelier](https://github.com/flajann2/juwelier).
|
183
|
-
|
184
|
-
Beyond just editing source code, you’ll be interacting with the gem using rake a lot. To see all the tasks available with a brief description, you can run:
|
185
|
-
|
186
|
-
```bash
|
187
|
-
rake -T
|
188
|
-
```
|
189
|
-
|
190
|
-
### Updating Project information
|
191
|
-
You can update the project information of the gem by updating the [Rakefile](Rakefile). Then you need to generate a new gemspec
|
192
|
-
|
193
|
-
```bash
|
194
|
-
rake gemspec
|
195
|
-
```
|
196
|
-
|
197
|
-
### Running Tests
|
198
|
-
We use rspec for testing. We have unit tests, functional tests and performance tests. To run tests use the following rake task
|
199
|
-
|
200
|
-
```bash
|
201
|
-
rake spec
|
202
|
-
```
|
203
|
-
|
204
|
-
### Installation
|
205
|
-
|
206
|
-
```bash
|
207
|
-
$ rake install
|
208
|
-
```
|
209
|
-
|
210
|
-
The install rake task builds the gem installs it. You're all
|
211
|
-
set if you're using [RVM](http://rvm.beginrescueend.com/), but you may
|
212
|
-
need to run it with sudo if you have a system-installed ruby:
|
213
|
-
|
214
|
-
### Bumping Version
|
215
|
-
|
216
|
-
It feels good to release code. Do it, do it often. But before that, bump
|
217
|
-
the version. Then release it. There's a few ways to update the version:
|
218
|
-
|
219
|
-
```ruby
|
220
|
-
# version:write like before
|
221
|
-
$ rake version:write MAJOR=0 MINOR=3 PATCH=0
|
222
|
-
|
223
|
-
# bump just major, ie 0.1.0 -> 1.0.0
|
224
|
-
$ rake version:bump:major
|
225
|
-
|
226
|
-
# bump just minor, ie 0.1.0 -> 0.2.0
|
227
|
-
$ rake version:bump:minor
|
228
|
-
|
229
|
-
# bump just patch, ie 0.1.0 -> 0.1.1
|
230
|
-
$ rake version:bump:patch
|
231
|
-
```
|
data/Rakefile
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'bundler'
|
5
|
-
begin
|
6
|
-
Bundler.setup(:default, :development)
|
7
|
-
rescue Bundler::BundlerError => e
|
8
|
-
$stderr.puts e.message
|
9
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
-
exit e.status_code
|
11
|
-
end
|
12
|
-
require 'rake'
|
13
|
-
require 'juwelier'
|
14
|
-
Juwelier::Tasks.new do |gem|
|
15
|
-
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
16
|
-
gem.name = "fast_jsonapi"
|
17
|
-
gem.homepage = "http://github.com/Netflix/fast_jsonapi"
|
18
|
-
gem.license = "Apache-2.0"
|
19
|
-
gem.summary = %Q{fast JSON API(jsonapi.org) serializer}
|
20
|
-
gem.description = %Q{JSON API(jsonapi.org) serializer that works with rails and can be used to serialize any kind of ruby objects}
|
21
|
-
gem.email = ""
|
22
|
-
gem.authors = ["Shishir Kakaraddi", "Srinivas Raghunathan", "Adam Gross"]
|
23
|
-
|
24
|
-
|
25
|
-
if gem.respond_to?(:metadata)
|
26
|
-
gem.metadata['allowed_push_host'] = 'https://rubygems.org'
|
27
|
-
else
|
28
|
-
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
29
|
-
end
|
30
|
-
# dependencies defined in Gemfile
|
31
|
-
end
|
32
|
-
Juwelier::RubygemsDotOrgTasks.new
|
33
|
-
require 'rspec/core'
|
34
|
-
require 'rspec/core/rake_task'
|
35
|
-
RSpec::Core::RakeTask.new(:spec) do |spec|
|
36
|
-
spec.pattern = FileList['spec/**/*_spec.rb']
|
37
|
-
end
|
38
|
-
|
39
|
-
desc "Code coverage detail"
|
40
|
-
task :simplecov do
|
41
|
-
ENV['COVERAGE'] = "true"
|
42
|
-
Rake::Task['spec'].execute
|
43
|
-
end
|
44
|
-
|
45
|
-
task :default => :spec
|
46
|
-
|
47
|
-
require 'rdoc/task'
|
48
|
-
Rake::RDocTask.new do |rdoc|
|
49
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
50
|
-
|
51
|
-
rdoc.rdoc_dir = 'rdoc'
|
52
|
-
rdoc.title = "fast_jsonapi #{version}"
|
53
|
-
rdoc.rdoc_files.include('README*')
|
54
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
-
end
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1.0.17
|
@@ -1,35 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"data": [
|
3
|
-
{
|
4
|
-
"id": "232",
|
5
|
-
"type": "movie",
|
6
|
-
"attributes": {
|
7
|
-
"name": "test movie",
|
8
|
-
"year": null
|
9
|
-
},
|
10
|
-
"relationships": {
|
11
|
-
"actors": {
|
12
|
-
"data": [
|
13
|
-
{
|
14
|
-
"id": "1",
|
15
|
-
"type": "actor"
|
16
|
-
},
|
17
|
-
{
|
18
|
-
"id": "2",
|
19
|
-
"type": "actor"
|
20
|
-
}
|
21
|
-
]
|
22
|
-
},
|
23
|
-
"owner": {
|
24
|
-
"data": {
|
25
|
-
"id": "3",
|
26
|
-
"type": "user"
|
27
|
-
}
|
28
|
-
}
|
29
|
-
}
|
30
|
-
}
|
31
|
-
],
|
32
|
-
"meta": {
|
33
|
-
"total": 2
|
34
|
-
}
|
35
|
-
}
|
data/docs/object_serializer.json
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"data": {
|
3
|
-
"id": "232",
|
4
|
-
"type": "movie",
|
5
|
-
"attributes": {
|
6
|
-
"name": "test movie",
|
7
|
-
"year": null
|
8
|
-
},
|
9
|
-
"relationships": {
|
10
|
-
"actors": {
|
11
|
-
"data": [
|
12
|
-
{
|
13
|
-
"id": "1",
|
14
|
-
"type": "actor"
|
15
|
-
},
|
16
|
-
{
|
17
|
-
"id": "2",
|
18
|
-
"type": "actor"
|
19
|
-
}
|
20
|
-
]
|
21
|
-
},
|
22
|
-
"owner": {
|
23
|
-
"data": {
|
24
|
-
"id": "3",
|
25
|
-
"type": "user"
|
26
|
-
}
|
27
|
-
}
|
28
|
-
}
|
29
|
-
}
|
30
|
-
}
|
data/fast_jsonapi.gemspec
DELETED
@@ -1,108 +0,0 @@
|
|
1
|
-
# Generated by juwelier
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: fast_jsonapi 1.0.16 ruby lib
|
6
|
-
|
7
|
-
Gem::Specification.new do |s|
|
8
|
-
s.name = "fast_jsonapi"
|
9
|
-
s.version = "1.0.17"
|
10
|
-
|
11
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
-
s.metadata = { "allowed_push_host" => "https://rubygems.org" } if s.respond_to? :metadata=
|
13
|
-
s.require_paths = ["lib"]
|
14
|
-
s.authors = ["Shishir Kakaraddi", "Srinivas Raghunathan", "Adam Gross"]
|
15
|
-
s.date = "2018-02-01"
|
16
|
-
s.description = "JSON API(jsonapi.org) serializer that works with rails and can be used to serialize any kind of ruby objects"
|
17
|
-
s.email = ""
|
18
|
-
s.extra_rdoc_files = [
|
19
|
-
"LICENSE.txt",
|
20
|
-
"README.md",
|
21
|
-
"README.rdoc"
|
22
|
-
]
|
23
|
-
s.files = [
|
24
|
-
".document",
|
25
|
-
".rspec",
|
26
|
-
"Gemfile",
|
27
|
-
"Gemfile.lock",
|
28
|
-
"LICENSE.txt",
|
29
|
-
"README.md",
|
30
|
-
"README.rdoc",
|
31
|
-
"Rakefile",
|
32
|
-
"VERSION",
|
33
|
-
"docs/collection_serializer_output.json",
|
34
|
-
"docs/object_serializer.json",
|
35
|
-
"fast_jsonapi.gemspec",
|
36
|
-
"lib/extensions/has_one.rb",
|
37
|
-
"lib/fast_jsonapi.rb",
|
38
|
-
"lib/fast_jsonapi/object_serializer.rb",
|
39
|
-
"lib/fast_jsonapi/serialization_core.rb",
|
40
|
-
"spec/lib/extensions/active_record_spec.rb",
|
41
|
-
"spec/lib/object_serializer_caching_spec.rb",
|
42
|
-
"spec/lib/object_serializer_class_methods_spec.rb",
|
43
|
-
"spec/lib/object_serializer_hyphen_spec.rb",
|
44
|
-
"spec/lib/object_serializer_performance_spec.rb",
|
45
|
-
"spec/lib/object_serializer_spec.rb",
|
46
|
-
"spec/lib/object_serializer_struct_spec.rb",
|
47
|
-
"spec/lib/serialization_core_spec.rb",
|
48
|
-
"spec/shared/contexts/ams_context.rb",
|
49
|
-
"spec/shared/contexts/movie_context.rb",
|
50
|
-
"spec/spec_helper.rb"
|
51
|
-
]
|
52
|
-
s.homepage = "http://github.com/Netflix/fast_jsonapi"
|
53
|
-
s.licenses = ["Apache-2.0"]
|
54
|
-
s.rubygems_version = "2.5.1"
|
55
|
-
s.summary = "fast JSON API(jsonapi.org) serializer"
|
56
|
-
|
57
|
-
if s.respond_to? :specification_version then
|
58
|
-
s.specification_version = 4
|
59
|
-
|
60
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
61
|
-
s.add_runtime_dependency(%q<activesupport>, ["~> 5.0"])
|
62
|
-
s.add_runtime_dependency(%q<multi_json>, ["~> 1.12"])
|
63
|
-
s.add_runtime_dependency(%q<oj>, ["~> 3.3"])
|
64
|
-
s.add_runtime_dependency(%q<activerecord>, ["~> 5.0"])
|
65
|
-
s.add_development_dependency(%q<skylight>, ["~> 1.3"])
|
66
|
-
s.add_development_dependency(%q<rspec>, ["~> 3.5.0"])
|
67
|
-
s.add_development_dependency(%q<rspec-benchmark>, ["~> 0.3.0"])
|
68
|
-
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
69
|
-
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
70
|
-
s.add_development_dependency(%q<juwelier>, ["~> 2.1.0"])
|
71
|
-
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
72
|
-
s.add_development_dependency(%q<byebug>, [">= 0"])
|
73
|
-
s.add_development_dependency(%q<active_model_serializers>, ["~> 0.10.4"])
|
74
|
-
s.add_development_dependency(%q<sqlite3>, ["~> 1.3"])
|
75
|
-
else
|
76
|
-
s.add_dependency(%q<activesupport>, ["~> 5.0"])
|
77
|
-
s.add_dependency(%q<multi_json>, ["~> 1.12"])
|
78
|
-
s.add_dependency(%q<oj>, ["~> 3.3"])
|
79
|
-
s.add_dependency(%q<skylight>, ["~> 1.3"])
|
80
|
-
s.add_dependency(%q<activerecord>, ["~> 5.0"])
|
81
|
-
s.add_dependency(%q<rspec>, ["~> 3.5.0"])
|
82
|
-
s.add_dependency(%q<rspec-benchmark>, ["~> 0.3.0"])
|
83
|
-
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
84
|
-
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
85
|
-
s.add_dependency(%q<juwelier>, ["~> 2.1.0"])
|
86
|
-
s.add_dependency(%q<simplecov>, [">= 0"])
|
87
|
-
s.add_dependency(%q<byebug>, [">= 0"])
|
88
|
-
s.add_dependency(%q<active_model_serializers>, ["~> 0.10.4"])
|
89
|
-
s.add_dependency(%q<sqlite3>, ["~> 1.3"])
|
90
|
-
end
|
91
|
-
else
|
92
|
-
s.add_dependency(%q<activesupport>, ["~> 5.0"])
|
93
|
-
s.add_dependency(%q<multi_json>, ["~> 1.12"])
|
94
|
-
s.add_dependency(%q<oj>, ["~> 3.3"])
|
95
|
-
s.add_dependency(%q<skylight>, ["~> 1.3"])
|
96
|
-
s.add_dependency(%q<activerecord>, ["~> 5.0"])
|
97
|
-
s.add_dependency(%q<rspec>, ["~> 3.5.0"])
|
98
|
-
s.add_dependency(%q<rspec-benchmark>, ["~> 0.3.0"])
|
99
|
-
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
100
|
-
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
101
|
-
s.add_dependency(%q<juwelier>, ["~> 2.1.0"])
|
102
|
-
s.add_dependency(%q<simplecov>, [">= 0"])
|
103
|
-
s.add_dependency(%q<byebug>, [">= 0"])
|
104
|
-
s.add_dependency(%q<active_model_serializers>, ["~> 0.10.4"])
|
105
|
-
s.add_dependency(%q<sqlite3>, ["~> 1.3"])
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|