Skip to content

JSON Serialization fails for LabVIEW classes containing Variant data #27

Open

Description

@boehrlii

Description

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

  1. Create a LabVIEW class with a Variant element in the private data cluster.
  2. Populate the class instance with valid data (including the Variant).
  3. Serialize the object using Generic Object Serializer (JSON mode).
  4. 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.
  • During deserialization, an error occurs.
Image

Activity

  1. logmanoriginal commented on May 25, 2026

    @logmanoriginal
    Owner

    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.

    Image

    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.

    The fix is to always return errors to the caller.

    Image
  2. logmanoriginal commented on May 25, 2026

    @logmanoriginal
    Owner

    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

    Image

    In the second case (observed issue), the returned info is String with empty label "" => array

    Image

    Finally, if the provided data has a label, the returned info is String with the label info "Test" => object

    Image

    This 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

  3. logmanoriginal commented on May 25, 2026

    @logmanoriginal
    Owner

    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?

    Image Image
    {
      "$type": "MyObject.lvclass",
      "Data": "Hello World!"
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions