JSON.NET无法处理简单的数组反序列化? [英] JSON.NET cannot handle simple array deserialization?

查看:91
本文介绍了JSON.NET无法处理简单的数组反序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个只有一个字段的简单类. class Test{int value;}

I created a simple class with one field. class Test{int value;}

如果我使用保留引用"功能并将其设置为全部"(即对象和数组),那么当我简单地序列化Test对象的数组时,它将被序列化为带有特殊"$"的JSON对象. 值"成员与数组值,以及预期的"$ id"属性来保留数组引用.没关系,但是整个过程在反序列化时再次中断.

If I use the "preserve references" feature and set it to "all" (i.e. both objects and arrays), then when I simply serialize an array of Test objects, it gets serialized as a JSON object with a special "$values" member with the array values, along with the expected "$id" property to preserve the array reference. That much is fine, but once again the whole thing breaks on deserialization.

单步执行源代码,我发现仅因为对"IsReadOnlyOrFixedSize"的测试为真,所以将标志"createdFromNonDefaultConstructor"设置为true,这甚至没有任何意义,因为尽管它是一个固定大小的数组,它是从默认构造函数创建的,除非它将任何固定大小的数组构造函数视为非默认构造函数.最重要的是,它应该能够处理如此基本的内容,但会引发此错误:"Cannot preserve reference to array or readonly list, or list created from a non-default constructor".

Stepping through the source code, I discovered that simply because the test for "IsReadOnlyOrFixedSize" is true, it sets a flag "createdFromNonDefaultConstructor" to true, which doesn't even make any sense, because although it is a fixed size array, it is created from a default constructor, unless it considers any fixed size array constructor a non-default constructor. The bottom line is that it should be able to handle something so basic, and yet it throws this error: "Cannot preserve reference to array or readonly list, or list created from a non-default constructor".

如何在保留JSON.NET中所有引用的同时反序列化基本数组,而不会出现错误?

How can I deserialize a basic array while preserving all references in JSON.NET without getting an error?

推荐答案

遇到了同样的问题,我使用了List<T>而不是T[]来解决它.

Got the same issue, I used List<T> instead of T[] to fix it.

这篇关于JSON.NET无法处理简单的数组反序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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