emery 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/emery/jsoner.rb CHANGED
@@ -3,211 +3,209 @@ require "date"
3
3
 
4
4
  require "emery/type"
5
5
 
6
- module Emery
7
- class JsonerError < StandardError
6
+ class JsonerError < StandardError
7
+ end
8
+
9
+ module Jsoner
10
+ T::StringFormatted.class_eval do
11
+ def jsoner_deserialize(json_value)
12
+ T.check(self, json_value)
13
+ end
14
+ def jsoner_serialize(value)
15
+ T.check(self, value)
16
+ end
8
17
  end
9
18
 
10
- module Jsoner
11
- T::StringFormatted.class_eval do
12
- def jsoner_deserialize(json_value)
13
- T.check(self, json_value)
19
+ T::ArrayType.class_eval do
20
+ def jsoner_deserialize(json_value)
21
+ T.check_not_nil(self, json_value)
22
+ if !json_value.is_a?(Array)
23
+ raise JsonerError.new("JSON value type #{json_value.class} is not Array")
14
24
  end
15
- def jsoner_serialize(value)
16
- T.check(self, value)
25
+ json_value.map { |item_json_value| Jsoner.deserialize(self.item_type, item_json_value) }
26
+ end
27
+ def jsoner_serialize(value)
28
+ if !value.is_a?(Array)
29
+ raise JsonerError.new("Value type #{json_value.class} is not Array")
17
30
  end
31
+ value.map { |item| Jsoner.serialize(self.item_type, item) }
18
32
  end
33
+ end
19
34
 
20
- T::ArrayType.class_eval do
21
- def jsoner_deserialize(json_value)
22
- T.check_not_nil(self, json_value)
23
- if !json_value.is_a?(Array)
24
- raise JsonerError.new("JSON value type #{json_value.class} is not Array")
25
- end
26
- json_value.map { |item_json_value| Jsoner.deserialize(self.item_type, item_json_value) }
35
+ T::HashType.class_eval do
36
+ def jsoner_deserialize(json_value)
37
+ T.check_not_nil(self, json_value)
38
+ if self.key_type != String
39
+ raise JsonerError.new("Hash key type #{self.key_type} is not supported for JSON (de)serialization - key should be String")
27
40
  end
28
- def jsoner_serialize(value)
29
- if !value.is_a?(Array)
30
- raise JsonerError.new("Value type #{json_value.class} is not Array")
31
- end
32
- value.map { |item| Jsoner.serialize(self.item_type, item) }
41
+ if !json_value.is_a?(Hash)
42
+ raise JsonerError.new("JSON value type #{json_value.class} is not Hash")
33
43
  end
44
+ json_value.map do |key, value|
45
+ [T.check(self.key_type, key), Jsoner.deserialize(self.value_type, value)]
46
+ end.to_h
34
47
  end
35
-
36
- T::HashType.class_eval do
37
- def jsoner_deserialize(json_value)
38
- T.check_not_nil(self, json_value)
39
- if self.key_type != String
40
- raise JsonerError.new("Hash key type #{self.key_type} is not supported for JSON (de)serialization - key should be String")
41
- end
42
- if !json_value.is_a?(Hash)
43
- raise JsonerError.new("JSON value type #{json_value.class} is not Hash")
44
- end
45
- json_value.map do |key, value|
46
- [T.check(self.key_type, key), Jsoner.deserialize(self.value_type, value)]
47
- end.to_h
48
+ def jsoner_serialize(value)
49
+ if self.key_type != String
50
+ raise JsonerError.new("Hash key type #{self.key_type} is not supported for JSON (de)serialization - key should be String")
48
51
  end
49
- def jsoner_serialize(value)
50
- if self.key_type != String
51
- raise JsonerError.new("Hash key type #{self.key_type} is not supported for JSON (de)serialization - key should be String")
52
- end
53
- if !value.is_a?(Hash)
54
- raise JsonerError.new("Value type #{value.class} is not Hash")
55
- end
56
- value.map do |key, value|
57
- [T.check(self.key_type, key), Jsoner.serialize(self.value_type, value)]
58
- end.to_h
52
+ if !value.is_a?(Hash)
53
+ raise JsonerError.new("Value type #{value.class} is not Hash")
59
54
  end
55
+ value.map do |key, value|
56
+ [T.check(self.key_type, key), Jsoner.serialize(self.value_type, value)]
57
+ end.to_h
60
58
  end
59
+ end
61
60
 
62
- T::AnyType.class_eval do
63
- def jsoner_deserialize(json_value)
64
- types.each do |type|
65
- begin
66
- return Jsoner.deserialize(type, json_value)
67
- rescue TypeError
68
- end
61
+ T::AnyType.class_eval do
62
+ def jsoner_deserialize(json_value)
63
+ types.each do |type|
64
+ begin
65
+ return Jsoner.deserialize(type, json_value)
66
+ rescue TypeError
69
67
  end
