如何在Json.Net中跳过IEnumerable类型的默认JavaScript数组序列化? [英] How do I skip default JavaScript array serialization for IEnumerable types in Json.Net?

查看:108
本文介绍了如何在Json.Net中跳过IEnumerable类型的默认JavaScript数组序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些实现IEnumerable的自定义类型不一定具有后备集合.它们可以动态生成,例如使用"yield"或LINQ.这是一个示例:

Some custom types that implement IEnumerable don't necessarily have backing collections. They could be generated dynamically, for example using 'yield' or LINQ. Here is an example:

public class SOJsonExample
{
    public class MyCustomEnumerable : IEnumerable<KeyValuePair<int,float>>
    {
        public List<int> Keys { get; set; }
        public List<float> Values { get; set; }

        public MyCustomEnumerable()
        {
            Keys = new List<int> { 1, 2, 3 };
            Values = new List<float> { 0.1f, 0.2f, 0.3f };
        }

        public IEnumerator<KeyValuePair<int, float>> GetEnumerator()
        {
            var kvps =
                from key in Keys
                from value in Values
                select new KeyValuePair<int, float>(key, value);

            return kvps.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}

我发现Json.NET的默认序列化是枚举每个值并将这些值存储在JavaScript数组中(我不需要).然后,默认的反序列化程序将无法反序列化集合,因为它无法填充.在这些情况下,我希望Json.NET跳过默认的JavaScript数组序列化,而只存储类的成员.

I have discovered that the default serialization by Json.NET is to enumerate each value and store the values in a JavaScript array (which I don't want). The default deserializer will then fail to deserialize the collection as it can't be populated. In these cases, I'd instead want Json.NET to skip the default JavaScript array serialization, and just store the members of the class.

我想要的只是我的键和值-有什么捷径可以做到这一点,还是我必须实现自己的自定义序列化程序?

All I want is my keys and values - is there any shortcut way to do this, or do I have to implement my own custom serializer?

选中了文档,但没有找到我想要的东西(也许我在错误的位置).

Checked this and this, neither of which are exactly my question. Scanned the documentation as well, but didn't find what I was looking for (perhaps I looked in the wrong place).

(编辑#1-提高清晰度)

(Edit #1 - improved clarity)

(编辑#2-回答了我自己的问题...请参见下文)

(Edit #2 - answered my own question...see below)

推荐答案

回答了我自己的问题-看到出色的

Answered my own question - see the excellent documentation on IEnumerable, Lists, and Arrays:

.NET列表(继承自IEnumerable的类型)和.NET数组将转换为JSON数组.由于JSON数组仅支持一定范围的值,而不支持属性,因此.NET集合上声明的所有其他属性和字段都不会序列化.在不需要JSON数组的情况下,可以将JsonObjectAttribute放在实现IEnumerable的.NET类型上,以强制将该类型序列化为JSON对象.

.NET lists (types that inherit from IEnumerable) and .NET arrays are converted to JSON arrays. Because JSON arrays only support a range of values and not properties, any additional properties and fields declared on .NET collections are not serialized. In situations where a JSON array is not wanted the JsonObjectAttribute can be placed on a .NET type that implements IEnumerable to force the type to be serialized as a JSON object instead.

因此,以我的示例为例:

So, for my example:

[JsonObject]
public class MyCustomEnumerable : IEnumerable<KeyValuePair<int,float>>
{
    ...
}

这篇关于如何在Json.Net中跳过IEnumerable类型的默认JavaScript数组序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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