Do you need to handle the root cause of failure in decoding JSON?
We often process the value as a default value if it could not be decoded from JSON. (Recovering with a default value) However, doing that might cause a serious problem and hide the actual root cause in the app. Recovering with a default value is not a bad choice, it’s important to know the JSON represents unexpected shape or value before recovering.
let json: JSON
do {
self.id = try json.next("id").getString()
} catch {
print(error)
// We can know why decoding failed from error.
// Not found "id" or "id" found but it was not `string` or something else.
// that's why here recover the value to fill `self.id`
self.id = "unknown"
}
JAYSON provides 2 ways of accessing to JSON object.
Easy access (with dynamic-member-lookup)
let urlString: String? = json[3]?.shot?.images?.hidpi_image?.string
Strict access (with dynamic-member-lookup)
We can know where error was caused. (with JSONError)
let id: String = try json
.next(0)
.next("id")
.getString()
JAYSON
Strict and Scalable JSON library.
And also supports dynamicMemberLookup
Requirements
Swift 5+ iOS📱, watchOS⌚️, tvOS📺, macOS🖥, Linux✨
Usage
Read JSON
Easy Access
let urlString: String? = json[3]?["shot"]?["images"]?["hidpi_image"]?.string
Strict Access (try-catch)
if the value does not exist, throw JSONError
Failed location can be known from JSONError
Get Value (String, Bool, Number)
let id: String = try json
.next(0)
.next("id")
.getString()
Using dynamicMemberLookup
let id: String = try json
.next(0)
.next(\.id)
.getString()
Get Value with Decoder (Custom Object)
Using the Decoder can be transformed in a custom object.
And, throwable
extension JSON {
public func getDictionary() throws -> [String : JSON]
public func getArray() throws -> [JSON]
public func getNumber() throws -> NSNumber
public func getInt() throws -> Int
public func getInt8() throws -> Int8
public func getInt16() throws -> Int16
public func getInt32() throws -> Int32
public func getInt64() throws -> Int64
public func getUInt() throws -> UInt
public func getUInt8() throws -> UInt8
public func getUInt16() throws -> UInt16
public func getUInt32() throws -> UInt32
public func getUInt64() throws -> UInt64
public func getString() throws -> String
public func getBool() throws -> Bool
public func getFloat() throws -> Float
public func getDouble() throws -> Double
}
///
extension JSON {
public func get<T>(_ s: (JSON) throws -> T) rethrows -> T
}
Optional Read-only properties😁
extension JSON {
public var dictionary: [String : Any]? { get }
public var array: [Any]? { get }
public var string: String? { get }
public var number: NSNumber? { get }
public var double: Double? { get }
public var float: Float? { get }
public var int: Int? { get }
public var uInt: UInt? { get }
public var int8: Int8? { get }
public var uInt8: UInt8? { get }
public var int16: Int16? { get }
public var uInt16: UInt16? { get }
public var int32: Int32? { get }
public var uInt32: UInt32? { get }
public var int64: Int64? { get }
public var uInt64: UInt64? { get }
public var bool: Bool? { get }
}
Initialize JSON
let jsonData: Data = ...
let json = try JSON(data: jsonData)
let jsonData: Data
let json: Any = try JSONSerialization.jsonObject(with: data, options: [])
let json = try JSON(any: json)
let userInfo: [AnyHashable: Any]
let json = try JSON(any: json)
let objects: [Any]
let json = try JSON(any: json)
In the case of the following try it is not required.
let object: [String : JSON]
let json = JSON(object)
let object: [JSON]
let json = JSON(object)
let object: [JSONWritableType]
let json = JSON(object)
let object: [String : JSONWritableType]
let json = JSON(object)
If you have access that does not exist key, throw JSONError.
public enum JSONError: Error {
case notFoundKey(key: String, json: JSON)
case notFoundIndex(index: Int, json: JSON)
case failedToGetString(source: Any, json: JSON)
case failedToGetBool(source: Any, json: JSON)
case failedToGetNumber(source: Any, json: JSON)
case failedToGetArray(source: Any, json: JSON)
case failedToGetDictionary(source: Any, json: JSON)
case decodeError(source: Any, json: JSON, decodeError: Error)
case invalidJSONObject
}
Do you need to handle the root cause of failure in decoding JSON?
We often process the value as a default value if it could not be decoded from JSON. (Recovering with a default value)
However, doing that might cause a serious problem and hide the actual root cause in the app.
Recovering with a default value is not a bad choice, it’s important to know the JSON represents unexpected shape or value before recovering.
JAYSON provides 2 ways of accessing to JSON object.
We can know where error was caused. (with JSONError)
JAYSON
Strict and Scalable JSON library. And also supports
dynamicMemberLookup
Requirements
Swift 5+ iOS📱, watchOS⌚️, tvOS📺, macOS🖥, Linux✨
Usage
Read JSON
Easy Access
Strict Access (try-catch)
if the value does not exist, throw
JSONError
Failed location can be known from JSONError
Get Value (String, Bool, Number)
Using dynamicMemberLookup
Get Value with Decoder (Custom Object)
Using the Decoder can be transformed in a custom object. And, throwable
General Getter
Strict getters
Optional Read-only properties😁
Initialize JSON
In the case of the following try it is not required.
Get current path (Debugging information.)
JSONError
If you have access that does not exist key, throw
JSONError
.example:
Output jsonError
Go Back JSON hierarchy
Import Example (dribbble API)
Write JSON
-> data
json Convertible Examples
Installation
json is available through CocoaPods. To install it, simply add the following line to your Podfile:
Author
muukii, muukii.app@gmail.com
License
json is available under the MIT license. See the LICENSE file for more info.