Skip to content
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

Bumping into 'Invalid type in JSON write (Foundation.__NSSwiftData)' with attribute declared Binary Data #566

Closed
robert-dodier opened this issue Jul 3, 2019 · 3 comments

Comments

@robert-dodier
Copy link

I am working with a number of entities declared in Core Data which have mostly primitive types and Dates, and I can convert those items to JSON via NSManagedObject.export with no problem.

One entity has an attribute which is declared in Core Data as Binary Data. Instances of that entity are assigned via: x.myattr = myArray.withUnsafeBufferPointer { Data (buffer: $0) } where x is an instance of the entity and myattr is the attribute declared Binary Data and myArray is an array I'd like to assign to myattr. This step appears to succeed as expected.

Where this is falling apart is that I want to generate a JSON representation of x. What works for instances of other entities (without Binary Data attributes) is x.export. However when I try that on x which has a Binary Data attribute, I get the following error message:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Foundation.__NSSwiftData)'

I'm a little confused about how stuff is supposed to work here. I guess the Foundation class to which Sync punts JSON serialization (NSJSONSerialization I guess) is complaining that it doesn't know what to do with that attribute. But how can Sync generally work with Binary Data? From the Sync readme, I got the impression that declaring attributes to be Binary Data is the conventional way, in Sync, to represent arrays and dictionaries. How can I make that work? Is the current behavior actually correct? I wonder if I am misunderstanding something here.

@robert-dodier
Copy link
Author

I've worked around the problem by calling NSManagedObject.export and then replacing Data values in the resulting dictionary with Array values (copying the Data into the Array via Data.copyTo and Array.withUnsafeMutableBufferPointer).

@3lvis
Copy link
Owner

3lvis commented Jul 9, 2019

Hi @robert-dodier,

Thanks for your patience and it's nice to hear that you were able to work around the issue from what I understand is that the export method was breaking because you had a binary element, right?

I don't think I've unit tested this scenario but to be clear, what would you expect it happens in this case? The binary attribute just gets ignored and the rest is exported?

Thanks for your time :)

@robert-dodier
Copy link
Author

Thanks for your reply. I guess I was expecting that the binary attribute would be represented by a base-64 string or other printable representation.

I guess I don't understand the examples in the Sync documentation which seem to show attributes which are declared as Binary Data. Are those examples never converted to a JSON representation? I guess I didn't expect that.

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

No branches or pull requests

2 participants