matthewcheok / Realm-JSON

A concise Mantle-like way of working with Realm and JSON.
MIT License
661 stars 129 forks source link

Use property default value if json value is null #51

Open drulang opened 9 years ago

drulang commented 9 years ago

Hey,

Just wondering if Realm-JSON can use a property's default value instead of trying to assign NSNull to the property which is causing Realm to crash?

Thanks! Dru

matthewcheok commented 9 years ago

Sounds like valid proposition. Mind submitting a PR?

komocode commented 9 years ago

If you're using AFNetworking and you're using AFHTTPSessionManager to make your calls, you can do this:

        _manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url];
        AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializer];
        [responseSerializer setRemovesKeysWithNullValues:YES];
        _manager.responseSerializer = responseSerializer;

This will remove keys that have null values. Then your models should use the default values since the keys with null values are no longer there.