Search code examples
iosjsonserializationrestkit

How do you use the RKDotNetDateFormatter in RestKit 0.20?


I am getting a JSON object back from a .NET service that has a date in the following format: /Date(1368825702750-0500)/

I need to be able to deserialize that format into an NSDate and then display the NSDate using a different format.

I am using RestKit and I can't seem to figure out how to tell it to use the proper date formatter. I know that there is a RKDotNetDateFormatter, but I can't figure out how to use it properly. Most of the examples I have seen on other sites use methods and classes that no longer exist in the current version of RestKit. Currently, I am trying to set it up as follows:

RKDotNetDateFormatter *formatter = [RKDotNetDateFormatter dotNetDateFormatterWithTimeZone:[NSTimeZone systemTimeZone]];
[RKObjectMapping addDefaultDateFormatter:formatter];
[RKObjectMapping setPreferredDateFormatter:formatter];

I have my JSON as a string at this point, so I am doing the following to deserialize it:

MyDisplayEntity *infoMapped = [[MyDisplayEntity alloc] init];

RKMappingOperation* mapper = [[RKMappingOperation alloc] initWithSourceObject:object destinationObject:infoMapped mapping:[MyDisplayEntity responseMapping]];
mapper.dataSource = object;
[mapper performMapping:&error];

Everything seems to get mapped correctly except for my NSDates.

Does anyone have an example of how to deserialize this format properly?


Solution

  • After digging way into the source code, I found that when I use the standard service retrieval methods, it doesn't seem to care if your mappings to your destination entity are cased exactly correct. Using the RKMappingOperation however requires them to be cased exactly right.

    Looks like it was user error here :P