Latest 1.6.1
Homepage https://github.com/Rogaven/ODX.Serialization
License MIT
Platforms ios 5.0, osx 10.6, watchos 1.0, requires ARC
Dependencies ODX.Core, ODObjCRuntime
Authors

Version
License
Platform

ODX.Serialization is utility classes for serialization and deserialization Objective-C objects.
It can be used together with NSJSONSerialization or XMLDictionary, FMDB, etc.

Usage

Serialization

(NSObject+ODSerialization)

<NSObject> -(id)od_serialize;

Converts any object to NSDictionary or NSArray with NSStrings, NSNumbers and NSNulls. After this new object can be converted to JSON string.

Deserialization

(NSObject+ODDeserialization)

<NSObject> +od_constructWithObject:(NSObject *)srcObj error:(NSError **)error;

Create object of current class from NSDictionary. Using that it’s possible to convert json string to model object.

Example

Let’s create our model class:

@interface Obj : NSObject <ODDataObject>
@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) NSInteger count;
@property (nonatomic, strong) NSArray<Obj *> *items;
@end

@implementation Obj

// We implement ODDataObject protocol's method for specify class of object in `items` array
+ (Class)classOfIvarWithName:(NSString *)ivarName {
    if ([ivarName isEqualToString:@"_items"]) return Obj.class;
    return nil;
}

@end

Now, if we fill object and perform od_serialize method:

Obj *o = [Obj new];
o.title = @"Title";
o.count = 10;
o.items = @[[Obj new], [Obj new]];

NSLog(%"@", o.od_serialize);
/*
{
   count = 10;
   items = (
       {
           count = 0;
           items = "<null>";
           title = "<null>";
       },
       {
           count = 0;
           items = "<null>";
           title = "<null>";
       }
   );
   title = Title;
}
*/

NSLog(@"%@", [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:o.od_serialize
                                                                            options:0 error:nil]
                                                                            encoding:NSUTF8StringEncoding]);
// {"title":"Title","count":10,"items":[{"title":null,"count":0,"items":null},{"title":null,"count":0,"items":null}]}

Deserialization.

NSString *jsonString = @"{"title":"Title","count":10,"items":[{"title":null,"count":0,"items":null},{"title":null,"count":0,"items":null}]}";
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
Obj *obj = [Obj od_constructWithObject:obj error:nil];

Object debug

Installation

CocoaPods

ODX.Serialization is available through CocoaPods. It’s much more easier. To install
it, simply add the following line to your Podfile:

pod "ODX.Serialization"

Manual

For build ODX.Serialization as library you need to put ODObjcRuntime and ODX.Core projects in the same directory

Author

Alexey Nazaroff, [email protected]

License

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

Latest podspec

{
    "name": "ODX.Serialization",
    "version": "1.6.1",
    "summary": "Utility classes for serialization and deserialization Objective-C objects.",
    "homepage": "https://github.com/Rogaven/ODX.Serialization",
    "license": {
        "type": "MIT",
        "file": "LICENSE.txt"
    },
    "authors": {
        "Alexey Nazaroff": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Rogaven/ODX.Serialization.git",
        "tag": "1.6.1"
    },
    "platforms": {
        "ios": "5.0",
        "osx": "10.6",
        "watchos": "1.0"
    },
    "source_files": "src/**/*",
    "requires_arc": true,
    "dependencies": {
        "ODX.Core": [
            "~> 1.5"
        ],
        "ODObjCRuntime": [
            "~> 1.1"
        ]
    },
    "deprecated_in_favor_of": "ODSerialization"
}

Pin It on Pinterest

Share This