Latest 1.0.9
Homepage https://github.com/qonceptual/QwikJson
License MIT
Platforms tvos 9.0, ios 7.0, osx 10.8, watchos 2.0, requires ARC
Frameworks CoreData
Authors

[![CI Status](http://img.shields.io/travis/Logan Sease/QwikJson.svg?style=flat)](https://travis-ci.org/Logan Sease/QwikJson)
Version
License
Platform

Summary

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 QwikJson. An amazingly powerful and simple library for serializing and deserializing json objects.

Simply have your model classes extend QwikJson and the world shall become your oyster.

QwikJson makes converting objects to dictionaries and arrays of dictionaries (and Vice Versa) a breeze. It includes support for nested model objects, nested array model objects, multiple styles of dates and times, simple storage to NSUserDefaults and conversion to and from JSON Strings.

Installation

QwikJson is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "QwikJson"

And import the following Header file

#import "QwikJson.h"

This pod is written in Objective-C but works great with Swift projects as well.

Usage

make a model class and extend QwikJson, and add your fields

//menu.h
@interface Menu : QwikJson
@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) and custom date serlizers

//restaurant.h
@interface Restaurant : QwikJson
@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];
}

Perform Specialized Logic during serialization or deserialization.


//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];
}

Define a property map to name your object fields differently for your model objects vs the API. This may be necessary if you use a reserved keyword like "description" or if your api returns underscore cased fields

+(NSDictionary<NSString*,NSString*>*)apiToObjectMapping
{
    //specify custom field mappings for qwikJsonObjects
    return @{@"description": @"descriptionText"};
}

Write straight to preferences

[self.restaurant writeToPreferencesWithKey:@"data"];
self.restaurant = [Restaurant readFromPrefencesWithKey:@"data"];

Convert to and from Strings

@interface NSDictionary (QwikJson)
-(NSString*)toJsonString;
+(NSDictionary*)fromJsonString:(NSString*)json;
@end

@interface NSArray (QwikJson)
-(NSString*)toJsonString;
+(NSArray*)fromJsonString:(NSString*)json;
@end

Supported Field Types Types

  • Boolean
  • NSString
  • NSArray
  • NSNumber

Custom Date Serializers, handle parsing various date / time formats

DBDate

2015-12-30

DBDateTime

2015-01-01T10:15:30

DBDateTimeStamp

0312345512

DBTime

12:00:00

Note that you can customize the date formats by calling setDateFormat on the date class.

[DBDate setDateFormat:@"MM/DD/YYYY"];

NSManagedObject Support

If you are using CoreData and would like to use QwikJson, you may also simply import and extend QwikJsonManagedObject instead of QwikJson

Android

Inside this repo and in the android directory, you will also find a very similar class, QwikJson.java that offers very similar functionality for Android and other Java Platforms.

Further Notes

In addition to parsing and serializing JSON, the other essential pieice of communicatiing with RESTful APIs is a good
networking library.
Consider using QwikHttp in combination with this library to complete your toolset.
https://github.com/qonceptual/QwikHttp

Also, checkout the SeaseAssist pod for a ton of great helpers to make writing your iOS code even simpler!
https://github.com/logansease/SeaseAssist

Author

Logan Sease, [email protected]

License

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

Latest podspec

{
    "name": "QwikJson",
    "version": "1.0.9",
    "summary": "A deep serialization and deserialization base object.",
    "description": "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.nnTo solve this, I introduce QwikJson. An amazingly powerful and simple library for serializing and deserializing json objects.nnSimple have your model classes extend the QwikJson class and the world shall become your oyster.nnQwikJson 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.",
    "homepage": "https://github.com/qonceptual/QwikJson",
    "license": "MIT",
    "authors": {
        "Logan Sease": "[email protected]"
    },
    "source": {
        "git": "https://github.com/qonceptual/QwikJson.git",
        "tag": "1.0.9"
    },
    "platforms": {
        "tvos": "9.0",
        "ios": "7.0",
        "osx": "10.8",
        "watchos": "2.0"
    },
    "requires_arc": true,
    "source_files": "Pod/Classes/**/*",
    "frameworks": "CoreData"
}

Pin It on Pinterest

Share This