-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added millisecond support for NSDate objects #123
Conversation
Seems like an odd way of doing it. I think it may be more appropriate to implement a parsing method which is more flexible - allowing just dates without times, etc. also |
well it's partially my fault, because I didn't provide a complete iso parser in first place, but I can't merge ifs for each variation of the ios specification either ... |
Is there a parsing method which doesn't rely on using a date formatter? — On Tue, Nov 12, 2013 at 11:03 PM, Marin Todorov notifications@github.com
|
looks like a simple regex will take care of it. I was considering including an iso date parser as a submodule, but in the end I'd rather not have automatic dates than having a submodule |
…Formatter library
Thanks for taking a look at this! Poking around, I found the following project for iso date parsing: https://github.com/boredzo/iso-8601-date-formatter It would create a library dependency for JSONModel, but at least it's available in CocoaPods. Another, more flexible, option would be to allow the user to override the date format string to use. That way even non-ISO compliant dates could be used. Compliant date formats are better of course, but when writing a client for an existing API, there isn't always the option of having the API's maintainer fix their design. Even more flexible than that, the user could be allowed to provide a custom NSDateFormatter object. That way I could use ISO8601DateFormatter without having to create a library dependency for JSONModel. I updated my own branch of JSONModel to use the ISO8601DateFormatter library directly. There were several changes to support pulling in a library from Cocoapods, but really the only files that changed were JSONValueTransformer.m and JSONModel.podspec. The branch path is https://github.com/scottadav/JSONModel/tree/dates_with_millisec . Thanks, On 11/12/13 4:18 PM, Marin Todorov wrote: looks like a simple regex will take care of it. I was considering including an iso date parser as a submodule, but in the end I'd rather not have automatic dates than having a submodule — |
Scott ... there are a number of ways you can override the date conversions. |
That worked. Thanks for the tip. @interface JSONValueTransformer (NSDate) @implementation JSONValueTransformer (NSDate) Regards, On 11/13/13 2:10 PM, Marin Todorov wrote: Scott ... there are a number of ways you can override the date conversions. — |
@icanzilb What about adding a dependent cocoapod for the date formatting? I've started using the |
if there's a condition checking at runtime if the cocoa pod is available so JSONModel can work also without the ISO8601DateFormatter ... this could be a go |
Ah, that would be a great solution. The only thing is that without the use of |
the thing is I still love it that you can just download JSONModel as a zip file from github, easy peasy ... adding dependancies on other cocoa pods, etc will force everyone to use cocoapods and I don't want to do that |
That is true, though I think if not cocoapods, most people use submodules. If it was done cleverly based on presence, that would be nice. |
hi, i'm a big problem. The api tha i'm calling has the date formatter: how can put the value in an NSDate *start_date of JSONModel ? |
Er, that format should work - it is valid in the ISO8601 spec. Is it currently rejected? |
To be honest, I think we should bite the bullet and include a dependency. That's @icanzilb's call though. |
Or alternatively include the files in JSONModel with some kind of prefix to prevent collision. |
See #437. |
Suppose for a moment that the format of my date is not iso8601 .. I read the documentation.. you can use these methods. Where they should be placed? `@implementation JSONValueTransformer (CustomTransformer)
@end` |
You could do that. The code for that can be anywhere in your project. It might be better to use a custom setter in that case though - otherwise you're overriding NSDate serialization for every use of JSONModel in your entire project (including dependencies). |
@billinghamj Can you give an example of the custom setter you a referring to? Are you suggesting adding a custom setter on the NSDate property on the JSONModel subclass? |
@fergalrooney e.g.: - (void)setSomeDateWithNSString:(NSString *)string
{
self.someDate = [ISO8601 parse:string];
} |
@billinghamj Ahhh, gotcha. So make the JSONModel property a string and add a new property for your date representation... |
@fergalrooney Nope - the property doesn't need to change at all. It would still be an NSDate. JSONModel just checks whether you have some custom logic for setting the date before doing it automatically. For completeness: @interface MyClass : JSONModel
@property (nonatomic, strong) NSDate *someDate;
@end @implementation MyClass
- (void)setSomeDateWithNSString:(NSString *)string
{
self.someDate = [ISO8601 parse:string];
}
@end Then to use it: NSString *json = @"{\"someDate\": \"2016-01-01T00:00:00Z\"}";
MyClass *obj = [[MyClass alloc] initWithString:json];
// obj.someDate is now an NSDate representing 1st Jan 2016 |
@billinghamj Fantastic. Appreciate the help! |
JSONModel was failing to parse NSDate values with milliseconds, for example "2013-11-27T05:00:00.000Z". I created a fork of JSONModel and made a fix, including an additional unit test case. Could you please review it and pull it into the main project if it looks good?
Thanks,
Scott