RKDotNetDateFormatter stringFromDate never gets called

101 views
Skip to first unread message

Nick Curran

unread,
Jun 24, 2012, 10:24:08 AM6/24/12
to res...@googlegroups.com
I'm having trouble getting RestKit to serialize my dates correctly. It deserializes them fine, but on a post or put, the stringFromDate method on RKDotNetDateFormatter never gets called as far as I can tell.

Am I missing something in this setup that would get the deserialization to work correctly? 

// My setup:
NSDateFormatter* dateFormatter = [RKDotNetDateFormatter dotNetDateFormatterWithTimeZone:[NSTimeZone localTimeZone]];
[RKObjectMapping setDefaultDateFormatters: [NSArray arrayWithObject: dateFormatter]];
[RKObjectMapping setPreferredDateFormatter: dateFormatter];

...

[thingSerializationMapping mapKeyPath: @"sysCreateDate" toAttribute: @"SysCreateDate"];
[thingSerializationMapping mapKeyPath: @"sysCreateUserDate" toAttribute: @"SysCreateUserDate"];

...

[objectManager.mappingProvider setSerializationMapping: thingSerializationMapping forClass: [Thing class]];

...

[objectManager.router routeClass: [Thing class] toResourcePath: @"/Things/" forMethod: RKRequestMethodPOST];
[objectManager.router routeClass: [Thing class] toResourcePath: @"/Things(:id)" forMethod: RKRequestMethodPUT];

Bonnie Jaiswal

unread,
Oct 22, 2012, 6:07:05 AM10/22/12
to res...@googlegroups.com
Hey Nick , Did u solve that issue. I am also facing the same problem while Posting Objects.
i get the error
The server encountered an error processing the request. The exception message is 'There was an error deserializing the object of type Dtos.MeetingDto. DateTime content '2012-10-22 09:26:34 +0000' does not start with '\/Date(' and end with ')\/' as required for JSON.'.

Nick Curran

unread,
Oct 22, 2012, 8:26:39 AM10/22/12
to res...@googlegroups.com
I have not yet learned of a fix, no.

Blake Watters

unread,
Oct 22, 2012, 10:06:55 AM10/22/12
to res...@googlegroups.com
A fix for this was merged 3 months ago and is available on the development branch: See issue https://github.com/RestKit/RestKit/issues/726

Bonnie Jaiswal

unread,
Oct 23, 2012, 5:31:05 AM10/23/12
to res...@googlegroups.com
Thanks a lot Blake for pointing in the right direction,
  I did the changes as mentioned in the RKDotNetDateFormatter.m and added the stringForObjectValue: method but still found no luck,
  then i realized that in the Method

- (void)objectMappingOperation:(RKObjectMappingOperation *)operation didSetValue:(id)value forKeyPath:(NSString *)keyPath usingMapping:(RKObjectAttributeMapping *)mapping {
    id transformedValue = nil;
    Class orderedSetClass = NSClassFromString(@"NSOrderedSet");
    if ([value isKindOfClass:[NSDate class]]) {
        // Date's are not natively serializable, must be encoded as a string
        @synchronized(self.mapping.preferredDateFormatter) {
            
//-- Changed this line
             transformedValue = [[RKDotNetDateFormatter dotNetDateFormatterWithTimeZone:[NSTimeZone systemTimeZone]] stringForObjectValue:value];
        }

self.mapping.preferredDateFormatter was always NSDateFormatter , even if i set

 [objectMapping setPreferredDateFormatter:[RKDotNetDateFormatter dotNetDateFormatterWithTimeZone:[NSTimeZone systemTimeZone]]]; while initializing the objectmapping

as result the stringForObjectValue: methood was not called.

So i explicitly changed it to the above given line and it worked.
I know this is not the best way to do it, but i cant understand why self.mapping.preferredDateFormatter was always NSDateFormatter
here's what i get when i NSLog the preferredDateFormatter and dateFormatters Array
[objectMapping preferredDateFormatter] = <NSDateFormatter: 0x82b7c60>
[objectMapping dateFormatters] = (
    "<NSDateFormatter: 0x82b8340>",
    "<NSDateFormatter: 0x82b8270>",
    "<RKISO8601DateFormatter: 0x82b8710>"
)

ps: my objectMappings are for an NSManagedObject Class.

Thanks again.
Reply all
Reply to author
Forward
0 new messages