matthewcheok / Realm-JSON

A concise Mantle-like way of working with Realm and JSON.
MIT License
661 stars 129 forks source link

Could not parse plain Array without keys #100

Closed vervatovskis closed 7 years ago

vervatovskis commented 7 years ago

How can I configure/parse JSON object like this Screenshot.

More explanation: It's an array of integers without keys like this: partitionIds: [6107,6108,6109,6110],

mpaulsonco commented 7 years ago

I believe that this is a limitation with Realm as opposed to Realm-JSON.

While Realm supports storing arrays, they must be stored in an RLMArray, and RLMArrays may only contain RLMObjects, not basic types such as a string or number. This specific change has been tracked on Realm Cocoa's issue tracker.

I would suggest structuring your data so that you are storing an array of integers with keys. You might need to do some preprocessing on that JSON to change it into something like [{"value": 6107}, {"value": 6108}, {"value": 6109}, {"value": 6110}] and then create a RLMObject (with Value: Int) that is stored in a RLMArray on your parent object. While this is not ideal app-side, if you're in a situation where you have control over the JSON coming down, consider changing it to confirm to jsonapi.org's latest specification which would help to avoid this issue.

Feel free to let me know if you need any further guidance, happy coding!

vervatovskis commented 7 years ago

Thanks Max for this clear and detailed answer. It really helps :)