3131
3232import Foundation
3333
34+ /// Top level encoder that converts `Codable` instances into loosely typed `[String: Any]`, `[Any]` or `Any`.
3435public final class KeyValueEncoder {
3536
37+ /// Contextual user-provided information for use during encoding.
3638 public var userInfo : [ CodingUserInfoKey : Any ]
39+
40+ /// The strategy to use for encoding `nil`. Defaults to `Optional<Any>.none` which can be cast to any optional type.
3741 public var nilEncodingStrategy : NilEncodingStrategy = . default
3842
43+ /// Initializes `self` with default strategies.
3944 public init ( ) {
4045 self . userInfo = [ : ]
4146 }
4247
48+ /// Encodes a value into a loosely typed key value type. May be a container `[Any]`, `[String: Any]`
49+ /// or any supported plist primitive `Bool`, `String`, `Int`, `UInt`, `URL`, `Data` or `Decimal`.
50+ /// - Parameter value: The `Encodable` value to encode.
51+ /// - Returns: The encoded value.
4352 public func encode< T> ( _ value: T ) throws -> Any ? where T: Encodable {
4453 try encodeValue ( value) . getValue ( strategy: nilEncodingStrategy)
4554 }
4655
47- func encodeValue < T > ( _ value : T ) throws -> EncodedValue where T : Encodable {
48- try Encoder ( userInfo : userInfo , nilEncodingStrategy : nilEncodingStrategy ) . encodeToValue ( value )
49- }
56+ /// Strategy used to encode nil values.
57+ public typealias NilEncodingStrategy = NilCodingStrategy
58+ }
5059
51- public enum NilEncodingStrategy {
52- case removed
53- case placeholder( Any , isNull: ( Any ) -> Bool )
60+ /// Strategy used to encode and decode nil values.
61+ public enum NilCodingStrategy {
62+ /// `nil` values are removed
63+ case removed
5464
55- public static let `default` = NilEncodingStrategy . placeholder ( Optional < Any > . none as Any , isNull: isOptionalNone)
56- public static let stringNull = NilEncodingStrategy . placeholder ( " $null " , isNull: { ( $0 as? String == " $null " ) } )
57- public static let nsNull = NilEncodingStrategy . placeholder ( NSNull ( ) , isNull: { $0 is NSNull } )
58- }
65+ /// `nil` values are substituted with a placeholder value
66+ case placeholder( Any , isNull: ( Any ) -> Bool )
67+
68+ /// `nil` values are substituted with `Optional<Any>.none`. Can be cast to any optional type.
69+ public static let `default` = NilCodingStrategy . placeholder ( Optional < Any > . none as Any , isNull: isOptionalNone)
70+
71+ /// `nil` values are substituted with `"$null"` placeholder string. Compatible with `PropertyListEncoder`.
72+ public static let stringNull = NilCodingStrategy . placeholder ( " $null " , isNull: { ( $0 as? String == " $null " ) } )
73+
74+ /// `nil` values are substituted with `"NSNull()"`. Compatible with `JSONSerialization`.
75+ public static let nsNull = NilCodingStrategy . placeholder ( NSNull ( ) , isNull: { $0 is NSNull } )
5976}
6077
6178extension KeyValueEncoder {
@@ -82,6 +99,10 @@ extension KeyValueEncoder {
8299 }
83100 }
84101 }
102+
103+ func encodeValue< T: Encodable > ( _ value: T ) throws -> EncodedValue {
104+ try Encoder ( userInfo: userInfo, nilEncodingStrategy: nilEncodingStrategy) . encodeToValue ( value)
105+ }
85106}
86107
87108extension KeyValueEncoder . NilEncodingStrategy {
0 commit comments