diff --git a/Sources/KituraContracts/BodyDecoder.swift b/Sources/KituraContracts/BodyDecoder.swift new file mode 100644 index 0000000..21f41c0 --- /dev/null +++ b/Sources/KituraContracts/BodyDecoder.swift @@ -0,0 +1,27 @@ +/** + * Copyright IBM Corporation 2018 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +import Foundation + +/** + A class that conforms to `BodyDecoder` must be able to decode from `Data` into a `Codable` type. + This class can then be used to produce input objects for a Codable route. + */ +public protocol BodyDecoder: AnyObject { + func decode(_ type: T.Type, from data: Data) throws -> T +} + +extension JSONDecoder: BodyDecoder {} diff --git a/Sources/KituraContracts/BodyEncoder.swift b/Sources/KituraContracts/BodyEncoder.swift new file mode 100644 index 0000000..9141cee --- /dev/null +++ b/Sources/KituraContracts/BodyEncoder.swift @@ -0,0 +1,27 @@ +/** + * Copyright IBM Corporation 2018 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +import Foundation + +/** + A class that conforms to `BodyEncoder` must be able to encode a `Codable` type into `Data`. + This class can then be used to produce output objects for a Codable route. + */ +public protocol BodyEncoder: AnyObject { + func encode(_ value: T) throws -> Data +} +extension JSONEncoder: BodyEncoder {} + diff --git a/Sources/KituraContracts/CodableQuery/QueryDecoder.swift b/Sources/KituraContracts/CodableQuery/QueryDecoder.swift index 0a78cd6..fb8028b 100644 --- a/Sources/KituraContracts/CodableQuery/QueryDecoder.swift +++ b/Sources/KituraContracts/CodableQuery/QueryDecoder.swift @@ -38,7 +38,7 @@ import LoggerAPI - Non-optional `Bool` decodes to `false` - All other non-optional types throw a decoding error */ -public class QueryDecoder: Coder, Decoder { +public class QueryDecoder: Coder, Decoder, BodyDecoder { /** The coding key path. diff --git a/Sources/KituraContracts/CodableQuery/QueryEncoder.swift b/Sources/KituraContracts/CodableQuery/QueryEncoder.swift index d5708e8..c6702e0 100644 --- a/Sources/KituraContracts/CodableQuery/QueryEncoder.swift +++ b/Sources/KituraContracts/CodableQuery/QueryEncoder.swift @@ -37,7 +37,7 @@ extension CharacterSet { } ```` */ -public class QueryEncoder: Coder, Encoder { +public class QueryEncoder: Coder, Encoder, BodyEncoder { /** A `[String: String]` dictionary.