'收集尺寸为固定。'例外:System.Text.json.jsonserializer中的错误还是错误?

发布于 2025-02-10 21:50:27 字数 2075 浏览 3 评论 0 原文

我已经研究了几个,所以回答了这个例外(例如这个)没有成功。另外,重现行为的示例是我看到的最小的。作为复制此问题的控制,我有一个常数的字符串:

const string json =
@"{
    ""ArrayOfString"": [""Hello World""]
  }";

classa

class ClassA
{
    public string[] ArrayOfString { get; set; }
}

没有问题 delelialization classa 是否使用<我做代码> newtonsoft.json 或 system.text.json

ClassA classA;
classA = Newtonsoft.Json.JsonConvert.DeserializeObject<ClassA>(json);
classA = System.Text.Json.JsonSerializer.Deserialize<ClassA>(json);

现在,考虑 classB ,其中同一属性具有 Baseclass 中的备份存储。

class ClassB : BaseClass
{
    public string[] ArrayOfString
    {
        get => new string[] { base.SingularString };
        set
        {
            if((value != null) && (value.Length == 1))
            {
                base.SingularString = value[0];
            }
        }
    }
}
class BaseClass
{
    public string SingularString { get; set; } = "Hello World";
}

从相同的 json 源中, classB clastb 似乎并不是一个大的要求。

ClassB classB;
classB = Newtonsoft.Json.JsonConvert.DeserializeObject<ClassB>(json);
classB = System.Text.Json.JsonSerializer.Deserialize<ClassB>(json);

newtonsoft 没有任何牛肉这样做:

“

但是, system.text.json 当要求做同样的事情时抛出:

任何人是否有第一手知识此错误 2年前?这是重新出现的吗?还是只是犯某种错误?是的,我明白了。集合 是固定的尺寸,但这为什么重要?

I have studied several SO answers citing this exception (like this one) without success. Also, my example to reproduce the behavior is the most minimal that I've seen. As a control for reproducing this issue, I have this constant string:

const string json =
@"{
    ""ArrayOfString"": [""Hello World""]
  }";

And ClassA:

class ClassA
{
    public string[] ArrayOfString { get; set; }
}

There are no issues deserializing ClassA whether I do it with Newtonsoft.Json or System.Text.Json.

ClassA classA;
classA = Newtonsoft.Json.JsonConvert.DeserializeObject<ClassA>(json);
classA = System.Text.Json.JsonSerializer.Deserialize<ClassA>(json);

Now consider ClassB where the same property has a backing store in BaseClass.

class ClassB : BaseClass
{
    public string[] ArrayOfString
    {
        get => new string[] { base.SingularString };
        set
        {
            if((value != null) && (value.Length == 1))
            {
                base.SingularString = value[0];
            }
        }
    }
}
class BaseClass
{
    public string SingularString { get; set; } = "Hello World";
}

It doesn't seem like a big ask to have ClassB deserialize from the same json source.

ClassB classB;
classB = Newtonsoft.Json.JsonConvert.DeserializeObject<ClassB>(json);
classB = System.Text.Json.JsonSerializer.Deserialize<ClassB>(json);

Newtonsoft doesn't have any beef doing this:

newtonsoft watch

However, System.Text.Json throws when asked to do the same thing:

system.text.json

Does anyone have first-hand knowledge of this bug which was closed more than 2 years ago? Is this a reemergence of that? Or am simply making an error of some kind? And yeah, I get it. The collection is a fixed size but why would that matter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑看君怀她人 2025-02-17 21:50:27

这是一个固定在.NET 5的错误。请参阅plup 替换收集实例#39442 。可以在这里看到一个工作.NET 6小提琴: https://dotnetfiddle.net/uhryjp

如果您必须针对.NET Core 3.1开发,则您的选项似乎包括:

  1. classB

  2. 使用 list&lt; string&gt; 而不是 string [] arayofstring

      classB:Baseclass
    {
        公共列表&lt; string&gt; arrayofstring
        {
            get =&gt;新列表&lt; string&gt;(){base.singularString};
            放
            {
                if((value!= null)&amp;&amp;(value.count == 1))
                {
                    base.singularString = value [0];
                }
            }
        }
    }
     

    (请注意,如果您仍在定位System.Text.json和Newtonsoft, list&lt; string&gt; 将无法与后者一起使用。请参阅Eg 当我的poco中的所有收藏都无效,当时将一些有效的json与.net newtonsoft.json component.json component P>

This is a bug that was fixed in .NET 5. See pull Replace collection instances on deserialize #39442. A working .NET 6 fiddle can be seen here: https://dotnetfiddle.net/UhRYJP.

If you must develop against .NET Core 3.1, your options would appear to include:

  1. Introduce a custom converter for ClassB.

  2. Use a List<string> instead of a string [] for ArrayOfString:

    class ClassB : BaseClass
    {
        public List<string> ArrayOfString
        {
            get => new List<string>() { base.SingularString };
            set
            {
                if((value != null) && (value.Count == 1))
                {
                    base.SingularString = value[0];
                }
            }
        }
    }
    

    (Note that if you are still targeting both System.Text.Json and Newtonsoft, List<string> will not work with the latter. See e.g. Why are all the collections in my POCO are null when deserializing some valid json with the .NET Newtonsoft.Json component for why.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文