tweecode / twine

UI for creating hypertext stories
http://twinery.org
656 stars 97 forks source link

Added JSON Save and Load #166

Open Lokno opened 10 years ago

Lokno commented 10 years ago

I thought it would be useful for Twine to be able to save and load JSON instead of pickle dumps. I wrote some changes to use the jsonpickle library instead of pickle. You can now "Save as JSON..." and this will save a .twjs file. Open now loads both the original .tws files and the JSON .twjs files. I use a regex in StoryPanel to remove the time fields from the jsonpickled classes. This is because I see they are being phased out, and there is no general way to remove them from the get/setstate methods which will work with legacy files.

Lokno commented 10 years ago

I only really did that for completeness (and absentmindedly I thought it might be nice to be able to paste JSON somewhere else and edit it). The actual calls and default parameters for those functions are such that the operation is still carried out in pickle. Happy to remove those extra lines, that's a simple change.

Edit: I'll just take the initiative and remove it.

Lokno commented 9 years ago

I added a line in app.py to intend the JSON so it's human readable, and also to sort the tags. Sorting the tags makes the regex in the storypanel.py stripTimeFields method more rebust. However, using this regex to strip out the time information is a hack I would rather do without. The trouble is: the JSON is too messy with the time codes left in, but the time codes have to stay in the pickling of the class, otherwise the original file format won't be backwards comparable.

Lokno commented 9 years ago

Line 324 of tiddlywiki.py states that pickling has been broken for older versions of Twine. However there is a contradicting note on Line 341 about maintaining compatibility with Twine 1.3.5. If compatibility is no longer a requirement, the attributes 'created' and 'modified' could be removed from getstate, which would in turn remove the need to strip these attributes out when saving JSON.