Merge branch 'develop' into feature/JSONValue-base

# Conflicts:
#	MVMCore/MVMCore/Models/Extensions/Encoder.swift

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-02-15 10:40:23 -06:00
commit 5f3daa573f
5 changed files with 62 additions and 13 deletions

View File

@ -9,13 +9,6 @@
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
typedef NS_ENUM(NSInteger, MVMAppContext) {
MVMAppContextNone = 0,
MVMAppContextMF,
MVMAppContextMFPrepay,
MVMAppContextContentTransfer
};
typedef NS_ENUM(NSInteger, NavigationType) {
NavigationTypePush = 0,
NavigationTypeSet,

View File

@ -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),

View File

@ -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
}
}

View File

@ -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)

View File

@ -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.