反序列化时将Json信息传递给对象构造函数

编程入门 行业动态 更新时间:2024-10-27 03:27:10
本文介绍了反序列化时将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信息传递给对象构造函数

本文发布于:2023-08-04 05:54:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1293541.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时将   函数   信息传递   对象   序列化

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!