TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Jan 2016 |
Maintained by Logan Sease.
In our ReSTful API world, we are constantly passing JSON objects to our api and receiving them back. Constantly serializating these objects to and from json string and dictionaries can be cumbersome and can make your model classes and data services start to fill up with boiler plate parsing code.
To solve this, I introduce QJsonable. An amazingly powerful and simple library for serializing and deserializing json objects.
Simple have your model classes extend the QJsonable class and the world shall become your oyster.
QJsonable makes converting objects to dictionaries and arrays of dictionaries a breeze. It includes support for nested model objects, nested array model objects, multiple date serializers, easily storing and loading objects from user defaults and converting your array arrays and dictionaries to json Strings and vice versa.
QJsonable is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "QJsonable"
And import the following Header file
#import "QJsonable.h"
This pod will work if your project uses objective-c or Swift.
make a model class and extend QJSonable, and add your fields
//menu.h
@interface Menu : QJsonable
@property(nonatomic,strong)NSString * name;
@property(nonatomic,strong)NSArray * menuItems;
@end
now you can convert from dictionaries and vice versa with ease
//deserialize
menu = [Menu objectFromDictionary:dictionary];
//serialize again
dictionary = [menu toDictionary];
Use Nested Objects (even nested arrays) use custom date serlizers
//restaurant.h
@interface Restaurant : QJsonable
@property(nonatomic,strong)NSString * image_url;
@property(nonatomic,strong)NSString * name;
@property(nonatomic,strong)NSArray * menus;
@property(nonatomic,strong)DBTimeStamp * createdAt;
@end
//restaurant.m
+(Class)classForKey:(NSString*)key
{
if([key isEqualToString:@"menus"])
{
return [Menu class];
}
if([key isEqualToString:@"createdAt"])
{
return [DBTimeStamp class];
}
return [super classForKey:key];
}
Customize field names if they don't match the database
+(Class)classForKey:(NSString*)key
{
if([key isEqualToString:@"menu_items"] || [key isEqualToString:@"menuItems"])
{
return [MenuItem class];
}
return [super classForKey:key];
}
//override in subclass to perform some custom deserizliation or change property keys
-(void)writeObjectFrom:(NSDictionary*)inputDictionary forKey:(NSString*)key toProperty:(NSString*)property
{
//adjust the property name since the database is formatted with _'s instead of camel case
if([property isEqualToString:@"menu_items"])
{
property = @"menuItems";
}
[super writeObjectFrom:inputDictionary forKey:key toProperty:property];
}
//override in subclass to specify a new key or perform some custom action on serialize
-(void)serializeObject:(NSObject*)object withKey:(NSString*)key toDictionary:(NSMutableDictionary*)dictionary
{
//adjust the property name since the database is formatted with _'s instead of camel case
if([key isEqualToString:@"menuItems"])
{
key = @"menu_items";
}
[super serializeObject:object withKey:key toDictionary:dictionary];
}
Write straight to preferences
[self.restaurant writeToPreferencesWithKey:@"data"];
self.restaurant = [Restaurant readFromPrefencesWithKey:@"data"];
Convert to and from Strings
@interface NSDictionary (QJson)
-(NSString*)toJsonString;
+(NSDictionary*)fromJsonString:(NSString*)json;
@end
@interface NSArray (QJson)
-(NSString*)toJsonString;
+(NSArray*)fromJsonString:(NSString*)json;
@end
-Boolean -NSString -NSArray -NSNumber
-2015-12-30
-2015-01-01T10:15:30
-0312345512
-12:00:00
Note that you can customize the date formats by calling
[DBDate setDateFormat:@"MM/DD/YYYY"];
Inside this repo and in the android directory, you will also find a very similar class, QJsonable.java that offers very similar functionality for Android and other Java Platforms.
Logan Sease, [email protected]
QJsonable is available under the MIT license. See the LICENSE file for more info.