LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

I want to store cluster in text file. Cluster may change when reading the text file. How to do?

Solved!
Go to solution

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:

JSON Error.png

I guess I could write my own cluster to JSON parser, but this seems unnecessarily complex. What to do?

0 Kudos
Message 1 of 5
(1,075 Views)

Use MGI Read/Write Anything VIs. They work quite well. You can download the package using JKI Package Manager.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 2 of 5
(1,063 Views)

@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:

JSON Error.png

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.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 3 of 5
(1,058 Views)
Solution
Accepted by topic author Basjong53

Use the JDP JSONtext library.  It can handle everything you are asking for.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 4 of 5
(1,043 Views)
@crossrulz This is perfect! Thank you very much.
0 Kudos
Message 5 of 5
(1,015 Views)