json-schema-serializer 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +39 -2
- data/lib/json/schema/serializer.rb +2 -0
- 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: d1a4d5629a30b0c5aedc94644fc57e231477a4987c338487ca579ec9141cad5e
|
4
|
+
data.tar.gz: 33df5ee8f27d068a35fa1637caeea613afa650dbbcee5dd62a24945c1b06c846
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae32409a4b3d94544a1f6e2ae4a1863762d40d6e8c62e53ddb289ce3782b0ae634a59cf9d8a30c5cfd5ef6f48b6a06fa5cdbfa092229934d10939ba4ec79d723
|
7
|
+
data.tar.gz: f0585aba113213442c38167ad46864b5402ae72893b5bb72d7e18316e287f40c1c7d0881478ad328d75e984c172bab7ea3981dda0bbf46611558d01b2de55fab
|
data/README.md
CHANGED
@@ -31,12 +31,49 @@ schema = {
|
|
31
31
|
properties: {
|
32
32
|
id: { type: "integer" },
|
33
33
|
name: { type: "string" },
|
34
|
+
fuzzy: { type: ["string", "integer", "null"] },
|
34
35
|
},
|
36
|
+
required: ["id"],
|
35
37
|
}
|
36
38
|
|
37
39
|
serializer = JSON::Schema::Serializer.new(schema)
|
38
|
-
|
39
|
-
|
40
|
+
|
41
|
+
serializer.serialize({id: "42", name: "me", foo: "bar", fuzzy: "1000"})
|
42
|
+
# => {"id"=>42, "name"=>"me", "fuzzy"=>"1000"}
|
43
|
+
# "42" -> 42! type coerced!
|
44
|
+
|
45
|
+
serializer.serialize({id: "42", name: "me", fuzzy: 42})
|
46
|
+
# => {"id"=>42, "name"=>"me", "fuzzy"=>42}
|
47
|
+
serializer.serialize({id: "42", name: "me"})
|
48
|
+
# => {"id"=>42, "name"=>"me", "fuzzy"=>nil}
|
49
|
+
# multiple type auto select!
|
50
|
+
|
51
|
+
serializer.serialize({})
|
52
|
+
# => {"id"=>0, "name"=>nil, "fuzzy"=>nil}
|
53
|
+
# nil -> 0! required property's type coerced!
|
54
|
+
|
55
|
+
serializer.serialize({id: 10, name: "I don't need null keys!"}).compact
|
56
|
+
# => {"id"=>10, "name"=>"I don't need null keys!"}
|
57
|
+
# compact it!
|
58
|
+
|
59
|
+
class A
|
60
|
+
def id
|
61
|
+
42
|
62
|
+
end
|
63
|
+
end
|
64
|
+
serializer.serialize(A.new)
|
65
|
+
# => {"id"=>42, "name"=>nil, "fuzzy"=>nil}
|
66
|
+
# method also allowed
|
67
|
+
|
68
|
+
class Schema
|
69
|
+
def type
|
70
|
+
:string
|
71
|
+
end
|
72
|
+
end
|
73
|
+
serializer2 = JSON::Schema::Serializer.new(Schema.new)
|
74
|
+
serializer2.serialize(32)
|
75
|
+
# => "32"
|
76
|
+
# non-hash schema allowed
|
40
77
|
```
|
41
78
|
|
42
79
|
### Caution
|
@@ -19,7 +19,9 @@ module JSON
|
|
19
19
|
|
20
20
|
def walk(schema, obj, required)
|
21
21
|
type = try_hash(schema, :type)
|
22
|
+
default = try_hash(schema, :default)
|
22
23
|
format = try_hash(schema, :format)
|
24
|
+
obj = default if obj.nil?
|
23
25
|
type_coerce(schema, detect_type(type, obj), format, obj, required)
|
24
26
|
end
|
25
27
|
|