BTJSON
Objective-C
@interface BTJSON : NSObject
Swift
class BTJSON : NSObject
A type-safe wrapper around JSON
The primary goal of this class is to two-fold: (1) prevent bugs by staying true to JSON (json.org) rather than interpreting it in mysterious ways; (2) prevent bugs by making JSON interpretation as un-surprising as possible.
Most notably, type casting occurs via the as* nullable methods; errors are deferred and can be checked explicitly using isError and asError.
Example Data:
{
"foo": "bar",
"baz": [1, 2, 3]
}
Example Usage:
let json : BTJSON = BTJSON(data:data);
json.isError // false
json.isObject // true
json.isNumber // false
json.asObject // self
json["foo"] // JSON(@"bar")
json["foo"].isString // true
json["foo"].asString // @"bar"
json["baz"].asString // null
json["baz"]["quux"].isError // true
json["baz"]["quux"].asError // NSError(domain: BTJSONErrorDomain, code: BTJSONErrorCodeTypeInvalid)
json["baz"][0].asError // null
json["baz"][0].asInteger //
json["random"]["nested"]["things"][3].isError // true
let json : BTJSON = BTJSON() // json.asJson => {}
json["foo"][0] = "bar" // json.asJSON => { "foo": ["bar"] }
json["baz"] = [ 1, 2, 3 ] // json.asJSON => { "foo": ["bar"], "baz": [1,2,3] }
json["quux"] = NSSet() // json.isError => true, json.asJSON => throws NSError(domain: BTJSONErrorDomain, code: BTJSONErrorInvalidData)
-
Designated initializer.
Declaration
Objective-C
- (nonnull instancetype)init;
Swift
init()
-
Initialize with a value.
Declaration
Objective-C
- (nonnull instancetype)initWithValue:(nonnull id)value;
Swift
convenience init(value: Any)
Parameters
value
The value to initialize with.
-
Initialize with a data.
Declaration
Objective-C
- (nonnull instancetype)initWithData:(nonnull NSData *)data;
Swift
convenience init(data: Data)
Parameters
data
The
NSData
to initialize with. -
Indexes into the JSON as if the current value is an object
Notably, this method will always return successfully; however, if the value is not an object, the JSON will wrap an error.
Declaration
Objective-C
- (nonnull BTJSON *)objectForKeyedSubscript:(nonnull NSString *)key;
Swift
subscript(key: String) -> BTJSON { get }
-
Indexes into the JSON as if the current value is an array
Notably, this method will always return successfully; however, if the value is not an array, the JSON will wrap an error.
Declaration
Objective-C
- (nonnull BTJSON *)objectAtIndexedSubscript:(NSUInteger)idx;
Swift
subscript(idx: UInt) -> BTJSON { get }
-
True if this instance of BTJSON is not valid.
Declaration
Objective-C
@property (nonatomic, readonly) BOOL isError;
Swift
var isError: Bool { get }
-
The BTJSON as
NSError
.Declaration
Objective-C
- (nullable NSError *)asError;
Swift
func asError() -> Error?
Return Value
An
NSError
representing the BTJSON instance. -
The BTJSON as
NSData
.Declaration
Objective-C
- (nullable NSData *)asJSONAndReturnError:(NSError *_Nullable *_Nullable)error;
Swift
func asJSON() throws -> Data
Parameters
error
Used if there is an issue parsing.
Return Value
An
NSData
representing the BTJSON instance. -
The BTJSON as a pretty string.
Declaration
Objective-C
- (nullable NSString *)asPrettyJSONAndReturnError: (NSError *_Nullable *_Nullable)error;
Swift
func asPrettyJSON() throws -> String
Parameters
error
Used if there is an issue parsing.
Return Value
An
NSString
representing the BTJSON instance. -
The BTJSON as
NSString
.Declaration
Objective-C
- (nullable NSString *)asString;
Swift
func asString() -> String?
Return Value
An
NSString
representing the BTJSON instance. -
The BTJSON as
NSArray<BTJSON *>
.Declaration
Objective-C
- (nullable NSArray<BTJSON *> *)asArray;
Swift
func asArray() -> [BTJSON]?
Return Value
An
NSArray<BTJSON *>
representing the BTJSON instance. -
The BTJSON as
NSDecimalNumber
.Declaration
Objective-C
- (nullable NSDecimalNumber *)asNumber;
Swift
func asNumber() -> NSDecimalNumber?
Return Value
An
NSDecimalNumber
representing the BTJSON instance. -
The BTJSON as
NSURL
.Declaration
Objective-C
- (nullable NSURL *)asURL;
Swift
func asURL() -> URL?
Return Value
An
NSURL
representing the BTJSON instance. -
The BTJSON as
NSArray<NSString *>
.Declaration
Objective-C
- (nullable NSArray<NSString *> *)asStringArray;
Swift
func asStringArray() -> [String]?
Return Value
An
NSArray<NSString *>
representing the BTJSON instance. -
The BTJSON as
NSDictionary
.Declaration
Objective-C
- (nullable NSDictionary *)asDictionary;
Swift
func asDictionary() -> [AnyHashable : Any]?
Return Value
An
NSDictionary
representing the BTJSON instance. -
The BTJSON as
NSInteger
. Zero will be returned if not a valid number.Declaration
Objective-C
- (NSInteger)asIntegerOrZero;
Swift
func asIntegerOrZero() -> Int
Return Value
An
NSInteger
representing the BTJSON instance. -
The BTJSON as Enum.
Declaration
Objective-C
- (NSInteger)asEnum:(nonnull NSDictionary *)mapping orDefault:(NSInteger)defaultValue;
Swift
func asEnum(_ mapping: [AnyHashable : Any], orDefault defaultValue: Int) -> Int
Parameters
mapping
The mapping dictionary to used to convert the value.
defaultValue
The default value if conversion fails.
Return Value
An Enum representing the BTJSON instance.
-
True if this instance of BTJSON is a valid string.
Declaration
Objective-C
@property (nonatomic, readonly) BOOL isString;
Swift
var isString: Bool { get }
-
True if this instance of BTJSON is a valid number.
Declaration
Objective-C
@property (nonatomic, readonly) BOOL isNumber;
Swift
var isNumber: Bool { get }
-
True if this instance of BTJSON is a valid array.
Declaration
Objective-C
@property (nonatomic, readonly) BOOL isArray;
Swift
var isArray: Bool { get }
-
True if this instance of BTJSON is a valid object.
Declaration
Objective-C
@property (nonatomic, readonly) BOOL isObject;
Swift
var isObject: Bool { get }
-
True if this instance of BTJSON is a boolean.
Declaration
Objective-C
@property (nonatomic, readonly) BOOL isBool;
Swift
var isBool: Bool { get }
-
True if this instance of BTJSON is a value representing
true
.Declaration
Objective-C
@property (nonatomic, readonly) BOOL isTrue;
Swift
var isTrue: Bool { get }
-
True if this instance of BTJSON is a value representing
false
.Declaration
Objective-C
@property (nonatomic, readonly) BOOL isFalse;
Swift
var isFalse: Bool { get }
-
True if this instance of BTJSON is
null
.Declaration
Objective-C
@property (nonatomic, readonly) BOOL isNull;
Swift
var isNull: Bool { get }