From 5c1b88bfabc0b922b533b0e85fb07762c34904f5 Mon Sep 17 00:00:00 2001 From: lmcmz Date: Thu, 26 Oct 2023 02:50:10 +0800 Subject: [PATCH] Add sortedKeys for encoder --- Sources/Decode/FlowArgument+Decode.swift | 2 +- Sources/Flow.swift | 11 +++++++++++ Sources/Models/FlowArgument.swift | 3 +-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Sources/Decode/FlowArgument+Decode.swift b/Sources/Decode/FlowArgument+Decode.swift index 4d685df..863c9fe 100644 --- a/Sources/Decode/FlowArgument+Decode.swift +++ b/Sources/Decode/FlowArgument+Decode.swift @@ -230,7 +230,7 @@ extension Flow.Argument: FlowCodable { } private func modelToDict(result: T) -> [String: Any]? { - guard let data = try? JSONEncoder().encode(result), + guard let data = try? flow.encoder.encode(result), let model = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else { return nil diff --git a/Sources/Flow.swift b/Sources/Flow.swift index 6abe32c..963cbcc 100644 --- a/Sources/Flow.swift +++ b/Sources/Flow.swift @@ -38,6 +38,17 @@ public final class Flow { /// The access API client public private(set) var accessAPI: FlowAccessProtocol + internal var encoder: JSONEncoder { + let encoder = JSONEncoder() + encoder.outputFormatting = .sortedKeys + return encoder + } + + internal var decoder: JSONDecoder { + let decoder = JSONDecoder() + return decoder + } + /// Default access client will be HTTP Client init() { accessAPI = FlowHTTPAPI(chainID: chainID) diff --git a/Sources/Models/FlowArgument.swift b/Sources/Models/FlowArgument.swift index ce5c586..b0c2c75 100644 --- a/Sources/Models/FlowArgument.swift +++ b/Sources/Models/FlowArgument.swift @@ -35,8 +35,7 @@ public extension Flow { /// Encode argument into json data. public var jsonData: Data? { - let encoder = JSONEncoder() - guard let jsonData = try? encoder.encode(self) else { + guard let jsonData = try? flow.encoder.encode(self) else { return nil } return jsonData