Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Deep NSObject to NSDictionary #19

Closed
cthorne91 opened this issue Mar 19, 2015 · 1 comment
Closed

Deep NSObject to NSDictionary #19

cthorne91 opened this issue Mar 19, 2015 · 1 comment

Comments

@cthorne91
Copy link

Hello, thanks for Motis. I use it in all my apps!

I've got a question though:

I noticed the method

- (NSDictionary*)mts_dictionaryWithValuesForKeys:(NSArray *)keys

doesn't do a 'deep' dictionary representation. What I mean is if you have an NSObject subclass such as Customer, and Customer has an array of Orders (Also an NSObject subclass). This method will return an array of Order instances as opposed to returning an array of Order dictionary representations.

Similarly, if Customer has an Address property (Also an NSObject subclass) this method will return an Address instance as opposed to a dictionary representation of that address instance.

Current output of:

[customer mts_dictionaryWithValuesForKeys:[[[customer class] mts_mapping] allKeys]];

is

{"name":"Curtis","address":"<Address mem_address>","orders":["<Order mem_address>","<Order mem_address>"]}

Desired output:

 {"name":"Curtis","address":{"street":"1111 Awesome Street","city":"You get the idea"},"orders": [{"id":1,"price":"5.00"},{"id":2,"price":"3.00"}]}

Basically, I'm looking to do the standard Motis flow, but backwards.

Is there a way to do this with the current motis version that I'm overlooking? Is it something you guys have though about including if not?

  • Thanks
@vilanovi
Copy link
Contributor

Hello @dirtbikerdude91,

Thanks for using Motis! I'm very happy to know when someone is using it!

Regarding the - (NSDictionary*)mts_dictionaryWithValuesForKeys:(NSArray *)keys method, this has been added to Motis as a convenience method, but not to perform a fully object to json mapping (I call it a "Reverse Motis").

Therefore, to implement "Reverse Motis" it is required to specify how to serialize each property of an object to JSON and this is not yet supported and I cannot promise you it will be in the future, I still have to think about how to do it and if I want to invest the time to do it.

Consequently, you will have to use another library to do it or just do it manually. Also you can fork Motis and start working on it ;).

Thanks,

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants