json-schema-serializer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aa99cc9446a97fd7d69624a5618fa53c4cba390ef4b008073ee3aaa65e7a18f7
4
+ data.tar.gz: bd775b351f47f8292ff70df162586d0822d814b9f3230ff7b7b7fe8caf9584c7
5
+ SHA512:
6
+ metadata.gz: e050ecaccb1a4de337b76a8c310c77b572c996e5689da63ea2008c899ace3d9cc1bc800e455349912d767f48d76d00fa09e2b4405ce49dc93707798a5d5ae350
7
+ data.tar.gz: 825b13e2e6c0bc51fbc0b41e18ec9785c44168cd098819e07afdb79c7a41a2ab6775e237f377c736d6406b9e4a3b7c41420e10768af87983d1f543322645721c
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --format documentation
2
+ --color
3
+ --require rspec-power_assert
4
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.5
7
+ before_install: gem install bundler -v 1.17.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.1.0
4
+
5
+ - released
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in json-schema-serializer.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2019 Narazaka
2
+
3
+ This software is provided 'as-is', without any express or implied
4
+ warranty. In no event will the authors be held liable for any damages
5
+ arising from the use of this software.
6
+
7
+ Permission is granted to anyone to use this software for any purpose,
8
+ including commercial applications, and to alter it and redistribute it
9
+ freely, subject to the following restrictions:
10
+
11
+ 1. The origin of this software must not be misrepresented; you must not
12
+ claim that you wrote the original software. If you use this software
13
+ in a product, an acknowledgment in the product documentation would be
14
+ appreciated but is not required.
15
+
16
+ 2. Altered source versions must be plainly marked as such, and must not be
17
+ misrepresented as being the original software.
18
+
19
+ 3. This notice may not be removed or altered from any source
20
+ distribution.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # Json::Schema::Serializer
2
+
3
+ JSON Schema based serializer
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'json-schema-serializer'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install json-schema-serializer
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require "json/schema/serializer"
25
+
26
+ schema = {
27
+ type: "object",
28
+ properties: {
29
+ id: { type: "integer" },
30
+ name: { type: "string" },
31
+ },
32
+ }
33
+
34
+ serializer = JSON::Schema::Serializer.new(schema)
35
+ serializer.serialize({id: 42, name: "me", foo: "bar"})
36
+ # => {"id"=>42, "name"=>"me"}
37
+ ```
38
+
39
+ ### Caution
40
+
41
+ `JSON::Schema::Serializer` does not resolve `$ref` so use external resolver.
42
+
43
+ with `json_refs` gem example:
44
+
45
+ ```ruby
46
+ require "json_refs"
47
+ require "json/schema/serializer"
48
+
49
+
50
+ schema = {
51
+ "type" => "object",
52
+ "properties" => {
53
+ "foo" => { "type" => "integer" },
54
+ "bar" => { "$ref" => "#/properties/foo" },
55
+ },
56
+ }
57
+
58
+ serializer = JSON::Schema::Serializer.new(JsonRefs.(schema))
59
+ serializer.serialize({foo: 0, bar: "42"})
60
+ # => {"foo"=>0, "bar"=>42}
61
+ ```
62
+
63
+ ## License
64
+
65
+ Zlib License
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+
71
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Narazaka/json-schema-serializer.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "json/schema/serializer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require "pry"
10
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "json/schema/serializer/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json-schema-serializer"
8
+ spec.version = Json::Schema::Serializer::VERSION
9
+ spec.authors = ["Narazaka"]
10
+ spec.email = ["info@narazaka.net"]
11
+ spec.licenses = ['Zlib']
12
+
13
+ spec.summary = %q{JSON Schema based serializer}
14
+ spec.homepage = "https://github.com/Narazaka/json-schema-serializer"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/Narazaka/json-schema-serializer.git"
21
+ spec.metadata["changelog_uri"] = "https://github.com/Narazaka/json-schema-serializer/blob/master/CHANGELOG.md"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against " \
24
+ "public gem pushes."
25
+ end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_development_dependency "bundler", "~> 2.0"
37
+ spec.add_development_dependency "rake", "~> 10.5"
38
+ spec.add_development_dependency "rspec", "~> 3.9"
39
+ spec.add_development_dependency "rspec-power_assert", "~> 1.1"
40
+ spec.add_development_dependency "pry-byebug", "~> 3.7"
41
+ end
@@ -0,0 +1,178 @@
1
+ require "json/schema/serializer/version"
2
+ require "date"
3
+ require "set"
4
+
5
+ module JSON
6
+ class Schema
7
+ class Serializer
8
+ def initialize(obj)
9
+ @schema = obj
10
+ end
11
+
12
+ def serialize(obj)
13
+ Walker.walk(@schema, obj, true)
14
+ end
15
+
16
+ class Walker
17
+ class << self
18
+ TimeWithZone = defined?(ActiveSupport::TimeWithZone) ? ActiveSupport::TimeWithZone : nil
19
+
20
+ def walk(schema, obj, required)
21
+ type = try_hash(schema, :type)
22
+ format = try_hash(schema, :format)
23
+ type_coerce(schema, detect_type(type, obj), format, obj, required)
24
+ end
25
+
26
+ def detect_type(type, obj)
27
+ return type unless type.is_a?(Array)
28
+ type = Set.new(type.map(&:to_s))
29
+
30
+ case obj
31
+ when nil
32
+ case
33
+ when type.include?("null")
34
+ "null"
35
+ else
36
+ type.first
37
+ end
38
+ when DateTime, Date, Time, TimeWithZone
39
+ case
40
+ when type.include?("string")
41
+ "string"
42
+ when type.include?("number")
43
+ "number"
44
+ when type.include?("integer")
45
+ "integer"
46
+ else
47
+ type.first
48
+ end
49
+ when String
50
+ case
51
+ when type.include?("string")
52
+ "string"
53
+ else
54
+ type.first
55
+ end
56
+ when Integer
57
+ case
58
+ when type.include?("integer")
59
+ "integer"
60
+ when type.include?("number")
61
+ "number"
62
+ when type.include?("string")
63
+ "string"
64
+ when type.include?("boolean")
65
+ "boolean"
66
+ else
67
+ type.first
68
+ end
69
+ when Float
70
+ case
71
+ when type.include?("number")
72
+ "number"
73
+ when type.include?("string")
74
+ "string"
75
+ when type.include?("integer")
76
+ "integer"
77
+ when type.include?("boolean")
78
+ "boolean"
79
+ else
80
+ type.first
81
+ end
82
+ when true, false
83
+ case
84
+ when type.include?("boolean")
85
+ "boolean"
86
+ else
87
+ type.first
88
+ end
89
+ when Array
90
+ case
91
+ when type.include?("array")
92
+ "array"
93
+ else
94
+ type.first
95
+ end
96
+ else
97
+ case
98
+ when type.include?("object")
99
+ "object"
100
+ else
101
+ type.first
102
+ end
103
+ end
104
+ end
105
+
106
+ def type_coerce(schema, type, format, obj, required)
107
+ return nil if !required && obj.nil?
108
+
109
+ case type.to_s
110
+ when "null"
111
+ nil
112
+ when "string"
113
+ case obj
114
+ when nil
115
+ ""
116
+ when DateTime, Date, Time, TimeWithZone
117
+ case format.to_s
118
+ when "date-time"
119
+ obj.strftime("%FT%T%:z")
120
+ when "date"
121
+ obj.strftime("%F")
122
+ when "time"
123
+ obj.strftime("%T%:z")
124
+ else
125
+ obj.to_s
126
+ end
127
+ else
128
+ obj.to_s
129
+ end
130
+ when "integer"
131
+ case obj
132
+ when true
133
+ 1
134
+ when false
135
+ 0
136
+ else
137
+ obj.to_i
138
+ end
139
+ when "number"
140
+ case obj
141
+ when true
142
+ 1.0
143
+ when false
144
+ 0.0
145
+ else
146
+ obj.to_f
147
+ end
148
+ when "boolean"
149
+ obj == true
150
+ when "array"
151
+ items_schema = try_hash(schema, :items)
152
+ obj.nil? ? [] : obj.map {|item| walk(items_schema, item, true) }
153
+ when "object"
154
+ properties_schema = try_hash(schema, :properties)
155
+ required_schema = Set.new(try_hash(schema, :required)&.map(&:to_s))
156
+ properties_schema.map {|name, property_schema|
157
+ [
158
+ name.to_s,
159
+ walk(property_schema, try_hash(obj, name), required_schema.include?(name.to_s)),
160
+ ]
161
+ }.to_h
162
+ end
163
+ end
164
+
165
+ private
166
+
167
+ def try_hash(obj, name)
168
+ if obj.respond_to?(:"[]")
169
+ obj[name] || obj[name.is_a?(Symbol) ? name.to_s : name.to_sym]
170
+ elsif obj.respond_to?(name)
171
+ obj.send(name)
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,7 @@
1
+ module Json
2
+ module Schema
3
+ module Serializer
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json-schema-serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Narazaka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-power_assert
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.7'
83
+ description:
84
+ email:
85
+ - info@narazaka.net
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CHANGELOG.md
94
+ - Gemfile
95
+ - LICENSE
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - json-schema-serializer.gemspec
101
+ - lib/json/schema/serializer.rb
102
+ - lib/json/schema/serializer/version.rb
103
+ homepage: https://github.com/Narazaka/json-schema-serializer
104
+ licenses:
105
+ - Zlib
106
+ metadata:
107
+ homepage_uri: https://github.com/Narazaka/json-schema-serializer
108
+ source_code_uri: https://github.com/Narazaka/json-schema-serializer.git
109
+ changelog_uri: https://github.com/Narazaka/json-schema-serializer/blob/master/CHANGELOG.md
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubygems_version: 3.0.3
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: JSON Schema based serializer
129
+ test_files: []