找回密码
 立即注册
首页 业界区 业界 jsonb 为什么会影响 System.Text.Json

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

哈妙思 昨天 14:18
  我在将一个属性映射到 jsonb 类型时遇到这样一个问题 —— 我有一个抽象基类 BaseClass 和一个派生类 DerivedClass:
  1. [JsonDerivedType(typeof(DerivedClass), typeDiscriminator: "derived")]
  2. public class BaseClass
  3. {
  4.     public BaseClass() { }
  5. }
  6. public class DerivedClass : BaseClass
  7. {
  8.     ....
  9. }
复制代码
  当持久化数据后,我可能得到这样的数据:
  1. {
  2.   "$type": "derived",
  3.   ...
  4. }<br>
复制代码
  或
  1. {
  2.   "value": 131121,
  3.   "$type": "derived",
  4.   ...
  5. }
复制代码
  第一个 json 中,类型鉴别器($type)是 json 里的第一个属性;而第二个 json 字符串则不是这种情况。当我执行 Deserialize 方法时,第二个 json 字符串会抛出异常。
  1. 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 的异常。
解决方案

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

  通过设置 AllowOutOfOrderMetadataProperties 属性为 true,可解决此问题:
  1. JsonSerializerOptions options = new() { AllowOutOfOrderMetadataProperties = true };
复制代码
  如果您没有找到该属性,请升级到高版本。
2.webp

 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册