Compare commits
3 Commits
develop
...
feature/Mo
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2c8e07bea | ||
|
|
02b5ba6b7b | ||
|
|
316c0a04ae |
@ -34,6 +34,15 @@ extension Encodable {
|
|||||||
return jsonAny
|
return jsonAny
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func toJSONArray() throws -> [Any] {
|
||||||
|
let data = try self.encode()
|
||||||
|
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
|
||||||
|
guard let jsonArray = json as? [Any] else {
|
||||||
|
throw JSONError.error(message: "JSON Array not found")
|
||||||
|
}
|
||||||
|
return jsonArray
|
||||||
|
}
|
||||||
|
|
||||||
public func toJSONString() -> String? {
|
public func toJSONString() -> String? {
|
||||||
guard let json = self.toJSON(),
|
guard let json = self.toJSON(),
|
||||||
let data = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
|
let data = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
|
||||||
|
|||||||
@ -13,6 +13,7 @@ public typealias JSONValueDictionary = [String: JSONValue]
|
|||||||
|
|
||||||
public enum JSONValueError: Error {
|
public enum JSONValueError: Error {
|
||||||
case encode
|
case encode
|
||||||
|
case TypeMismatch
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Optional {
|
extension Optional {
|
||||||
@ -68,6 +69,38 @@ public enum JSONValue: Codable, Equatable {
|
|||||||
case .null: break
|
case .null: break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public var base: Any {
|
||||||
|
switch self {
|
||||||
|
case .string(let string): return string
|
||||||
|
case .int(let int): return int
|
||||||
|
case .double(let double): return double
|
||||||
|
case .bool(let bool): return bool
|
||||||
|
case .object(let object): return try! object.toJSONAny()
|
||||||
|
case .array(let array): return try! array.toJSONArray()
|
||||||
|
case .null: return NSNull()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func value<T>() throws -> T {
|
||||||
|
guard let base = self.base as? T else {
|
||||||
|
throw JSONValueError.TypeMismatch
|
||||||
|
}
|
||||||
|
return base
|
||||||
|
}
|
||||||
|
|
||||||
|
private func value<T>(type: T.Type) throws -> T {
|
||||||
|
let base: T = try value()
|
||||||
|
return base
|
||||||
|
}
|
||||||
|
|
||||||
|
public func toString() throws -> String { try value(type: String.self) }
|
||||||
|
public func toDouble() throws -> Double { try value(type: Double.self) }
|
||||||
|
public func toInt() throws -> Int { try value(type: Int.self) }
|
||||||
|
public func toBool() throws -> Bool { try value(type: Bool.self) }
|
||||||
|
public func toArray<T>(of type: T.Type) throws -> [T]{ try value(type: [T].self) }
|
||||||
|
public func toObject<T>(of type: T.Type) throws -> T { try value(type: T.self) }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func ==(lhs: JSONValue, rhs: JSONValue) -> Bool {
|
public func ==(lhs: JSONValue, rhs: JSONValue) -> Bool {
|
||||||
|
|||||||
@ -365,10 +365,11 @@ public extension UnkeyedDecodingContainer {
|
|||||||
}
|
}
|
||||||
// Now get the decoder to use for the type
|
// Now get the decoder to use for the type
|
||||||
let decoder = try self.superDecoder()
|
let decoder = try self.superDecoder()
|
||||||
if let model = try type.init(from: decoder) as? T {
|
let model = try type.init(from: decoder)
|
||||||
models.append(model)
|
if let found = model as? T {
|
||||||
|
models.append(found)
|
||||||
} else {
|
} else {
|
||||||
MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderError: \(typeCodingKey)")
|
MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderError: \(identifier)")
|
||||||
throw ModelRegistry.Error.decoderError
|
throw ModelRegistry.Error.decoderError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user