diff --git a/README.md b/README.md index ea3422311..3a02e5907 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,11 @@ func decode(_ type: T.Type, from data: String) throws -> T func decodeIfPresent(_ type: T.Type, from data: String) throws -> T? ``` +##### Combine + +When used on Apple platforms where the Combine framework is available, all the encoders conform to Combine's +`TopLevelEncoder` and all decoders conform to its `TopLevelDecoder`. + ### Polymorphic Encoding/Decoding `Codable` encoders and decoders are a great tool and very convenient. Unfortunately using them with polymorphic types is cumbersome, diff --git a/Sources/PotentASN1/ASN1Decoder.swift b/Sources/PotentASN1/ASN1Decoder.swift index 6c15fee9b..ecafe385b 100644 --- a/Sources/PotentASN1/ASN1Decoder.swift +++ b/Sources/PotentASN1/ASN1Decoder.swift @@ -679,3 +679,14 @@ extension SchemaState { } } + + +#if canImport(Combine) + +import Combine + +extension ASN1Decoder : TopLevelDecoder { + public typealias Input = Data +} + +#endif diff --git a/Sources/PotentASN1/ASN1Encoder.swift b/Sources/PotentASN1/ASN1Encoder.swift index dcf22d335..69224af06 100644 --- a/Sources/PotentASN1/ASN1Encoder.swift +++ b/Sources/PotentASN1/ASN1Encoder.swift @@ -565,3 +565,14 @@ extension SchemaState { } } + + +#if canImport(Combine) + +import Combine + +extension ASN1Encoder : TopLevelEncoder { + public typealias Output = Data +} + +#endif diff --git a/Sources/PotentCBOR/CBORDecoder.swift b/Sources/PotentCBOR/CBORDecoder.swift index a99f7cd36..19ee2a5e0 100644 --- a/Sources/PotentCBOR/CBORDecoder.swift +++ b/Sources/PotentCBOR/CBORDecoder.swift @@ -383,3 +383,14 @@ private let _iso8601Formatter: DateFormatter = { formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX" return formatter }() + + +#if canImport(Combine) + +import Combine + +extension CBORDecoder : TopLevelDecoder { + public typealias Input = Data +} + +#endif diff --git a/Sources/PotentCBOR/CBOREncoder.swift b/Sources/PotentCBOR/CBOREncoder.swift index cd7b18756..69c9de40a 100644 --- a/Sources/PotentCBOR/CBOREncoder.swift +++ b/Sources/PotentCBOR/CBOREncoder.swift @@ -119,3 +119,14 @@ private let _iso8601Formatter: DateFormatter = { formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX" return formatter }() + + +#if canImport(Combine) + +import Combine + +extension CBOREncoder : TopLevelEncoder { + public typealias Output = Data +} + +#endif diff --git a/Sources/PotentCodables/AnyValue/AnyValueDecoder.swift b/Sources/PotentCodables/AnyValue/AnyValueDecoder.swift index c1501a23f..83de4cb6d 100644 --- a/Sources/PotentCodables/AnyValue/AnyValueDecoder.swift +++ b/Sources/PotentCodables/AnyValue/AnyValueDecoder.swift @@ -155,3 +155,18 @@ public struct AnyValueDecoderTransform: InternalDecoderTransform { } } + + +#if canImport(Combine) + +import Combine + +extension AnyValueDecoder : TopLevelDecoder { + public typealias Input = AnyValue + + public func decode(_ type: T.Type, from tree: AnyValue) throws -> T where T : Decodable { + return try decodeTree(type, from: tree) + } +} + +#endif diff --git a/Sources/PotentCodables/AnyValue/AnyValueEncoder.swift b/Sources/PotentCodables/AnyValue/AnyValueEncoder.swift index 0ef3e6317..578f40fe4 100644 --- a/Sources/PotentCodables/AnyValue/AnyValueEncoder.swift +++ b/Sources/PotentCodables/AnyValue/AnyValueEncoder.swift @@ -148,3 +148,18 @@ public struct AnyValueEncoderTransform: InternalEncoderTransform { } } + + +#if canImport(Combine) + +import Combine + +extension AnyValueEncoder : TopLevelEncoder { + public typealias Output = AnyValue + + public func encode(_ value: T) throws -> AnyValue where T : Encodable { + return try encodeTree(value) + } +} + +#endif diff --git a/Sources/PotentJSON/JSONDecoder.swift b/Sources/PotentJSON/JSONDecoder.swift index 697fc9a6f..8d43bc7b1 100644 --- a/Sources/PotentJSON/JSONDecoder.swift +++ b/Sources/PotentJSON/JSONDecoder.swift @@ -359,3 +359,14 @@ private let _iso8601Formatter: DateFormatter = { formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX" return formatter }() + + +#if canImport(Combine) + +import Combine + +extension JSONDecoder : TopLevelDecoder { + public typealias Input = Data +} + +#endif diff --git a/Sources/PotentJSON/JSONEncoder.swift b/Sources/PotentJSON/JSONEncoder.swift index 55dad98d0..581f7a4e0 100644 --- a/Sources/PotentJSON/JSONEncoder.swift +++ b/Sources/PotentJSON/JSONEncoder.swift @@ -297,3 +297,14 @@ private let _iso8601Formatter: DateFormatter = { formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX" return formatter }() + + +#if canImport(Combine) + +import Combine + +extension JSONEncoder : TopLevelEncoder { + public typealias Output = Data +} + +#endif