如何使用JSON.Net将JsonExtensionData(Dictionary< string,JToken>)应用于另一个对象 [英] How to apply JsonExtensionData (Dictionary<string, JToken>) to another object with JSON.Net

查看:656
本文介绍了如何使用JSON.Net将JsonExtensionData(Dictionary< string,JToken>)应用于另一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这种(看似平常)的情况,但是找不到令人满意的解决方案.也许有人知道:

I came across this (seemingly usual) scenario but I could not find a satisfying solution. Maybe someone knows:

出于某种原因,我解析JSON,并且允许用户提供比类具有属性更多的键/值对.我像这样存储任意的东西:

For some reason I parse JSON and I allow the user to provide more key-value pairs than my class has properties. I store the arbitrary ones away like so:

class MusterNode
{
    // some definite property
    public string TypeName { get; set; }

    // takes the rest
    // see https://www.newtonsoft.com/json/help/html/DeserializeExtensionData.htm
    [JsonExtensionData]
    private Dictionary<string, JToken> _extparams;
}

如果我反序列化诸如此类的东西

If I deserialize something like

{
   "TypeName": "a",
   "stuff": 3
}

将设置TypeName,并且我的_extparams包含键"stuff".

TypeName will be set and my _extparams contains a key "stuff".

由于某种原因,我想将存储的数据应用于另一个(刚刚创建的)对象'obj'(实际上,该参数被认为是该类型名的东西). 所以我有一个词典和一个对象.有没有一种方法可以应用"字典而不先对其进行序列化?

For some reason I want to apply that stored data to another (just created) object 'obj' (in fact the parameters were thought for that typename thingy). So I have a Dictionary and an object. Is there a way to 'apply' the dictionary without serializing it first?

我不满意的解决方案是这样:

My non-satisfying solution is this:

string json = JsonConvert.SerializeObject(_extparams);
JsonConvert.PopulateObject(json, obj);

用一些JsonSerializerSettings装饰,对我有用.但这确实不必要.

decorated with some JsonSerializerSettings, this works for me. But it does unnecessary work.

推荐答案

Json.Net没有可以直接从标准字典填充对象的方法.毕竟,它是一个序列化库,而不是映射库.也就是说,有一种无需中间序列化/反序列化步骤即可使其工作的方法.

Json.Net does not have a method which will populate an object directly from a standard dictionary. After all, it is a serialization library, not a mapping library. That said, there is a way to make it work without the intermediate serialization/deserialization step.

首先,不要使用Dictionary<string, JToken>作为[JsonExtensionData]参数的容器,而要使用JObject. JObject实现了IDictionary<string, JToken>,因此它仍然可以捕获额外的属性.

First, instead of using a Dictionary<string, JToken> as the container for your [JsonExtensionData] parameters, use a JObject. JObject implements IDictionary<string, JToken>, so it will still work to catch the extra properties.

class MusterNode
{
    ...
    [JsonExtensionData]
    private JObject _extparams;
}

然后,要填充其他对象,只需从JObject创建一个读取器,然后将其传递给JsonSerializer.Populate(),如下所示:

Then, to populate the other object, you just need to create a reader from the JObject and pass it to JsonSerializer.Populate() like this:

new JsonSerializer().Populate(_extparams.CreateReader(), obj);

如果需要特定的序列化设置,则可以在调用Populate()之前直接在JsonSerializer上进行设置.

If you have specific serialization settings you need, you can set them directly on the JsonSerializer prior to calling Populate().

这是一个有效的演示: https://dotnetfiddle.net/kIzc5G

Here is a working demo: https://dotnetfiddle.net/kIzc5G

这篇关于如何使用JSON.Net将JsonExtensionData(Dictionary&lt; string,JToken&gt;)应用于另一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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