diff --git a/Sources/IonicPortals/JSONDecoder+JSObject.swift b/Sources/IonicPortals/JSONDecoder+JSObject.swift index 51de223..b30b647 100644 --- a/Sources/IonicPortals/JSONDecoder+JSObject.swift +++ b/Sources/IonicPortals/JSONDecoder+JSObject.swift @@ -9,10 +9,14 @@ import Foundation import Capacitor public extension JSONDecoder { + @available(*, deprecated, renamed: "decodeJsObject") func decodeJSObject(_ type: T.Type, from object: JSObject) throws -> T { + try decodeJsObject(type, from: object) + } + + func decodeJsObject(_ type: T.Type, from object: JSObject) throws -> T { let data = try JSONSerialization.data(withJSONObject: object, options: []) - let result = try decode(T.self, from: data) - return result + return try decode(T.self, from: data) } } diff --git a/Sources/IonicPortals/JSONEncoder+JSObject.swift b/Sources/IonicPortals/JSONEncoder+JSObject.swift index e2415b4..0b09275 100644 --- a/Sources/IonicPortals/JSONEncoder+JSObject.swift +++ b/Sources/IonicPortals/JSONEncoder+JSObject.swift @@ -11,11 +11,18 @@ import Combine public extension JSONEncoder { + @available(*, deprecated, renamed: "encodeJsObject") func encodeJSObject(_ value: T) throws -> JSValue { + try encodeJsObject(value) + } + + func encodeJsObject(_ value: T) throws -> JSObject { let data = try encode(value) let dictionary = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary - - // Any valid Codable type should not fail here. + + // Any valid Codable type that is keyed should not fail here. + // An unkeyed or single value container would have failed in the + // JSONSerialization step. return JSTypes.coerceDictionaryToJSObject(dictionary)! } } diff --git a/Sources/IonicPortals/PortalsPlugin+Combine.swift b/Sources/IonicPortals/PortalsPlugin+Combine.swift index f24316f..bb8998c 100644 --- a/Sources/IonicPortals/PortalsPlugin+Combine.swift +++ b/Sources/IonicPortals/PortalsPlugin+Combine.swift @@ -104,7 +104,7 @@ extension PortalsPubSub.Publisher { /// - Returns: A publisher emitting the decoded value or a decoding error. public func decodeData(_ type: T.Type, decoder: JSONDecoder) -> AnyPublisher where T: Decodable { tryData(as: JSObject.self) - .tryMap { try decoder.decodeJSObject(T.self, from: $0) } + .tryMap { try decoder.decodeJsObject(T.self, from: $0) } .eraseToAnyPublisher() } } diff --git a/Tests/IonicPortalsTests/PortalsPluginTests.swift b/Tests/IonicPortalsTests/PortalsPluginTests.swift index fa7710e..b7c9ba1 100644 --- a/Tests/IonicPortalsTests/PortalsPluginTests.swift +++ b/Tests/IonicPortalsTests/PortalsPluginTests.swift @@ -141,7 +141,7 @@ class PortalsPluginTests: XCTestCase { .assertNoFailure() .expectOutput(toBe: card) - let jsObject = try JSONEncoder().encodeJSObject(card) + let jsObject = try JSONEncoder().encodeJsObject(card) PortalsPubSub.publish(jsObject, to: topic) wait(for: result, timeout: 1)