From 2e023eb156e640c65b8836f40d2253f12fe4a669 Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Thu, 11 Mar 2021 14:14:12 +0800 Subject: [PATCH] Add utils to convert from/to .NET DateTime,TimeSpan,byte[] --- Sources/ServiceStack/CoreTypes.swift | 44 +++++++++++++++---- Sources/ServiceStack/DateExtensions.swift | 12 +++-- .../JsonServiceClientTests.swift | 2 +- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/Sources/ServiceStack/CoreTypes.swift b/Sources/ServiceStack/CoreTypes.swift index ba7e29e..f3484c7 100644 --- a/Sources/ServiceStack/CoreTypes.swift +++ b/Sources/ServiceStack/CoreTypes.swift @@ -149,15 +149,22 @@ extension Double { } } +// From .NET TimeSpan (XSD Duration) to Swift TimeInterval +func fromTimeSpan(timeSpan:String) -> TimeInterval? { + return TimeInterval.fromString(timeSpan) +} + +// From Swift TimeInterval to .NET TimeSpan (XSD Duration) +func toTimeSpan(timeInterval:TimeInterval) -> String { + return timeInterval.toXsdDuration() +} + @propertyWrapper public struct TimeSpan { public var wrappedValue: TimeInterval? = 0 public init(wrappedValue: TimeInterval?) { self.wrappedValue = wrappedValue } - public static func parse(_ timeSpan:String) -> TimeInterval? { - return TimeInterval.fromString(timeSpan) - } } extension TimeSpan : Codable { @@ -195,18 +202,39 @@ public class TimeIntervalConveter : StringConvertible { } } + +// From .NET Guid to Guid string +func fromGuid(_ guid:String) -> String { + return guid +} + +// From Guid string to .NET Guid +func toGuid(_ guid:String) -> String { + return guid +} + +// From .NET byte[] (Base64 String) to Swift [UInt8] +func fromByteArray(_ base64String:String) -> [UInt8] { + if let data = Data(base64Encoded: base64String) { + return [UInt8](data) + } + return [] +} + +// From Swift [UInt8] to .NET byte[] (Base64 String) +func toByteArray(_ bytes:[UInt8]) -> String { + return Data(bytes).base64EncodedString() +} + public class UInt8Base64Converter : StringConvertible { public var forType = Reflect<[UInt8]>.typeName public func fromString(_ type: T.Type, _ string: String) -> T? { - if let data = Data(base64Encoded: string) { - return [UInt8](data) as? T - } - return [] as? T + return fromByteArray(string) as? T } public func toString(instance: T) -> String? { if let bytes = instance as? [UInt8] { - let to = Data(bytes).base64EncodedString() + let to = toByteArray(bytes) return to } return nil diff --git a/Sources/ServiceStack/DateExtensions.swift b/Sources/ServiceStack/DateExtensions.swift index 95b00f2..6e94467 100755 --- a/Sources/ServiceStack/DateExtensions.swift +++ b/Sources/ServiceStack/DateExtensions.swift @@ -11,10 +11,14 @@ #endif import Foundation -public class DateTime { - public static func parse(_ dateTime:String) -> Date? { - return Date.fromString(dateTime) - } +// From .NET DateTime (WCF JSON or ISO Date) to Swift Date +func fromDateTime(_ jsonDate:String) -> Date? { + return Date.fromString(jsonDate) +} + +// From Swift Date to .NET DateTime (WCF JSON Date) +func toDateTime(_ dateTime:Date) -> String { + return dateTime.jsonDate; } public extension Date { diff --git a/Tests/ServiceStackTests/JsonServiceClientTests.swift b/Tests/ServiceStackTests/JsonServiceClientTests.swift index bcdfd46..f201949 100755 --- a/Tests/ServiceStackTests/JsonServiceClientTests.swift +++ b/Tests/ServiceStackTests/JsonServiceClientTests.swift @@ -419,7 +419,7 @@ import XCTest to.intList = [4, 5, 6] to.stringArray = ["A", "B", "C"] to.stringList = ["D", "E", "F"] - to.byteArray = [65,66,67] + to.byteArray = fromByteArray("QUJD") //base64(ABC) to.pocoArray.append(createPoco("pocoArray")) to.pocoList.append(createPoco("pocoList")) to.pocoLookup["A"] = [createPoco("B"), createPoco("C")]