json-schema-serializer 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +38 -0
- data/lib/json/schema/serializer.rb +13 -7
- data/lib/json/schema/serializer/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 466a7da01b15a1e212581fdac863dbea33ff469fdc41aa8b158a7203f4e8082e
|
4
|
+
data.tar.gz: 29e92d4f1bb61a91ded2b2d88f8ab3afe7a2470678b25f6b3bf2e15f1998637c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8c9a3c7b6d722b06c898feb3b47fdc50eeaa7413a52d97df02e620fa8acb1834bee8bef88a13f112dec849d2efdf1090b68fd6d86d40e9237f8ef7d364b8bbc
|
7
|
+
data.tar.gz: 4ff50230eeaa258532d6219e319879805a9a596629b87f195f1d82c8aa048f1d8a9f9a66963598521f71f193363ba8684141e76bb81a28ed33b43feaf5f222ae
|
data/README.md
CHANGED
@@ -74,6 +74,44 @@ serializer2 = JSON::Schema::Serializer.new(Schema.new)
|
|
74
74
|
serializer2.serialize(32)
|
75
75
|
# => "32"
|
76
76
|
# non-hash schema allowed
|
77
|
+
|
78
|
+
#
|
79
|
+
# object injector allowed!
|
80
|
+
#
|
81
|
+
|
82
|
+
class FooSerializer
|
83
|
+
def initialize(model)
|
84
|
+
@model = model
|
85
|
+
end
|
86
|
+
|
87
|
+
def first
|
88
|
+
@model.first
|
89
|
+
end
|
90
|
+
|
91
|
+
def count
|
92
|
+
@model.size
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
serializer_injected = JSON::Schema::Serializer.new(
|
97
|
+
{
|
98
|
+
type: :object,
|
99
|
+
inject: :Foo,
|
100
|
+
properties: {
|
101
|
+
first: { type: :integer },
|
102
|
+
count: { type: :integer },
|
103
|
+
},
|
104
|
+
},
|
105
|
+
{
|
106
|
+
inject_key: :inject,
|
107
|
+
injectors: {
|
108
|
+
Foo: FooSerializer,
|
109
|
+
},
|
110
|
+
},
|
111
|
+
)
|
112
|
+
|
113
|
+
serializer_injected.serialize([1, 2, 3])
|
114
|
+
# => {"first"=>1, "count"=>3}
|
77
115
|
```
|
78
116
|
|
79
117
|
### Caution
|
@@ -5,24 +5,30 @@ require "set"
|
|
5
5
|
module JSON
|
6
6
|
class Schema
|
7
7
|
class Serializer
|
8
|
-
def initialize(obj)
|
8
|
+
def initialize(obj, options = {})
|
9
9
|
@schema = obj
|
10
|
+
@options = options
|
10
11
|
end
|
11
12
|
|
12
13
|
def serialize(obj)
|
13
|
-
Walker.walk(@schema, obj, true)
|
14
|
+
Walker.walk(@schema, obj, true, @options)
|
14
15
|
end
|
15
16
|
|
16
17
|
class Walker
|
17
18
|
class << self
|
18
19
|
TimeWithZone = defined?(ActiveSupport::TimeWithZone) ? ActiveSupport::TimeWithZone : nil
|
19
20
|
|
20
|
-
def walk(schema, obj, required)
|
21
|
+
def walk(schema, obj, required, options)
|
21
22
|
type = try_hash(schema, :type)
|
22
23
|
default = try_hash(schema, :default)
|
23
24
|
format = try_hash(schema, :format)
|
24
25
|
obj = default if obj.nil?
|
25
|
-
|
26
|
+
if options[:inject_key]
|
27
|
+
inject_key = try_hash(schema, options[:inject_key])
|
28
|
+
injector = try_hash(options[:injectors], inject_key) if inject_key
|
29
|
+
obj = injector.new(obj) if injector
|
30
|
+
end
|
31
|
+
type_coerce(schema, detect_type(type, obj), format, obj, required, options)
|
26
32
|
end
|
27
33
|
|
28
34
|
def detect_type(type, obj)
|
@@ -105,7 +111,7 @@ module JSON
|
|
105
111
|
end
|
106
112
|
end
|
107
113
|
|
108
|
-
def type_coerce(schema, type, format, obj, required)
|
114
|
+
def type_coerce(schema, type, format, obj, required, options)
|
109
115
|
return nil if !required && obj.nil?
|
110
116
|
|
111
117
|
case type.to_s
|
@@ -151,12 +157,12 @@ module JSON
|
|
151
157
|
obj == true
|
152
158
|
when "array"
|
153
159
|
items_schema = try_hash(schema, :items)
|
154
|
-
obj.nil? ? [] : obj.map { |item| walk(items_schema, item, true) }
|
160
|
+
obj.nil? ? [] : obj.map { |item| walk(items_schema, item, true, options) }
|
155
161
|
when "object"
|
156
162
|
properties_schema = try_hash(schema, :properties)
|
157
163
|
required_schema = Set.new(try_hash(schema, :required)&.map(&:to_s))
|
158
164
|
properties_schema.map do |name, property_schema|
|
159
|
-
[name.to_s, walk(property_schema, try_hash(obj, name), required_schema.include?(name.to_s))]
|
165
|
+
[name.to_s, walk(property_schema, try_hash(obj, name), required_schema.include?(name.to_s), options)]
|
160
166
|
end.to_h
|
161
167
|
end
|
162
168
|
end
|