70
- raise JsonerError.new("Value '#{json_value.inspect.to_s}' can not be deserialized as any of #{@types.map { |t| t.to_s}.join(', ')}")
71
- end
72
- def jsoner_serialize(value)
73
- T.check(self, value)
74
- type = types.find {|t| T.instance_of?(t, value) }
75
- Jsoner.serialize(type, value)
76
68
  end
69
+ raise JsonerError.new("Value '#{json_value.inspect.to_s}' can not be deserialized as any of #{@types.map { |t| t.to_s}.join(', ')}")
77
70
  end
71
+ def jsoner_serialize(value)
72
+ T.check(self, value)
73
+ type = types.find {|t| T.instance_of?(t, value) }
74
+ Jsoner.serialize(type, value)
75
+ end
76
+ end
78
77
 
79
- T::UnionType.class_eval do
80
- def jsoner_deserialize(json_value)
81
- if !json_value.is_a?(Hash)
82
- raise JsonerError.new("JSON value type #{json_value.class} is not Hash")
83
- end
84
- if json_value.keys.length != 1
85
- raise JsonerError.new("JSON value #{json_value} should have only one key to represent union type, found #{json_value.keys.length}")
86
- end
87
- case_key = json_value.keys[0]
88
- if not cases.key? case_key.to_sym
89
- raise JsonerError.new("JSON key '#{case_key}' does not match any case in union type #{self}")
90
- end
91
- type = cases[case_key.to_sym]
92
- case_json_value = json_value[case_key]
93
- return Jsoner.deserialize(type, case_json_value)
78
+ T::UnionType.class_eval do
79
+ def jsoner_deserialize(json_value)
80
+ if !json_value.is_a?(Hash)
81
+ raise JsonerError.new("JSON value type #{json_value.class} is not Hash")
94
82
  end
95
- def jsoner_serialize(value)
96
- T.check(self, value)
97
- type = types.find {|t| T.instance_of?(t, value) }
98
- case_key = cases.key(type)
99
- result = { case_key => Jsoner.serialize(type, value) }
100
- result
83
+ if json_value.keys.length != 1
84
+ raise JsonerError.new("JSON value #{json_value} should have only one key to represent union type, found #{json_value.keys.length}")
101
85
  end
86
+ case_key = json_value.keys[0]
87
+ if not cases.key? case_key.to_sym
88
+ raise JsonerError.new("JSON key '#{case_key}' does not match any case in union type #{self}")
89
+ end
90
+ type = cases[case_key.to_sym]
91
+ case_json_value = json_value[case_key]
92
+ return Jsoner.deserialize(type, case_json_value)
93
+ end
94
+ def jsoner_serialize(value)
95
+ T.check(self, value)
96
+ type = types.find {|t| T.instance_of?(t, value) }
97
+ case_key = cases.key(type)
98
+ result = { case_key => Jsoner.serialize(type, value) }
99
+ result
102
100
  end
101
+ end
103
102
 
104
- T::Nilable.class_eval do
105
- def jsoner_deserialize(json_value)
106
- if json_value != nil
107
- Jsoner.deserialize(self.type, json_value)
108
- else
109
- nil
110
- end
103
+ T::Nilable.class_eval do
104
+ def jsoner_deserialize(json_value)
105
+ if json_value != nil
106
+ Jsoner.deserialize(self.type, json_value)
107
+ else
108
+ nil
111
109
  end
112
- def jsoner_serialize(value)
113
- if value != nil
114
- Jsoner.serialize(self.type, value)
115
- else
116
- nil
117
- end
110
+ end
111
+ def jsoner_serialize(value)
112
+ if value != nil
113
+ Jsoner.serialize(self.type, value)
114
+ else
115
+ nil
118
116
  end
119
117
  end
118
+ end
120
119
 
121
- module FloatSerializer
122
- def self.jsoner_deserialize(json_value)
123
- T.check(T.any(Float, Integer), json_value)
124
- json_value.to_f
125
- end
126
- def self.jsoner_serialize(value)
127
- T.check(Float, value)
128
- end
120
+ module FloatSerializer
121
+ def self.jsoner_deserialize(json_value)
122
+ T.check(T.any(Float, Integer), json_value)
123
+ json_value.to_f
124
+ end
125
+ def self.jsoner_serialize(value)
126
+ T.check(Float, value)
129
127
  end
128
+ end
130
129
 
131
- module DateTimeSerializer
132
- def self.jsoner_deserialize(json_value)
133
- T.check(String, json_value)
134
- begin
135
- DateTime.strptime(json_value, '%Y-%m-%dT%H:%M:%S')
136
- rescue
137
- raise JsonerError.new("Failed to parse DateTime from '#{json_value.inspect.to_s}' format %Y-%m-%dT%H:%M:%S is required")
138
- end
139
- end
140
- def self.jsoner_serialize(value)
141
- T.check(DateTime, value)
142
- value.strftime('%Y-%m-%dT%H:%M:%S')
130
+ module DateTimeSerializer
131
+ def self.jsoner_deserialize(json_value)
132
+ T.check(String, json_value)
133
+ begin
134
+ DateTime.strptime(json_value, '%Y-%m-%dT%H:%M:%S')
135
+ rescue
136
+ raise JsonerError.new("Failed to parse DateTime from '#{json_value.inspect.to_s}' format %Y-%m-%dT%H:%M:%S is required")
143
137
  end
