反序列化时将Json信息传递给对象构造函数 [英] Passing Json information to object constructor when deserializing

查看:260
本文介绍了反序列化时将Json信息传递给对象构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由嵌套序列化对象组成的Json文件。

反序列化此文件时,可以根据需要重建嵌套对象,执行以下操作:

  var SomeNestedObjects = JsonConvert.DeserializeObject< SomeNestedObjectsFormat>(File.ReadAllText(@ e:\file.json));; 

现在,一切都很好,因为我的系统已经定义了所有这些嵌套对象,因此JsonConverter是能够根据该Json文件中的内容创建和初始化批次。



我想做的是创建一个空模块,并在运行时根据Json文件中的内容进行构造/构建/填充。让我们以以下Json文件为例,其中包含1个模块,该模块由2个参数组成:

  {
Name : DummyModule,
param1:{
Value:456,
Name: Param1,
MinValue:0,
MaxValue:500
},
param2:{
Value:false,
Name: Param2,
MinValue: false,
MaxValue:真
}
}

现在,我的系统有一个Parameter类,但是我的DummyModule类对Param1和Param2一无所知。原因是此DummyModule可以由任何东西组成,并且可以在运行时更改。



所以我想做的就是能够在运行时在读取Json时向DummyModule添加属性。为此,我需要在DummyModule构造函数中基于从Json中读取的内容进行一些魔术处理。

问题是我不知道构造函数如何访问或传递有关Json文件的信息。这是我的DummyModule()类:

 公共类DummyModule 
{
public string Name {get;组; }

[JsonConstructor]
public DummyModule()
{
//使用Json Object在此处创建/添加我的 Param1和 Param2属性fly
// ...

//某种形式:
foreach(jsonObject中的var param)
CreateProperty(tb,param.FieldName,param。 FieldType);
}
}


解决方案

如果您的对象不必是 DummyModule 类型,可以使用


I have a Json file that is composed of nested serialized objects.
When deserializing this file I get my nested object reconstructed as needed, doing the following:

var SomeNestedObjects= JsonConvert.DeserializeObject<SomeNestedObjectsFormat>(File.ReadAllText(@"e:\file.json"));

Now, everything works great because my system has a definition of all these nested object and the JsonConverter is therefore able to create and initialise the lot out of whatever is in that Json file.

What I would like to do though is to have a dummy module that is empty and gets constructed/built/populated at runtime based on what is found in the Json file. Let's take the following Json file as an example, containing 1 module composed of 2 parameters:

{
  "Name": "DummyModule",
  "param1": {
    "Value": 456,
    "Name": "Param1",
    "MinValue": 0,
    "MaxValue": 500
  },
  "param2": {
    "Value": false,
    "Name": "Param2",
    "MinValue": false,
    "MaxValue": true
  }
}

Now, my system has a class Parameter, but my DummyModule class does not know anything about Param1 and Param2. The reason is that this DummyModule can be composed of anything and can change at runtime.

So what I would like to do is to be able to add properties to my DummyModule at runtime when I read my Json. In order to do so, I would need to do some magic within my DummyModule constructor based on that is read out of the Json.
Problem is that I don't know how my constructor can access or be passed information about the Json file. Here is my DummyModule() class:

public class DummyModule
{
    public string Name { get; set; }

    [JsonConstructor]
    public DummyModule()
    {
        // Use Json Object here to create/add my "Param1" and "Param2" properties on the fly
        // ...

        // Something of the sort:       
        foreach (var param in jsonObject)
            CreateProperty(tb, param.FieldName, param.FieldType);
    }
}

解决方案

If your object doesn't have to be DummyModule type, you can use ExpandoObject as your destination type to deserialize the JSON:

The ExpandoObject class enables you to add and delete members of its instances at run time and also to set and get values of these members.

dynamic myObject = JsonConvert.DeserializeObject<ExpandoObject>(File.ReadAllText(@"e:\file.json"));

Console.WriteLine(myObject.Name);
Console.WriteLine(myObject.Name.GetType());

Console.WriteLine(myObject.param1.Value);
Console.WriteLine(myObject.param1.Value.GetType());

And this would be the output:

这篇关于反序列化时将Json信息传递给对象构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