jagill / JAGPropertyConverter

An Objective-C library to convert model objects to PropertyLists/JSON Dictionaries, and vice versa.
MIT License
179 stars 27 forks source link

convertToDate - handling 2 different date types #7

Open ParthaGudivada opened 11 years ago

ParthaGudivada commented 11 years ago

If the json object has two date string objects (say, 2013-03-02, 2013-03-02 10:15), can't the convertToDate message handle these formats.

JAGPropertyConverter *converter = [[JAGPropertyConverter alloc] initWithOutputType:kJAGJSONOutput] ;

[converter setConvertToDate:^(id d1) { return [NSDate convertFromStringDate:d1 usingFormatterString:DT_yyyy_MM_dd_HH_mm_ss]; } ];

    [converter setConvertToDate:^(id d2)
     {
         return [NSDate convertFromStringDate:d12 usingFormatterString:DT_yyyy_MM_dd];

     }];

I have written a custom method on NSDate to convert different string date formats. What I am seeing is that, I can use only one at any point of time.
Commenting either of the above works fine for the individual date string objects. However if uncomment both of the above, only one gets converted. This lead me to believe that I can convert only one date string object ? Am I missing something. Thanks in advance for your reply.

jagill commented 11 years ago

How would you like the converter to decide between which of the formats it should use?

ParthaGudivada commented 11 years ago

Can the following rules be included :
a) if the convert block has specific reference to the attribute name, allow the specific format. b) Otherwise, switch to the default one.

jagill commented 11 years ago

Could you give me a code example of the API you'd like? Perhaps a general solution would be something like an NSSDictionary of property names to ConvertBlocks, so we'd first check if a property has a particular convert block, and if so use that?

[converter forPropertyName: @"firstDate" setConvertBlock:^(id d1)
  {
    return [NSDate convertFromStringDate:d1 usingFormatterString:@"DT_yyyy_MM_dd_HH_mm_ss"];
  }
];
ParthaGudivada commented 11 years ago

It makes sense to follow the approach suggested.