jsonb 为什么会影响 System.Text.Json

jsonb 为什么会影响 System.Text.Json

    正在检查是否收录...
一言准备中...

  我在将一个属性映射到 jsonb 类型时遇到这样一个问题 —— 我有一个抽象基类 BaseClass 和一个派生类 DerivedClass:

[JsonDerivedType(typeof(DerivedClass), typeDiscriminator: "derived")] public class BaseClass { public BaseClass() { } } public class DerivedClass : BaseClass { .... }

  当持久化数据后,我可能得到这样的数据:

{ "$type": "derived", ... }

  或

{ "value": 131121, "$type": "derived", ... }

  第一个 json 中,类型鉴别器($type)是 json 里的第一个属性;而第二个 json 字符串则不是这种情况。当我执行 Deserialize 方法时,第二个 json 字符串会抛出异常。

System.Text.Json.JsonException : The metadata property is either not supported by the type or is not the first property in the deserialized JSON object. Path: $.$type | LineNumber: 0 | BytePositionInLine: 47.

问题原因

System.Text.Json 的多态反序列化限制

  当使用 [JsonDerivedType] 时,System.Text.Json 要求类型鉴别器(如 $type)必须是 json 中的第一个属性。如果鉴别器不在首位,反序列化会失败并抛出异常。

PostgreSQL 的存储特性

  -

json

:以原始文本形式存储,完全保留输入的 json 字符串格式(包括空格、重复键、顺序等)。

  -

jsonb

:以二进制形式存储,会对 json 进行解析和规范化(例如去重键、忽略多余空格、重新排序键)。

  jsonb 类型会自动对 json 属性进行排序,导致存储后的属性顺序与序列化时不同。例如:{"$type":"derived","value": xxx } 存储到 jsonb 后可能变为:{"value": xxx ,"$type":"derived"} 这种顺序变化会引发 System.Text.Json 的异常。

解决方案

  其实微软在官方文档中已经给出了说明:

jsonb 为什么会影响 System.Text.Json

  通过设置 AllowOutOfOrderMetadataProperties 属性为 true,可解决此问题:

JsonSerializerOptions options = new() { AllowOutOfOrderMetadataProperties = true };

  如果您没有找到该属性,请升级到高版本。

jsonb 为什么会影响 System.Text.Json

 



  • 本文作者:WAP站长网
  • 本文链接: https://wapzz.net/post-26956.html
  • 版权声明:本博客所有文章除特别声明外,均默认采用 CC BY-NC-SA 4.0 许可协议。
本站部分内容来源于网络转载,仅供学习交流使用。如涉及版权问题,请及时联系我们,我们将第一时间处理。
文章很赞!支持一下吧 还没有人为TA充电
为TA充电
还没有人为TA充电
0
  • 支付宝打赏
    支付宝扫一扫
  • 微信打赏
    微信扫一扫
感谢支持
文章很赞!支持一下吧
关于作者
2.7W+
8
1
1
WAP站长官方

Argo CD持续交付工具部署

上一篇

PyCharm 快速入门指南

下一篇
评论区
内容为空

这一切,似未曾拥有

  • 复制图片
按住ctrl可打开默认菜单