Drafts crashes when writing json that contains a date object

,

This bug effects both iOS and macOS.

Sample code:

const data = { date: new Date() }
const scrubbedData = JSON.parse(JSON.stringify(data));
const fm = FileManager.createCloud();

// Writes file 
fm.writeJSON("test1.json", scrubbedData);
// Crashes Drafts v43 (423)
fm.writeJSON("test2.json", data);

That one is kind of tricky to avoid, because the crash is a long-standing bug in Apple’s JSONSerialization object, which should throw an error on that case, but instead crashes—but the reason it crashes is that JSON does not support date data types. By passing it through stringify/parse in the JavaScript engine, you have removed the date value and converted it to a string.

You should construct a value JSON object prior to passing it to writeJSON…or just pass the stringify value to writeString.

Here’s some useful background on working with dates in JSON:

2 Likes

Ahh ok, no problem. I didn’t realize the issue was so low level. I’ll convert the date before passing it to writeJSON. Thanks!