LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
Arun_Persaud

Allow enums in "flatten to json"/"unflatten from json"

Status: New

Currently enums can not be in a cluster that you want convert to JSON. It would be great, if this could be added.

 

Either the enum value could be converted to its internal representation (e.g. u16) or the value could be saved as a string (make it easier to read the JSON and parse it outside LabVIEW).

 

The unflatten should be able to handle the u16 easily to convert back to enum or take the string and search through the enum of the datatype that one has to connect to the vi. If there is no match the VI can issue an error.

 

4 Comments
malocascio
Member

I just ran into this issue myself. It makes it difficult to introduce JSON serialization into any existing code that includes enums in clusters.

 

The unflatten/flatten JSON VIs could just include an input terminal that allows you to specify whether you want enums to be encoded as their integer value or their string.

AristosQueue (NI)
NI Employee (retired)

The JSON prim developers shied away from anything that wasn't specifically JSON; they assume that you'll translate your block of data into the data you want serialized. The primitives could have an options switch for "integers or strings" for enums. Or they could have just picked a behavior for enums. Both options have downsides, but both are significantly less work and significantly less memory usage than the current solutions.

 

Personally, I'd support just picking the string names.

 

(I would go further and say that the JSON prims should have a defined behavior for all LV data types, like the XML prims.... it might not be what you want, in which case you'd have to manage your own data translation, but it would be a fair sight more helpful in many cases than what we have now.)

jwscs
Active Participant

letting ppl have the option to either have the selected number or the string (with defaulting to string) would be my preference.

 

i use the to-json serialization (or would like to) for my logging

 

the xml just spams all info, which makes it unusable for me ther

(especially if you have many cases in the enum)

 

regards


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
Tomi_Maila
Active Participant

JKI JSON library allows flattening and unflattening enums to both numbers and strings.

 

jki.net/tools#json

--
Tomi Maila