Skip to content

Commit e86b079

Browse files
authored
Merge pull request #10 from arttb/OptionalsSupport
Added support for optional values for dictionary encoder and decoder;
2 parents 5244caa + 04d706c commit e86b079

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

Sources/DictionaryDecoder.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,13 @@ extension DictionaryDecoder {
100100
return try decoder.unbox(value, as: T.self)
101101
}
102102

103-
func decodeNil(forKey key: Key) throws -> Bool { throw decoder.notFound(key: key) }
103+
func decodeNil(forKey key: Key) throws -> Bool {
104+
guard let entry = self.container[key.stringValue] else {
105+
throw decoder.notFound(key: key)
106+
}
107+
108+
return entry is NSNull
109+
}
104110
func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool { return try _decode(type, forKey: key) }
105111
func decode(_ type: Int.Type, forKey key: Key) throws -> Int { return try _decode(type, forKey: key) }
106112
func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 { return try _decode(type, forKey: key) }
@@ -190,7 +196,13 @@ extension DictionaryDecoder {
190196

191197
func decodeNil() throws -> Bool {
192198
try checkIndex(Any?.self)
193-
return false
199+
200+
if self.container[self.currentIndex] is NSNull {
201+
self.currentIndex += 1
202+
return true
203+
} else {
204+
return false
205+
}
194206
}
195207
func decode(_ type: Bool.Type) throws -> Bool { return try _decode(type) }
196208
func decode(_ type: Int.Type) throws -> Int { return try _decode(type) }

Sources/DictionaryEncoder.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ extension DictionaryEncoder {
7474
storage.push(container: dictionary)
7575
}
7676

77-
func encodeNil(forKey key: Key) throws {}
77+
func encodeNil(forKey key: Key) throws { set(NSNull(), forKey: key.stringValue) }
7878
func encode(_ value: Bool, forKey key: Key) throws { set(value, forKey: key.stringValue) }
7979
func encode(_ value: Int, forKey key: Key) throws { set(value, forKey: key.stringValue) }
8080
func encode(_ value: Int8, forKey key: Key) throws { set(value, forKey: key.stringValue) }
@@ -143,7 +143,7 @@ extension DictionaryEncoder {
143143
storage.push(container: array)
144144
}
145145

146-
func encodeNil() throws {}
146+
func encodeNil() throws { push(NSNull()) }
147147
func encode(_ value: Bool) throws {}
148148
func encode(_ value: Int) throws { push(try encoder.box(value)) }
149149
func encode(_ value: Int8) throws { push(try encoder.box(value)) }
@@ -200,7 +200,7 @@ extension DictionaryEncoder {
200200
storage.push(container: array)
201201
}
202202

203-
func encodeNil() throws {}
203+
func encodeNil() throws { storage.push(container: NSNull()) }
204204
func encode(_ value: Bool) throws { storage.push(container: value) }
205205
func encode(_ value: Int) throws { storage.push(container: value) }
206206
func encode(_ value: Int8) throws { storage.push(container: value) }

0 commit comments

Comments
 (0)