Is there an analog to the String.parseJSON for writing? I guess I could make a fancy format string that would write the things i want to, but it would be nice to take a Dictionary and ship it to the filesystem directly.
Edit: actually, it doesn’t have to be JSON. I’m looking to store some synth configs (and maybe make it easy to load presets). And JSON seemed like a good idea. But anything else that would enable me to save and load some numbers would work.
SuperCollider makes it really easy to store things inside files and get them back.
Instead of “File.read”, which manipulates a string, you can also execute the file to get the content back directly, if you stored the data using .asCompileString :
(
// You can't do this if the file is not saved
var currentFolder = thisProcess.nowExecutingPath.dirname ++ "/";
// Datas stored inside an IdentityDictionary
var saveDatas = (
foo: 5,
bar: "test"
);
var saveFile, loadDatas;
// Saving the ID inside a file
saveFile = File(currentFolder ++ "saveFile.scd", "w");
saveFile.write(saveDatas.asCompileString);
saveFile.close;
// Retrieving the ID back
loadDatas = this.executeFile(currentFolder ++ "saveFile.scd");
loadDatas.postln;
loadDatas[\foo].postln;
// No conversion needed, this isn't a string, it's an ID dict (it's an Event, but it's the same)
loadDatas.class.postln;
)
Please note, if this makes sense to you, that you can use this to create new instances of an object prototype. This might help organizing the code/project, and allows you to quickly copy-paste an object prototype from a project to an other, or to dynamically load them :
(
var currentFolder = thisProcess.nowExecutingPath.dirname ++ "/";
// Object Prototype
var calculatorProto = (
square: { |self, number|
number * number
},
);
var protoFile, instance;
// Saving the ID inside a file
protoFile = File(currentFolder ++ "calculatorProto.scd", "w");
protoFile.write(calculatorProto.asCompileString);
protoFile.close;
// Creating a new instance
instance = this.executeFile(currentFolder ++ "calculatorProto.scd");
instance.square(4).postln;
)
Please let me know if this works. I think it would be good if this function (of course array should be parsed correctly) was implemented as a method in the core library or quarks.