08-24-2022 09:42 AM - edited 08-24-2022 09:44 AM
My program has lots of settings for its measurement. I'm giving the users the option to store their settings to a file to be loaded at a later time. Settings include numbers, strings, booleans and enums. They are stored in multiple clusters (and clusters within clusters).
At the moment I'm just bundling each category together and writing it to a binary file. This works very well and requires little code. The problem is when in a future update, I want to add an additional parameter to one of the settings categories. Reading the binary file (with the previous cluster) using the new cluster type gives an error. This means that the entire settings file cannot be read and files between updates are incompatible.
I would like that parameters that didn't change will still be read, and new parameters will go to a default value. What is the best way to do this.
Obviously, writing a cluster to a binary file doesn't work. I've tried flattening the cluster to an XML string, but this gives the same problem (error on reading). Flattening to string sort of works, but doens't respect the order of the cluster if the new settings is added not at the end. Flattening to JSON seemed the way to go to me, since both label and key are saved. But JSON doesn't support enums unfortunately. Moreover, if I have 5 elements, and I add a new element on index 2, Unflatten JSON String will stop once it reaches an unknown key:
I guess I could write my own cluster to JSON parser, but this seems unnecessarily complex. What to do?
Solved! Go to Solution.
08-24-2022 09:55 AM - edited 08-24-2022 09:56 AM
Use MGI Read/Write Anything VIs. They work quite well. You can download the package using JKI Package Manager.
08-24-2022 10:03 AM
@Basjong53 wrote:
My program has lots of settings for its measurement. I'm giving the users the option to store their settings to a file to be loaded at a later time. Settings include numbers, strings, booleans and enums. They are stored in multiple clusters (and clusters within clusters).
At the moment I'm just bundling each category together and writing it to a binary file. This works very well and requires little code. The problem is when in a future update, I want to add an additional parameter to one of the settings categories. Reading the binary file (with the previous cluster) using the new cluster type gives an error. This means that the entire settings file cannot be read and files between updates are incompatible.
I would like that parameters that didn't change will still be read, and new parameters will go to a default value. What is the best way to do this.
Obviously, writing a cluster to a binary file doesn't work. I've tried flattening the cluster to an XML string, but this gives the same problem (error on reading). Flattening to string sort of works, but doens't respect the order of the cluster if the new settings is added not at the end. Flattening to JSON seemed the way to go to me, since both label and key are saved. But JSON doesn't support enums unfortunately. Moreover, if I have 5 elements, and I add a new element on index 2, Unflatten JSON String will stop once it reaches an unknown key:
I guess I could write my own cluster to JSON parser, but this seems unnecessarily complex. What to do?
Whenever you save formatted data, you risk formatting change that will make the old data incompatible. One thing that will help is if you made the cluster typedef'd so at least changes made to the cluster will update in the data read/write. it still won't help with incompatibilities, but at least you don't have to worry about updating the same cluster in several different places (and potentially missing some instances).
Your "unnecessarily complex" parser is the standard way of doing this. By using a data format agnostic file, you can still parse the old data along with the new if you do it right.
08-24-2022 10:17 AM
Use the JDP JSONtext library. It can handle everything you are asking for.
08-24-2022 11:15 AM