Skip to content

Commit

Permalink
Merge pull request #10 from arttb/OptionalsSupport
Browse files Browse the repository at this point in the history
Added support for optional values for dictionary encoder and decoder;
  • Loading branch information
tattn authored Apr 8, 2019
2 parents 5244caa + 04d706c commit e86b079
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
16 changes: 14 additions & 2 deletions Sources/DictionaryDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ extension DictionaryDecoder {
return try decoder.unbox(value, as: T.self)
}

func decodeNil(forKey key: Key) throws -> Bool { throw decoder.notFound(key: key) }
func decodeNil(forKey key: Key) throws -> Bool {
guard let entry = self.container[key.stringValue] else {
throw decoder.notFound(key: key)
}

return entry is NSNull
}
func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool { return try _decode(type, forKey: key) }
func decode(_ type: Int.Type, forKey key: Key) throws -> Int { return try _decode(type, forKey: key) }
func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 { return try _decode(type, forKey: key) }
Expand Down Expand Up @@ -190,7 +196,13 @@ extension DictionaryDecoder {

func decodeNil() throws -> Bool {
try checkIndex(Any?.self)
return false

if self.container[self.currentIndex] is NSNull {
self.currentIndex += 1
return true
} else {
return false
}
}
func decode(_ type: Bool.Type) throws -> Bool { return try _decode(type) }
func decode(_ type: Int.Type) throws -> Int { return try _decode(type) }
Expand Down
6 changes: 3 additions & 3 deletions Sources/DictionaryEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ extension DictionaryEncoder {
storage.push(container: dictionary)
}

func encodeNil(forKey key: Key) throws {}
func encodeNil(forKey key: Key) throws { set(NSNull(), forKey: key.stringValue) }
func encode(_ value: Bool, forKey key: Key) throws { set(value, forKey: key.stringValue) }
func encode(_ value: Int, forKey key: Key) throws { set(value, forKey: key.stringValue) }
func encode(_ value: Int8, forKey key: Key) throws { set(value, forKey: key.stringValue) }
Expand Down Expand Up @@ -143,7 +143,7 @@ extension DictionaryEncoder {
storage.push(container: array)
}

func encodeNil() throws {}
func encodeNil() throws { push(NSNull()) }
func encode(_ value: Bool) throws {}
func encode(_ value: Int) throws { push(try encoder.box(value)) }
func encode(_ value: Int8) throws { push(try encoder.box(value)) }
Expand Down Expand Up @@ -200,7 +200,7 @@ extension DictionaryEncoder {
storage.push(container: array)
}

func encodeNil() throws {}
func encodeNil() throws { storage.push(container: NSNull()) }
func encode(_ value: Bool) throws { storage.push(container: value) }
func encode(_ value: Int) throws { storage.push(container: value) }
func encode(_ value: Int8) throws { storage.push(container: value) }
Expand Down

0 comments on commit e86b079

Please sign in to comment.