144
138
  end
139
+ def self.jsoner_serialize(value)
140
+ T.check(DateTime, value)
141
+ value.strftime('%Y-%m-%dT%H:%M:%S')
142
+ end
143
+ end
145
144
 
146
- module DateSerializer
147
- def self.jsoner_deserialize(json_value)
148
- T.check(String, json_value)
149
- begin
150
- Date.strptime(json_value, '%Y-%m-%d')
151
- rescue
152
- raise JsonerError.new("Failed to parse Date from '#{json_value.inspect.to_s}' format %Y-%m-%d is required")
153
- end
154
- end
155
- def self.jsoner_serialize(value)
156
- T.check(Date, value)
157
- value.strftime('%Y-%m-%d')
145
+ module DateSerializer
146
+ def self.jsoner_deserialize(json_value)
147
+ T.check(String, json_value)
148
+ begin
149
+ Date.strptime(json_value, '%Y-%m-%d')
150
+ rescue
151
+ raise JsonerError.new("Failed to parse Date from '#{json_value.inspect.to_s}' format %Y-%m-%d is required")
158
152
  end
159
153
  end
160
-
161
- @@serializers = {
162
- Float => FloatSerializer,
163
- Date => DateSerializer,
164
- DateTime => DateTimeSerializer
165
- }
166
- def self.add_serializer(type, serializer)
167
- @@serializers[type] = serializer
154
+ def self.jsoner_serialize(value)
155
+ T.check(Date, value)
156
+ value.strftime('%Y-%m-%d')
168
157
  end
158
+ end
169
159
 
170
- def Jsoner.from_json(type, json)
171
- data = JSON.parse(json)
172
- return deserialize(type, data)
173
- end
160
+ @@serializers = {
161
+ Float => FloatSerializer,
162
+ Date => DateSerializer,
163
+ DateTime => DateTimeSerializer
164
+ }
165
+ def self.add_serializer(type, serializer)
166
+ @@serializers[type] = serializer
167
+ end
174
168
 
175
- def Jsoner.deserialize(type, json_value)
176
- begin
177
- if type.methods.include? :jsoner_deserialize
178
- return type.jsoner_deserialize(json_value)
179
- elsif @@serializers.include? type
180
- return @@serializers[type].jsoner_deserialize(json_value)
181
- else
182
- if ![String, Float, Integer, TrueClass, FalseClass, NilClass].include? type
183
- raise JsonerError.new("Type #{type} is not supported in Jsoner deserialization")
184
- end
185
- return T.check(type, json_value)
169
+ def Jsoner.from_json(type, json)
170
+ data = JSON.parse(json)
171
+ return deserialize(type, data)
172
+ end
173
+
174
+ def Jsoner.deserialize(type, json_value)
175
+ begin
176
+ if type.methods.include? :jsoner_deserialize
177
+ return type.jsoner_deserialize(json_value)
178
+ elsif @@serializers.include? type
179
+ return @@serializers[type].jsoner_deserialize(json_value)
180
+ else
181
+ if ![String, Float, Integer, TrueClass, FalseClass, NilClass].include? type
182
+ raise JsonerError.new("Type #{type} is not supported in Jsoner deserialization")
186
183
  end
187
- rescue StandardError => error
188
- raise JsonerError.new(error.message)
184
+ return T.check(type, json_value)
189
185
  end
186
+ rescue StandardError => error
187
+ raise JsonerError.new(error.message)
190
188
  end
189
+ end
191
190
 
192
- def Jsoner.to_json(type, value)
193
- JSON.dump(serialize(type, value))
194
- end
191
+ def Jsoner.to_json(type, value)
192
+ JSON.dump(serialize(type, value))
193
+ end
195
194
 
196
- def Jsoner.serialize(type, value)
197
- begin
198
- if type.methods.include? :jsoner_serialize
199
- return type.jsoner_serialize(value)
200
- elsif @@serializers.include? type
201
- return @@serializers[type].jsoner_serialize(value)
202
- else
203
- if ![String, Float, Integer, TrueClass, FalseClass, NilClass].include? type
204
- raise JsonerError.new("Type #{type} is not supported in Jsoner serialization")
205
- end
206
- return T.check(type, value)
195
+ def Jsoner.serialize(type, value)
196
+ begin
197
+ if type.methods.include? :jsoner_serialize
198
+ return type.jsoner_serialize(value)
199
+ elsif @@serializers.include? type
200
+ return @@serializers[type].jsoner_serialize(value)
201
+ else
202
+ if ![String, Float, Integer, TrueClass, FalseClass, NilClass].include? type
203
+ raise JsonerError.new("Type #{type} is not supported in Jsoner serialization")
207
204
  end
208
- rescue StandardError => error
209
- raise JsonerError.new(error.message)
205
+ return T.check(type, value)
210
206
  end
207
+ rescue StandardError => error
208
+ raise JsonerError.new(error.message)
211
209
  end
212
210
  end
213
- end
211
+ end