Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JKI JSON does not deserialize objects with properties set to empty objects #32

Open
TAGC opened this issue Nov 27, 2017 · 3 comments
Open

Comments

@TAGC
Copy link

TAGC commented Nov 27, 2017

This library is not able to deserialize JSON containing objects in which:

  • one property is set to an empty object
  • at least one other property exists, set to something like a number or a string

Given this JSON (call it "JSON A"):

{
  "foo": {
    "propA": {
      "nestedA": 1
    },
    "propB": 1
  }
}

And this JSON (call it "JSON B"):

{
  "foo": {
    "propA": {},
    "propB": 1
  }
}

Expected results

Both JSON A and B are valid, and JKI JSON should deserialize both without issue using Unflatten From JSON String.vi.

Actual results

Unflatten From JSON String.vi can deserialize JSON A without issue, but returns an error trying to deserialize JSON B:

Error 1527 occurred at Flattened String To Variant in Set Data Name__ogtk.vi->JKI JSON Serialization.lvlib:JSON Deserializer.lvclass:Adapt To Type.vi:3110001->JKI JSON Serialization.lvlib:JSON Deserializer.lvclass:Adapt To Type.vi:3110002->JKI JSON Serialization.lvlib:JSON Deserializer.lvclass:Unflatten From String.vi:2180001->JKI JSON Serialization.lvlib:Unflatten From JSON String.vi:4940001->Run.vi

Possible reason(s):

LabVIEW:  Attempted to read flattened data of a LabVIEW class that is not currently loaded into LabVIEW.

Steps to reproduce

  1. Save this LabVIEW as a VI in some directory (call it Run.vi):

    snippet

  2. Create a file example.json in the same directory.

  3. Copy JSON A into example.json and save.

  4. Execute Run.vi. There should be no error.

  5. Replace the contents of example.json with JSON B and save.

  6. Execute Run.vi. You should see Error Out display error 1527.

@TAGC
Copy link
Author

TAGC commented Nov 27, 2017

The error occurs in JSON Deserializer.lvclass:Adapt To Type.vi during a call to Set Data Name.vi

@TAGC
Copy link
Author

TAGC commented Nov 27, 2017

Also, like most things, the same thing is much more straightforward in C#:

void Main()
{
    var json = @"
    {
        ""foo"": {
            ""propA"": {},
            ""propB"": 1
        }
    }";
    
    var root = JsonConvert.DeserializeObject<RootObject>(json);
    root.Foo.PropB.Dump(); // 1
}

public class PropA
{
}

public class Foo
{
    public PropA PropA { get; set; }
    public int PropB { get; set; }
}

public class RootObject
{
    public Foo Foo { get; set; }
}

@TAGC
Copy link
Author

TAGC commented Nov 27, 2017

It seems to work fine if the type definitions aren't wired to Unflatten From JSON String.vi:

snippet2

I'm not sure why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant