Skip to content

Type-safe Objective-C wrapper around NSJSONSerialization

License

Notifications You must be signed in to change notification settings

floschliep/FLOJSONObject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FLOJSONObject

This is a proof of concept and not a full-featured library.

FLOJSONObject provides a type-safe wrapper around NSJSONSerialization in Objective-C. Yes, you read correctly, Objective-C. This is not intended to be used with Swift, there are other, much better solutions for Swift.

FLOJSONObject attempts to remove the isKindOfClass-hell from your code by wrapping all objects parsed by NSJSONSerialization inside a FLOJSONObject object. The FLOJSONObject class allows you to access JSON objects using the following properties:

@property (nullable, nonatomic, readonly) NSArray<FLOJSONObject *> *array;
@property (nullable, nonatomic, readonly) NSMutableArray<FLOJSONObject *> *mutableArray;
@property (nullable, nonatomic, readonly) NSDictionary<NSString *, FLOJSONObject *> *dictionary;
@property (nullable, nonatomic, readonly) NSMutableDictionary<NSString *, FLOJSONObject *> *mutableDictionary;
@property (nullable, nonatomic, readonly) NSString *string;
@property (nullable, nonatomic, readonly) NSNumber *number;
@property (nullable, nonatomic, readonly) NSNull *null;

When accessing a property, the class checks whether the JSON object matches the desired type or not and returns either the object or nil.

Example

NSData *data = … // JSON: { "message": "Hello", "timestamp": 123456789 }
FLOJSONObject *object = [FLOJSONObject objectWithData:data options:kNilOptions error:nil];
NSDictionary<NSString *, FLOJSONObject *> *json = object.dictionary;
NSString *message = json[@"message"].string;
NSNumber *timestamp = json[@"timestamp"].number;

Even if the JSON didn't match the scheme you expected, not a single unrecognized selector sent to instance would be thrown.

Installation

Just drop the two files from the Source folder into your project. The Xcode project in this repository is only used for testing.

Contact

Florian Schliep

License

FLOJSONObject is available under the MIT license. See the LICENSE.txt file for more info.

About

Type-safe Objective-C wrapper around NSJSONSerialization

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published