Latest 1.6.1
Homepage https://github.com/Rogaven/ODX.Serialization
License MIT
Platforms ios 5.0, osx 10.6, watchos 1.0, tvos 9.0, requires ARC
Dependencies ODTransformation, ODValidation, ODRuntime
Frameworks Foundation
Authors

ODX.Serialization

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": "ODSerialization",
    "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",
        "tvos": "9.0"
    },
    "requires_arc": true,
    "frameworks": "Foundation",
    "source_files": "src/**/*",
    "public_header_files": "src/**/*.h",
    "dependencies": {
        "ODTransformation": [
            "~> 1.0"
        ],
        "ODValidation": [
            "~> 1.0"
        ],
        "ODRuntime": [
            "~> 1.2"
        ]
    },
    "prefix_header_contents": "#ifndef TARGET_OS_IOSn  #define TARGET_OS_IOS TARGET_OS_IPHONEn#endifn#ifndef TARGET_OS_WATCHn  #define TARGET_OS_WATCH 0n#endifn#ifndef TARGET_OS_TVn  #define TARGET_OS_TV 0n#endif"
}

Pin It on Pinterest

Share This