从LabView程序调用时,Json不反序列化List

时间:2021-01-13 16:00:51

So to make a long story short, I have a dll where it's main job is to get data sent to it from a LabView program and then handle some back-end work with said data.

所以简而言之,我有一个dll,其主要工作是从LabView程序获取数据,然后使用所述数据处理一些后端工作。

Once the data has been sent to the dll (by calling method of .NET object which has multiple params - see below) and properly parsed into .NET object, if anything goes "bad" (connection to db, any other failure to persist data) I take the data and serialize it to a local folder on the machine in the form of json files. I am using Newtonsoft.Json 6.0.8 for the json aspect of this project.

一旦数据被发送到dll(通过调用具有多个参数的.NET对象的方法 - 见下文)并正确解析为.NET对象,如果任何事情变得“坏”(连接到db,任何其他未能保留数据)我将数据以json文件的形式序列化到机器上的本地文件夹中。我使用Newtonsoft.Json 6.0.8作为该项目的json方面。

public class MainClass
{
    public void saveData(string[] someNames, string[] someValues)
    {
        // here I parse the data passed in, and create the .NET object I work with try
        {
            // here I write data to the database, do whatever else I need to do
        }
        catch (System.Exception ex)
        {
            // if ANYTHING goes wrong with the persist of data, write json to local file
        }
    }
}

This is how I serialize the data:

这是我序列化数据的方式:

private void writeParentToJson(ParentClass parent)
{
    using (var printer = new StreamWriter(filePath, false))
    {
        var izer = new JsonSerializer();

        izer.TypeNameHandling = TypeNameHandling.All;
        izer.Serialize(printer, parent);
    }
}

[JsonObject]
public class ParentClass
{
    [JsonProperty("childThings")]
    public List<ChildClass> listOfChildClass { get; set; }

    public ParentClass()
    {

    }

    public ParentClass(List<ChildClass> listOfChildClass)
    {
        this.listOfChildClass = listOfChildClass;
    }
}

[JsonObject]
public class ChildClass
{
    [JsonProperty("childName")]
    public string name { get; set; }
    [JsonProperty("childValue")]
    public string value { get; set; }

    public ChildClass()
    {

    }

    public ChildClass(string name, string value)
    {
        this.name = name;
        this.value = value;
    }
}

When this happens, everything writes fine to the json file. When I read the data again, this is how I do it:

发生这种情况时,所有内容都可以正常写入json文件。当我再次读取数据时,我就是这样做的:

public ParentClass loadParentFromJson()
{
    using (var reader = new JsonTextReader(new StreamReader(filePath)))
    {
        var izer = new JsonSerializer();
        izer.TypeNameHandling = TypeNameHandling.All;

        return izer.Deserialize<ParentClass>(reader);
    }
}

When I create a .NET project and use this dll... everything works flawlessly. The problem is that the person using LabView is not so lucky. Everything works fine when the dll writes the data to the file, but reading it back is where we are having the issue.

当我创建一个.NET项目并使用这个DLL ...一切都完美无瑕。问题是使用LabView的人不是那么幸运。当dll将数据写入文件时,一切正常,但读回来是我们遇到问题的地方。

More specifically, this happens: Inner Exception: Newtonsoft.Json.JsonSerializationException: Could not find type 'System.Collections.Generic.List`1[[MyDll.ChildClass, MyDll]]' in assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

更具体地说,发生这种情况:内部异常:Newtonsoft.Json.JsonSerializationException:在程序集'mscorlib,版本= 4.0.0.0中找不到类型'System.Collections.Generic.List`1 [[MyDll.ChildClass,MyDll]]', Culture = neutral,PublicKeyToken = b77a5c561934e089'

I don't know anything about LabView, and the person using LabView doesn't know anything about .NET. Any insight would be greatly appreciated.

我对LabView一无所知,使用LabView的人对.NET一无所知。任何见解将不胜感激。

The LabView program only calls the method, the dll does all the reading/writing of json.. which is why I don't know how it works for me, but not when called from the LabView program.

LabView程序只调用方法,dll完成json的所有读/写操作...这就是为什么我不知道它对我有用的原因,但是从LabView程序调用时却没有。

All computers have .NET 4.0 framework installed.. I have seen others with this exception, but it only fails when being called from LabView program.. and I have no idea why.

所有的计算机都安装了.NET 4.0框架。我见过其他人有这个例外,但是只有在从LabView程序调用时它才会失败。我不明白为什么。

1 个解决方案

#1


NI have a couple of application notes that I use each time I use a new Installation on LabVIEW:

NI每次在LabVIEW上使用新安装时都会使用一些应用笔记:

http://zone.ni.com/reference/en-XX/help/371361K-01/lvhowto/configuring_clr_version/

http://digital.ni.com/public.nsf/allkb/4742EB60B64BE22186257BCE0053B8FD

These describe setting the LabVIEW.exe.config file to allow the usage of 3rd part .Net assemblies (and for different .Net versions), ultimately I end up with a LabVIEW.exe.config as follows:

这些描述设置LabVIEW.exe.config文件以允许使用第三部分.Net程序集(以及不同的.Net版本),最终我最终得到一个LabVIEW.exe.config,如下所示:

<?xml version ="1.0" encoding="utf-8" ?>
    <configuration>
        <startup useLegacyV2RuntimeActivationPolicy="true">
           <supportedRuntime version="v4.0.30319"/>
        </startup>
        <runtime>
            <loadFromRemoteSources enabled="true" />
        </runtime>
    </configuration>

#1


NI have a couple of application notes that I use each time I use a new Installation on LabVIEW:

NI每次在LabVIEW上使用新安装时都会使用一些应用笔记:

http://zone.ni.com/reference/en-XX/help/371361K-01/lvhowto/configuring_clr_version/

http://digital.ni.com/public.nsf/allkb/4742EB60B64BE22186257BCE0053B8FD

These describe setting the LabVIEW.exe.config file to allow the usage of 3rd part .Net assemblies (and for different .Net versions), ultimately I end up with a LabVIEW.exe.config as follows:

这些描述设置LabVIEW.exe.config文件以允许使用第三部分.Net程序集(以及不同的.Net版本),最终我最终得到一个LabVIEW.exe.config,如下所示:

<?xml version ="1.0" encoding="utf-8" ?>
    <configuration>
        <startup useLegacyV2RuntimeActivationPolicy="true">
           <supportedRuntime version="v4.0.30319"/>
        </startup>
        <runtime>
            <loadFromRemoteSources enabled="true" />
        </runtime>
    </configuration>