diff --git a/MVMCore/MVMCore/Constants/MVMCoreConstants.h b/MVMCore/MVMCore/Constants/MVMCoreConstants.h index e653ffe..21148ad 100644 --- a/MVMCore/MVMCore/Constants/MVMCoreConstants.h +++ b/MVMCore/MVMCore/Constants/MVMCoreConstants.h @@ -9,13 +9,6 @@ #import #import -typedef NS_ENUM(NSInteger, MVMAppContext) { - MVMAppContextNone = 0, - MVMAppContextMF, - MVMAppContextMFPrepay, - MVMAppContextContentTransfer -}; - typedef NS_ENUM(NSInteger, NavigationType) { NavigationTypePush = 0, NavigationTypeSet, diff --git a/MVMCore/MVMCore/Models/Extensions/Encoder.swift b/MVMCore/MVMCore/Models/Extensions/Encoder.swift index 754f055..9f50bc3 100644 --- a/MVMCore/MVMCore/Models/Extensions/Encoder.swift +++ b/MVMCore/MVMCore/Models/Extensions/Encoder.swift @@ -25,11 +25,15 @@ extension Encodable { return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? JSONDictionary } } - public func toJSONAny() -> [String: Any]? { - guard let data = try? self.encode() else { return nil } - return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? [String: Any] } + public func toJSONAny() throws -> [String: Any] { + let data = try self.encode() + let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) + guard let jsonAny = json as? [String: Any] else { + throw JSONError.error(message: "JSON Dictionary not found") + } + return jsonAny } - + public func toJSONString() -> String? { guard let json = self.toJSON(), let data = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted), diff --git a/MVMCore/MVMCore/Models/JSON/JSONHelper.swift b/MVMCore/MVMCore/Models/JSON/JSONHelper.swift index a6a61ad..a0c20d0 100644 --- a/MVMCore/MVMCore/Models/JSON/JSONHelper.swift +++ b/MVMCore/MVMCore/Models/JSON/JSONHelper.swift @@ -8,11 +8,54 @@ import Foundation -public typealias JSONArray = [[String: Any]] +public typealias JSONArray = [AnyHashable] public typealias JSONDictionary = [String: AnyHashable] public enum JSONError: Error { case pathNotFound case data(path: String) case other(error: Error) + case error(message: String) +} + +extension JSONDictionary { + + public func toJSONString(options: JSONSerialization.WritingOptions = []) throws -> String { + let data = try JSONSerialization.data(withJSONObject: self, options: options) + guard let string = String(data: data, encoding: .utf8) else { + throw JSONError.error(message: "Failed to convert data to string") + } + return string + } + + public func toUrlQueryItems() throws -> [URLQueryItem] { + var queryItems: [URLQueryItem] = [] + for (key, value) in self { + var valueString: String + if let value = value as? JSONDictionary { + valueString = try value.toJSONString() + } else if let value = value as? JSONArray { + valueString = try value.toJSONString() + } else { + guard let baseValueString = String(describing: value.base).removingPercentEncoding else { + throw JSONError.error(message: "query item failed: \(key) value \(value.base)") + } + valueString = baseValueString + } + let queryItem = URLQueryItem(name: key, value: valueString) + queryItems.append(queryItem) + } + return queryItems + } +} + +extension JSONArray { + + public func toJSONString(options: JSONSerialization.WritingOptions = []) throws -> String { + let data = try JSONSerialization.data(withJSONObject: self, options: options) + guard let string = String(data: data, encoding: .utf8) else { + throw JSONError.error(message: "Failed to convert data to string") + } + return string + } } diff --git a/MVMCore/MVMCore/Models/JSON/JSONValue.swift b/MVMCore/MVMCore/Models/JSON/JSONValue.swift index 062ec5d..2ce4a31 100644 --- a/MVMCore/MVMCore/Models/JSON/JSONValue.swift +++ b/MVMCore/MVMCore/Models/JSON/JSONValue.swift @@ -140,6 +140,13 @@ extension Dictionary where Key == String, Value == Any { } } +extension Dictionary where Key == String, Value == AnyHashable { + public func toJSONValue() throws -> [String: JSONValue] { + let data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted) + return try JSONDecoder().decode([String:JSONValue].self, from: data) + } +} + extension Dictionary where Key == String, Value == JSONValue { public func toJSONObject() throws -> JSONDictionary { let encoded = try JSONEncoder().encode(self) diff --git a/MVMCore/MVMCore/Utility/MVMCoreErrorObject.m b/MVMCore/MVMCore/Utility/MVMCoreErrorObject.m index d16a7a5..d61075b 100644 --- a/MVMCore/MVMCore/Utility/MVMCoreErrorObject.m +++ b/MVMCore/MVMCore/Utility/MVMCoreErrorObject.m @@ -91,7 +91,9 @@ NSString *type = [errorInfo string:KeyType]; if (![ValueTypeSuccess isEqualToString:type]) { - MVMCoreErrorObject *error = [[MVMCoreErrorObject alloc] initWithTitle:[errorInfo stringForKey:KeyErrorHeading] message:[errorInfo stringForKey:KeyUserMessage] messageToLog:[errorInfo stringForKey:KeyMessage] code:errorCode domain:ErrorDomainServer location:location]; + NSString *title = [errorInfo string:KeyErrorHeading] ?: [MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle]; + NSString *message = [errorInfo string:KeyUserMessage] ?: [MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess]; + MVMCoreErrorObject *error = [[MVMCoreErrorObject alloc] initWithTitle:title message:message messageToLog:[errorInfo string:KeyMessage] code:errorCode domain:ErrorDomainServer location:location]; if ([ValueTypeErrorScreen isEqualToString:type]) { // If this is a server error screen, there should be no additional alerts... It will be handled by the load handler.