You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JSON Serialization fails for LabVIEW classes containing Variant data #27
When a LabVIEW class contains a Variant in its private data, the JSON serialization process produces an incomplete or incorrect JSON string. As a result, deserialization fails because required data is missing. User Error or is it not able to handle Variants in Classes.
Steps to Reproduce
Create a LabVIEW class with a Variant element in the private data cluster.
Populate the class instance with valid data (including the Variant).
Serialize the object using Generic Object Serializer (JSON mode).
Attempt to deserialize the generated JSON string back into the original class.
Expected Behavior
The serializer should produce a complete and valid JSON representation of the class, including the Variant data.
Actual Behavior
The generated JSON string is incomplete and does not contain all required data.
Thank you for reporting this issue. I am able to reproduce the behavior. There are actually two issues involved:
Private data cluster is converted to array type by JSONtext
This is the primary issue. Tested with JSONtext 1.6.10
The output of the highlighted section is ["Nils"]
Expected: {"Variant":"Nils"}
Note that the library is specifically using the Array of Variants to Cluster utility function from the JDP Science Common Utilities library to ensure it is a cluster'y type before passing to JSONtext. However, it does produce the correct result.
In the case of non-inline type name handling, the data array is returned. While it contains the correct data, it is the wrong representation for object data.
I'm investigating possible fixes to avoid this behavior.
No error reported when JSON object is merged with JSON array
As part of Inline type name handling, the type name is merged with the object data. In this case, however, we attempt to merge the type name (which is guaranteed to be a JSON object) with the previously created JSON array. This is not allowed and results in an error, which is not passed through.
I see now what is happening. In the case of variants inside clusters (not just private data clusters but any cluster), if the first element is a variant, JSONtext is unable to get the type label because Get Type Information returns the type label of the type contained by a variant, not the type label of the variant itself:
In this first case, the variant is empty, which is returned as void with empty label "" => array
In the second case (observed issue), the returned info is String with empty label "" => array
Finally, if the provided data has a label, the returned info is String with the label info "Test" => object
The suggested solution is to use a string type with "<JSON>" label to allow JSONtext to insert any JSON string. However, it would require the consumer to handle JSON conversion and is not automatic.
A possible solution is for JSONtext to check all fields, not just the first one. However, that would only fix the cases where there are other elements with labels in a cluster. It would still not work for clusters that only consist of variants (zero or more, regardless).
Also, the content would still be based on the embedded type and not carry the variant label
I'm not sure there is a good solution for this, other than the one provided in the linked post.
Variants have no type information, so it is impossible to restore a variant unless type information is maintained.
For inline type name handling, it would be possible to add type information. However, it would only work for the top-level hierarchy as nested types are handled by JSONtext and no hook exists. This also introduces a lot of complexity for type reconstruction.
Have you considered using a JSON string field to allow consumers to inject JSON payload into your class instead of variant data?
Activity
logmanoriginal commentedon May 25, 2026
Thank you for reporting this issue. I am able to reproduce the behavior. There are actually two issues involved:
Private data cluster is converted to array type by JSONtext
This is the primary issue. Tested with JSONtext 1.6.10
The output of the highlighted section is
["Nils"]Expected:
{"Variant":"Nils"}Note that the library is specifically using the
Array of Variants to Clusterutility function from theJDP Science Common Utilitieslibrary to ensure it is a cluster'y type before passing to JSONtext. However, it does produce the correct result.In the case of non-inline type name handling, the data array is returned. While it contains the correct data, it is the wrong representation for object data.
I'm investigating possible fixes to avoid this behavior.
No error reported when JSON object is merged with JSON array
As part of
Inlinetype name handling, the type name is merged with the object data. In this case, however, we attempt to merge the type name (which is guaranteed to be a JSON object) with the previously created JSON array. This is not allowed and results in an error, which is not passed through.The fix is to always return errors to the caller.
logmanoriginal commentedon May 25, 2026
I see now what is happening. In the case of variants inside clusters (not just private data clusters but any cluster), if the first element is a variant, JSONtext is unable to get the type label because
Get Type Informationreturns the type label of the type contained by a variant, not the type label of the variant itself:In this first case, the variant is empty, which is returned as
voidwith empty label "" => arrayIn the second case (observed issue), the returned info is
Stringwith empty label "" => arrayFinally, if the provided data has a label, the returned info is
Stringwith the label info "Test" => objectThis has been discussed before on LAVA: https://lavag.org/topic/22351-un-flatten-from-json-to-array-of-cluster-with-contained-variant-loses-data/#findComment-137928
The suggested solution is to use a string type with "<JSON>" label to allow JSONtext to insert any JSON string. However, it would require the consumer to handle JSON conversion and is not automatic.
A possible solution is for JSONtext to check all fields, not just the first one. However, that would only fix the cases where there are other elements with labels in a cluster. It would still not work for clusters that only consist of variants (zero or more, regardless).
Also, the content would still be based on the embedded type and not carry the variant label
logmanoriginal commentedon May 25, 2026
I'm not sure there is a good solution for this, other than the one provided in the linked post.
inlinetype name handling, it would be possible to add type information. However, it would only work for the top-level hierarchy as nested types are handled by JSONtext and no hook exists. This also introduces a lot of complexity for type reconstruction.Have you considered using a JSON string field to allow consumers to inject JSON payload into your class instead of variant data?
{ "$type": "MyObject.lvclass", "Data": "Hello World!" }