juso 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/main.yml +24 -0
- data/.gitignore +8 -0
- data/.rubocop.yml +13 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +44 -0
- data/LICENSE.txt +21 -0
- data/README.md +101 -0
- data/Rakefile +16 -0
- data/benchmark/single_resource.rb +456 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/juso.gemspec +37 -0
- data/lib/juso/version.rb +5 -0
- data/lib/juso.rb +77 -0
- data/misc/juso.png +0 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d887123b941a26f478f70d16785ff8dbb715ddd2bd6ac4454f5ab94091cae491
|
4
|
+
data.tar.gz: 799410137cc3491745f6907b5d3481bc33e905d56277a5fc49e6c431f698b544
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f7cf662800ae228c3b7c9971352c64dd4f1b67d8aad1c2eb83a98ea80b758d99660e875bd063c843ac172dcb10f583a36fb3738ddf229eeed151df1dc6857940
|
7
|
+
data.tar.gz: ae70c822282eceaac1d3cca0190cf35b8104575363978ac0860551b1cb2b92ef4b7ea9b871bb364adc23a8517353a391a16f7471cc40dc80b2905e13043b39e6
|
@@ -0,0 +1,24 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
timeout-minutes: 10
|
9
|
+
|
10
|
+
strategy:
|
11
|
+
matrix:
|
12
|
+
ruby-version: [3.1.0-preview1, 3.0.2, 2.7.4, 2.6.8]
|
13
|
+
|
14
|
+
steps:
|
15
|
+
- uses: actions/checkout@v2
|
16
|
+
|
17
|
+
- name: Set up Ruby ${{ matrix.ruby-version }}
|
18
|
+
uses: ruby/setup-ruby@v1
|
19
|
+
with:
|
20
|
+
ruby-version: ${{ matrix.ruby-version }}
|
21
|
+
bundler-cache: true
|
22
|
+
|
23
|
+
- name: Run test
|
24
|
+
run: bundle exec rake test
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at yukibukiyou@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
juso (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
minitest (5.14.4)
|
11
|
+
parallel (1.21.0)
|
12
|
+
parser (3.0.2.0)
|
13
|
+
ast (~> 2.4.1)
|
14
|
+
rainbow (3.0.0)
|
15
|
+
rake (13.0.6)
|
16
|
+
regexp_parser (2.1.1)
|
17
|
+
rexml (3.2.5)
|
18
|
+
rubocop (1.23.0)
|
19
|
+
parallel (~> 1.10)
|
20
|
+
parser (>= 3.0.0.0)
|
21
|
+
rainbow (>= 2.2.2, < 4.0)
|
22
|
+
regexp_parser (>= 1.8, < 3.0)
|
23
|
+
rexml
|
24
|
+
rubocop-ast (>= 1.12.0, < 2.0)
|
25
|
+
ruby-progressbar (~> 1.7)
|
26
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
27
|
+
rubocop-ast (1.13.0)
|
28
|
+
parser (>= 3.0.1.1)
|
29
|
+
ruby-progressbar (1.11.0)
|
30
|
+
unicode-display_width (2.1.0)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
arm64-darwin-20
|
34
|
+
ruby
|
35
|
+
x86_64-linux
|
36
|
+
|
37
|
+
DEPENDENCIES
|
38
|
+
juso!
|
39
|
+
minitest (~> 5.0)
|
40
|
+
rake (~> 13.0)
|
41
|
+
rubocop (~> 1.7)
|
42
|
+
|
43
|
+
BUNDLED WITH
|
44
|
+
2.2.22
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 ykpythemind
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# Juso
|
2
|
+
|
3
|
+
<div align="center">
|
4
|
+
<img width="300" src="misc/juso.png" alt="juso"/>
|
5
|
+
</div>
|
6
|
+
|
7
|
+
Juso is simple, fast and explicit JSON Serializer.
|
8
|
+
Juso means 13 (thirteen) in Japanese.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'juso'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle install
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install juso
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
1. Include `Juso::Serializable` to your class.
|
29
|
+
2. Define as_juso_json(context) method.
|
30
|
+
3. Use Juso.generate(object) method to generate json.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
class User < ApplicationRecord
|
34
|
+
include Juso::Serializable
|
35
|
+
|
36
|
+
# ...
|
37
|
+
|
38
|
+
def as_juso_json(context)
|
39
|
+
{
|
40
|
+
id: id,
|
41
|
+
nickname: nickname,
|
42
|
+
}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Team < ApplicationRecord
|
47
|
+
include Juso::Serializable
|
48
|
+
|
49
|
+
has_many :users
|
50
|
+
|
51
|
+
# ...
|
52
|
+
|
53
|
+
def as_juso_json(context)
|
54
|
+
{
|
55
|
+
id: id,
|
56
|
+
name: name,
|
57
|
+
users: users
|
58
|
+
}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
team = Team.first!
|
65
|
+
Juso.generate(team)
|
66
|
+
=> {"id":1, name: "team name", users: [{id:1,nickname:"hoge"},{id:2,nickname:"piyo"}]}
|
67
|
+
```
|
68
|
+
|
69
|
+
### Rule of juso
|
70
|
+
|
71
|
+
#### Japanese
|
72
|
+
|
73
|
+
as_juso_json メソッドは以下のインスタンスしか返してはいけません
|
74
|
+
|
75
|
+
- Numeric Class
|
76
|
+
- String Class
|
77
|
+
- Null Class
|
78
|
+
- Hash Class
|
79
|
+
- Array Class
|
80
|
+
- Juso::Serializable をinclude したクラス
|
81
|
+
- Date / DateTime / ActiveSupport::TimeWithZone
|
82
|
+
|
83
|
+
再帰的にjusoの処理が適用されるため、Arrayの要素やHashのvalueも同様のルールが適用されます
|
84
|
+
|
85
|
+
## Development
|
86
|
+
|
87
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
88
|
+
|
89
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/juso. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/juso/blob/main/CODE_OF_CONDUCT.md).
|
94
|
+
|
95
|
+
## License
|
96
|
+
|
97
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
98
|
+
|
99
|
+
## Code of Conduct
|
100
|
+
|
101
|
+
Everyone interacting in the Juso project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/juso/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,456 @@
|
|
1
|
+
# original from https://github.com/okuramasafumi/alba/blob/main/benchmark/single_resource.rb
|
2
|
+
|
3
|
+
# Benchmark script to run varieties of JSON serializers
|
4
|
+
# Fetch Alba from local, otherwise fetch latest from RubyGems
|
5
|
+
|
6
|
+
# --- Bundle dependencies ---
|
7
|
+
|
8
|
+
require "bundler/inline"
|
9
|
+
|
10
|
+
gemfile(true) do
|
11
|
+
source "https://rubygems.org"
|
12
|
+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
13
|
+
|
14
|
+
gem "active_model_serializers"
|
15
|
+
gem "activerecord", "6.1.3"
|
16
|
+
gem 'alba'
|
17
|
+
gem "juso", path: '../'
|
18
|
+
gem "benchmark-ips"
|
19
|
+
gem "benchmark-memory"
|
20
|
+
gem "blueprinter"
|
21
|
+
gem "jbuilder"
|
22
|
+
gem "jserializer"
|
23
|
+
gem "jsonapi-serializer" # successor of fast_jsonapi
|
24
|
+
gem "multi_json"
|
25
|
+
# gem "panko_serializer"
|
26
|
+
# gem "pg"
|
27
|
+
gem "primalize"
|
28
|
+
# gem "oj"
|
29
|
+
gem "representable"
|
30
|
+
gem "simple_ams"
|
31
|
+
gem "sqlite3"
|
32
|
+
end
|
33
|
+
|
34
|
+
# --- Test data model setup ---
|
35
|
+
|
36
|
+
# require "pg"
|
37
|
+
require "active_record"
|
38
|
+
# require "active_record/connection_adapters/postgresql_adapter"
|
39
|
+
require "active_record/connection_adapters/sqlite3_adapter"
|
40
|
+
require "logger"
|
41
|
+
# require "oj"
|
42
|
+
require "sqlite3"
|
43
|
+
# Oj.optimize_rails
|
44
|
+
|
45
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
46
|
+
# ActiveRecord::Base.logger = Logger.new($stdout)
|
47
|
+
|
48
|
+
ActiveRecord::Schema.define do
|
49
|
+
create_table :posts, force: true do |t|
|
50
|
+
t.string :body
|
51
|
+
end
|
52
|
+
|
53
|
+
create_table :comments, force: true do |t|
|
54
|
+
t.integer :post_id
|
55
|
+
t.string :body
|
56
|
+
t.integer :commenter_id
|
57
|
+
end
|
58
|
+
|
59
|
+
create_table :users, force: true do |t|
|
60
|
+
t.string :name
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class Post < ActiveRecord::Base
|
65
|
+
has_many :comments
|
66
|
+
has_many :commenters, through: :comments, class_name: 'User', source: :commenter
|
67
|
+
|
68
|
+
def attributes
|
69
|
+
{id: nil, body: nil, commenter_names: commenter_names}
|
70
|
+
end
|
71
|
+
|
72
|
+
def commenter_names
|
73
|
+
commenters.pluck(:name)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
class Comment < ActiveRecord::Base
|
78
|
+
belongs_to :post
|
79
|
+
belongs_to :commenter, class_name: 'User'
|
80
|
+
|
81
|
+
def attributes
|
82
|
+
{id: nil, body: nil}
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class User < ActiveRecord::Base
|
87
|
+
has_many :comments
|
88
|
+
end
|
89
|
+
|
90
|
+
# --- Juso serializers ---
|
91
|
+
|
92
|
+
require "juso"
|
93
|
+
require 'active_record' # hack for CollectionProxy
|
94
|
+
Juso.reset_collection_classes
|
95
|
+
|
96
|
+
class Comment
|
97
|
+
include ::Juso::Serializable
|
98
|
+
|
99
|
+
def as_juso_json(context)
|
100
|
+
{id: id, body: body}
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
class Post
|
105
|
+
include ::Juso::Serializable
|
106
|
+
|
107
|
+
def as_juso_json(context)
|
108
|
+
{id: id, body: body, commenter_names: commenter_names, comments: comments}
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# --- Alba serializers ---
|
113
|
+
|
114
|
+
require "alba"
|
115
|
+
|
116
|
+
class AlbaCommentResource
|
117
|
+
include ::Alba::Resource
|
118
|
+
attributes :id, :body
|
119
|
+
end
|
120
|
+
|
121
|
+
class AlbaPostResource
|
122
|
+
include ::Alba::Resource
|
123
|
+
attributes :id, :body
|
124
|
+
attribute :commenter_names do |post|
|
125
|
+
post.commenters.pluck(:name)
|
126
|
+
end
|
127
|
+
many :comments, resource: AlbaCommentResource
|
128
|
+
end
|
129
|
+
|
130
|
+
# --- ActiveModelSerializer serializers ---
|
131
|
+
|
132
|
+
require "active_model_serializers"
|
133
|
+
|
134
|
+
class AMSCommentSerializer < ActiveModel::Serializer
|
135
|
+
attributes :id, :body
|
136
|
+
end
|
137
|
+
|
138
|
+
class AMSPostSerializer < ActiveModel::Serializer
|
139
|
+
attributes :id, :body
|
140
|
+
attribute :commenter_names
|
141
|
+
has_many :comments, serializer: AMSCommentSerializer
|
142
|
+
|
143
|
+
def commenter_names
|
144
|
+
object.commenters.pluck(:name)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
# --- Blueprint serializers ---
|
149
|
+
|
150
|
+
require "blueprinter"
|
151
|
+
|
152
|
+
class CommentBlueprint < Blueprinter::Base
|
153
|
+
fields :id, :body
|
154
|
+
end
|
155
|
+
|
156
|
+
class PostBlueprint < Blueprinter::Base
|
157
|
+
fields :id, :body, :commenter_names
|
158
|
+
association :comments, blueprint: CommentBlueprint
|
159
|
+
|
160
|
+
def commenter_names
|
161
|
+
commenters.pluck(:name)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
# --- JBuilder serializers ---
|
166
|
+
|
167
|
+
require "jbuilder"
|
168
|
+
|
169
|
+
class Post
|
170
|
+
def to_builder
|
171
|
+
Jbuilder.new do |post|
|
172
|
+
post.call(self, :id, :body, :commenter_names, :comments)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def commenter_names
|
177
|
+
commenters.pluck(:name)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
class Comment
|
182
|
+
def to_builder
|
183
|
+
Jbuilder.new do |comment|
|
184
|
+
comment.call(self, :id, :body)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
# --- Jserializer serializers ---
|
190
|
+
|
191
|
+
require 'jserializer'
|
192
|
+
|
193
|
+
class JserializerCommentSerializer < Jserializer::Base
|
194
|
+
attributes :id, :body
|
195
|
+
end
|
196
|
+
|
197
|
+
class JserializerPostSerializer < Jserializer::Base
|
198
|
+
attributes :id, :body, :commenter_names
|
199
|
+
has_many :comments, serializer: JserializerCommentSerializer
|
200
|
+
def commenter_names
|
201
|
+
object.commenters.pluck(:name)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
|
206
|
+
# --- JSONAPI:Serializer serializers / (successor of fast_jsonapi) ---
|
207
|
+
|
208
|
+
class JsonApiStandardCommentSerializer
|
209
|
+
include JSONAPI::Serializer
|
210
|
+
|
211
|
+
attribute :id
|
212
|
+
attribute :body
|
213
|
+
end
|
214
|
+
|
215
|
+
class JsonApiStandardPostSerializer
|
216
|
+
include JSONAPI::Serializer
|
217
|
+
|
218
|
+
# set_type :post # optional
|
219
|
+
attribute :id
|
220
|
+
attribute :body
|
221
|
+
attribute :commenter_names
|
222
|
+
|
223
|
+
attribute :comments do |post|
|
224
|
+
post.comments.map { |comment| JsonApiSameFormatCommentSerializer.new(comment) }
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
# --- JSONAPI:Serializer serializers that format the code the same flat way as the other gems here ---
|
229
|
+
|
230
|
+
# code to convert from JSON:API output to "flat" JSON, like the other serializers build
|
231
|
+
class JsonApiSameFormatSerializer
|
232
|
+
include JSONAPI::Serializer
|
233
|
+
|
234
|
+
def as_json(*_options)
|
235
|
+
hash = serializable_hash
|
236
|
+
|
237
|
+
if hash[:data].is_a? Hash
|
238
|
+
hash[:data][:attributes]
|
239
|
+
|
240
|
+
elsif hash[:data].is_a? Array
|
241
|
+
hash[:data].pluck(:attributes)
|
242
|
+
|
243
|
+
elsif hash[:data].nil?
|
244
|
+
{ }
|
245
|
+
|
246
|
+
else
|
247
|
+
raise "unexpected data type #{hash[:data].class}"
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
class JsonApiSameFormatCommentSerializer < JsonApiSameFormatSerializer
|
253
|
+
attribute :id
|
254
|
+
attribute :body
|
255
|
+
end
|
256
|
+
|
257
|
+
class JsonApiSameFormatPostSerializer < JsonApiSameFormatSerializer
|
258
|
+
attribute :id
|
259
|
+
attribute :body
|
260
|
+
attribute :commenter_names
|
261
|
+
|
262
|
+
attribute :comments do |post|
|
263
|
+
post.comments.map { |comment| JsonApiSameFormatCommentSerializer.new(comment) }
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
# --- Panko serializers ---
|
268
|
+
#
|
269
|
+
# require "panko_serializer"
|
270
|
+
#
|
271
|
+
# class PankoCommentSerializer < Panko::Serializer
|
272
|
+
# attributes :id, :body
|
273
|
+
# end
|
274
|
+
#
|
275
|
+
#
|
276
|
+
# class PankoPostSerializer < Panko::Serializer
|
277
|
+
# attributes :id, :body, :commenter_names
|
278
|
+
#
|
279
|
+
# has_many :comments, serializer: PankoCommentSerializer
|
280
|
+
#
|
281
|
+
# def commenter_names
|
282
|
+
# object.comments.pluck(:name)
|
283
|
+
# end
|
284
|
+
# end
|
285
|
+
|
286
|
+
# --- Primalize serializers ---
|
287
|
+
#
|
288
|
+
class PrimalizeCommentResource < Primalize::Single
|
289
|
+
attributes id: integer, body: string
|
290
|
+
end
|
291
|
+
|
292
|
+
class PrimalizePostResource < Primalize::Single
|
293
|
+
alias post object
|
294
|
+
|
295
|
+
attributes(
|
296
|
+
id: integer,
|
297
|
+
body: string,
|
298
|
+
comments: array(primalize(PrimalizeCommentResource)),
|
299
|
+
commenter_names: array(string),
|
300
|
+
)
|
301
|
+
|
302
|
+
def commenter_names
|
303
|
+
post.commenters.pluck(:name)
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
# --- Representable serializers ---
|
308
|
+
|
309
|
+
require "representable"
|
310
|
+
|
311
|
+
class CommentRepresenter < Representable::Decorator
|
312
|
+
include Representable::JSON
|
313
|
+
|
314
|
+
property :id
|
315
|
+
property :body
|
316
|
+
end
|
317
|
+
|
318
|
+
class PostRepresenter < Representable::Decorator
|
319
|
+
include Representable::JSON
|
320
|
+
|
321
|
+
property :id
|
322
|
+
property :body
|
323
|
+
property :commenter_names
|
324
|
+
collection :comments
|
325
|
+
|
326
|
+
def commenter_names
|
327
|
+
commenters.pluck(:name)
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
# --- SimpleAMS serializers ---
|
332
|
+
|
333
|
+
require "simple_ams"
|
334
|
+
|
335
|
+
class SimpleAMSCommentSerializer
|
336
|
+
include SimpleAMS::DSL
|
337
|
+
|
338
|
+
attributes :id, :body
|
339
|
+
end
|
340
|
+
|
341
|
+
class SimpleAMSPostSerializer
|
342
|
+
include SimpleAMS::DSL
|
343
|
+
|
344
|
+
attributes :id, :body
|
345
|
+
attribute :commenter_names
|
346
|
+
has_many :comments, serializer: SimpleAMSCommentSerializer
|
347
|
+
|
348
|
+
def commenter_names
|
349
|
+
object.commenters.pluck(:name)
|
350
|
+
end
|
351
|
+
end
|
352
|
+
|
353
|
+
# --- Test data creation ---
|
354
|
+
|
355
|
+
post = Post.create!(body: 'post')
|
356
|
+
user1 = User.create!(name: 'John')
|
357
|
+
user2 = User.create!(name: 'Jane')
|
358
|
+
post.comments.create!(commenter: user1, body: 'Comment1')
|
359
|
+
post.comments.create!(commenter: user2, body: 'Comment2')
|
360
|
+
post.reload
|
361
|
+
|
362
|
+
# --- Store the serializers in procs ---
|
363
|
+
|
364
|
+
juso = Proc.new do
|
365
|
+
Juso.generate(post)
|
366
|
+
rescue
|
367
|
+
binding.irb
|
368
|
+
end
|
369
|
+
|
370
|
+
alba = Proc.new { AlbaPostResource.new(post).serialize }
|
371
|
+
alba_inline = Proc.new do
|
372
|
+
Alba.serialize(post) do
|
373
|
+
attributes :id, :body
|
374
|
+
attribute :commenter_names do |post|
|
375
|
+
post.commenters.pluck(:name)
|
376
|
+
end
|
377
|
+
many :comments do
|
378
|
+
attributes :id, :body
|
379
|
+
end
|
380
|
+
end
|
381
|
+
end
|
382
|
+
ams = Proc.new { AMSPostSerializer.new(post, {}).to_json }
|
383
|
+
blueprinter = Proc.new { PostBlueprint.render(post) }
|
384
|
+
jbuilder = Proc.new { post.to_builder.target! }
|
385
|
+
jserializer = Proc.new { JserializerPostSerializer.new(post).to_json }
|
386
|
+
jsonapi = proc { JsonApiStandardPostSerializer.new(post).to_json }
|
387
|
+
jsonapi_same_format = proc { JsonApiSameFormatPostSerializer.new(post).to_json }
|
388
|
+
# panko = proc { PankoPostSerializer.new.serialize_to_json(post) }
|
389
|
+
panko = proc { 'nop' }
|
390
|
+
primalize = proc { PrimalizePostResource.new(post).to_json }
|
391
|
+
rails = Proc.new { ActiveSupport::JSON.encode(post.serializable_hash(include: :comments)) }
|
392
|
+
representable = Proc.new { PostRepresenter.new(post).to_json }
|
393
|
+
simple_ams = Proc.new { SimpleAMS::Renderer.new(post, serializer: SimpleAMSPostSerializer).to_json }
|
394
|
+
|
395
|
+
# --- Execute the serializers to check their output ---
|
396
|
+
|
397
|
+
puts "Serializer outputs ----------------------------------"
|
398
|
+
{
|
399
|
+
alba: alba,
|
400
|
+
alba_inline: alba_inline,
|
401
|
+
juso: juso,
|
402
|
+
ams: ams,
|
403
|
+
blueprinter: blueprinter,
|
404
|
+
jbuilder: jbuilder, # different order
|
405
|
+
jserializer: jserializer,
|
406
|
+
jsonapi: jsonapi, # nested JSON:API format
|
407
|
+
jsonapi_same_format: jsonapi_same_format,
|
408
|
+
panko: panko,
|
409
|
+
primalize: primalize,
|
410
|
+
rails: rails,
|
411
|
+
representable: representable,
|
412
|
+
simple_ams: simple_ams,
|
413
|
+
}.each { |name, serializer| puts "#{name.to_s.ljust(24, ' ')} #{serializer.call}" }
|
414
|
+
|
415
|
+
# --- Run the benchmarks ---
|
416
|
+
|
417
|
+
require 'benchmark/ips'
|
418
|
+
Benchmark.ips do |x|
|
419
|
+
x.report(:alba, &alba)
|
420
|
+
x.report(:alba_inline, &alba_inline)
|
421
|
+
x.report(:juso, &juso)
|
422
|
+
x.report(:ams, &ams)
|
423
|
+
x.report(:blueprinter, &blueprinter)
|
424
|
+
x.report(:jbuilder, &jbuilder)
|
425
|
+
x.report(:jserializer, &jserializer)
|
426
|
+
x.report(:jsonapi, &jsonapi)
|
427
|
+
x.report(:jsonapi_same_format, &jsonapi_same_format)
|
428
|
+
x.report(:panko, &panko)
|
429
|
+
x.report(:primalize, &primalize)
|
430
|
+
x.report(:rails, &rails)
|
431
|
+
x.report(:representable, &representable)
|
432
|
+
x.report(:simple_ams, &simple_ams)
|
433
|
+
|
434
|
+
x.compare!
|
435
|
+
end
|
436
|
+
|
437
|
+
|
438
|
+
require 'benchmark/memory'
|
439
|
+
Benchmark.memory do |x|
|
440
|
+
x.report(:alba, &alba)
|
441
|
+
x.report(:alba_inline, &alba_inline)
|
442
|
+
x.report(:juso, &juso)
|
443
|
+
x.report(:ams, &ams)
|
444
|
+
x.report(:blueprinter, &blueprinter)
|
445
|
+
x.report(:jbuilder, &jbuilder)
|
446
|
+
x.report(:jserializer, &jserializer)
|
447
|
+
x.report(:jsonapi, &jsonapi)
|
448
|
+
x.report(:jsonapi_same_format, &jsonapi_same_format)
|
449
|
+
x.report(:panko, &panko)
|
450
|
+
x.report(:primalize, &primalize)
|
451
|
+
x.report(:rails, &rails)
|
452
|
+
x.report(:representable, &representable)
|
453
|
+
x.report(:simple_ams, &simple_ams)
|
454
|
+
|
455
|
+
x.compare!
|
456
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "juso"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/juso.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/juso/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "juso"
|
7
|
+
spec.version = Juso::VERSION
|
8
|
+
spec.authors = ["ykpythemind"]
|
9
|
+
spec.email = ["yukibukiyou@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Simple json serializer"
|
12
|
+
spec.description = "juso is simple json serializer for web application"
|
13
|
+
spec.homepage = "https://github.com/ykpythemind/juso"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.4.0"
|
16
|
+
|
17
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'https://mygemserver.com'"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/ykpythemind/juso"
|
21
|
+
spec.metadata["changelog_uri"] = "https://github.com/ykpythemind/juso"
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
# Uncomment to register a new dependency of your gem
|
33
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
34
|
+
|
35
|
+
# For more information and examples about making a new gem, checkout our
|
36
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
37
|
+
end
|
data/lib/juso/version.rb
ADDED
data/lib/juso.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "juso/version"
|
4
|
+
|
5
|
+
require 'json'
|
6
|
+
require 'date'
|
7
|
+
|
8
|
+
module Juso
|
9
|
+
class Error < StandardError; end
|
10
|
+
|
11
|
+
module Serializable
|
12
|
+
def as_juso_json(_)
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Juso Context is serializer context
|
18
|
+
# xxxxx
|
19
|
+
class Context
|
20
|
+
def initialize(serializer_type: :default)
|
21
|
+
@serializer_type = serializer_type.to_sym
|
22
|
+
end
|
23
|
+
|
24
|
+
attr_reader :serializer_type
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.generate(object, context: Context.new)
|
28
|
+
JSON.fast_generate(_generate(object, context))
|
29
|
+
end
|
30
|
+
|
31
|
+
# generate returns hash (as json)
|
32
|
+
def self._generate(object, context)
|
33
|
+
case object
|
34
|
+
when nil, Numeric, String
|
35
|
+
return object
|
36
|
+
when Hash
|
37
|
+
return object.each_with_object({}) do |(k, v), acc|
|
38
|
+
acc[k] = _generate(v, context)
|
39
|
+
end
|
40
|
+
when Serializable
|
41
|
+
# respond_to?(:as_juso_json) のほうが良い可能性ある?
|
42
|
+
return _generate(object.as_juso_json(context), context)
|
43
|
+
when *collection_classes
|
44
|
+
return object.to_a.map { |o| _generate(o, context) }
|
45
|
+
when *date_classes
|
46
|
+
return object.iso8601
|
47
|
+
else
|
48
|
+
# TODO: fallback to respond_to?(:as_juso_json) and warn?
|
49
|
+
|
50
|
+
raise Error.new("cannot serialize object: #{object}. you must include Juso::Serializable")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.collection_classes
|
55
|
+
@collection_classes
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.reset_collection_classes
|
59
|
+
@collection_classes =
|
60
|
+
if defined?(ActiveRecord)
|
61
|
+
[Array, ActiveRecord::Relation, ActiveRecord::Associations::CollectionProxy]
|
62
|
+
else
|
63
|
+
[Array]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.date_classes
|
68
|
+
# TODO: FIX / memorize
|
69
|
+
if defined?(ActiveSupport::TimeWithZone)
|
70
|
+
[Date, DateTime, ActiveSupport::TimeWithZone]
|
71
|
+
else
|
72
|
+
[Date, DateTime]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
reset_collection_classes
|
77
|
+
end
|
data/misc/juso.png
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: juso
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ykpythemind
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-11-23 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: juso is simple json serializer for web application
|
14
|
+
email:
|
15
|
+
- yukibukiyou@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".github/workflows/main.yml"
|
21
|
+
- ".gitignore"
|
22
|
+
- ".rubocop.yml"
|
23
|
+
- CODE_OF_CONDUCT.md
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- benchmark/single_resource.rb
|
30
|
+
- bin/console
|
31
|
+
- bin/setup
|
32
|
+
- juso.gemspec
|
33
|
+
- lib/juso.rb
|
34
|
+
- lib/juso/version.rb
|
35
|
+
- misc/juso.png
|
36
|
+
homepage: https://github.com/ykpythemind/juso
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata:
|
40
|
+
homepage_uri: https://github.com/ykpythemind/juso
|
41
|
+
source_code_uri: https://github.com/ykpythemind/juso
|
42
|
+
changelog_uri: https://github.com/ykpythemind/juso
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: 2.4.0
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubygems_version: 3.2.22
|
59
|
+
signing_key:
|
60
|
+
specification_version: 4
|
61
|
+
summary: Simple json serializer
|
62
|
+
test_files: []
|