Writing and Reading JSON files with QML
Hi, I'm upgrading my Tunings and Temperaments plugin to allow the user finer control over the tunings.
I'd like the user to be able to save their own tunings locally, for sharing and/or later re-use.
Ideally I'd like the saved files to be JSON, but that's not an absolute requirement.
Searching through the QML documentation I can see no obvious way to do this without also writing C++, which is a non-starter for a plugin.
Have I missed something, or should I turn this in to a feature request?
Comments
Making a JSON string should be possible using javascript, JSON.parse and JSON.stringify.
To write the file, you can use FileIO, see for example the ABC import plugin https://github.com/musescore/MuseScore/blob/2.3.2/share/plugins/abc_imp…
In reply to Making a JSON string should… by [DELETED] 5
I'll give it a try, thanks!
In reply to Making a JSON string should… by [DELETED] 5
What about reading a file? I can't get one to load..
In reply to What about reading a file? I… by XiaoMigros
The duration editor plugin uses this syntax:
import "zparkingb/notehelper.js" as NoteHelper
and it works...
In reply to The duration editor plugin… by elsewhere
That works for .js files, but if possible I would like to load a JSON
In reply to That works for .js files,… by XiaoMigros
JSON files are JavaScript… that being said, they lack any assignment, so the import statement would probably not work.
If you can load the file into a string, the standard
JSON.parse(s)
works…> but only from network as QML doesnot have direct access to local file system
… according to the Qt forum.
Your best bet is to transform the JSON to add a line before the first that simply says
exports.default =
(IIRC, untested), then useimport
. This can be done programmatically at release build time, if needed (tiny shell script or something).In reply to JSON files are JavaScript…… by mirabilos
I thought there might have been a way around the local file system thing with FileIO, but I think I'll try using a .js file instead. Thanks