jsonb 为什么会影响 System.Text.Json
💡
原文中文,约1300字,阅读约需4分钟。
📝
内容提要
在将属性映射到 jsonb 类型时,System.Text.Json 要求类型鉴别器($type)必须是第一个属性,否则会抛出异常。由于 jsonb 存储会自动排序属性,建议将 JsonSerializerOptions 的 AllowOutOfOrderMetadataProperties 属性设置为 true 以解决此问题。
🎯
关键要点
- 在将属性映射到 jsonb 类型时,$type 必须是第一个属性,否则会抛出异常。
- 使用 [JsonDerivedType] 时,System.Text.Json 要求类型鉴别器必须在 json 中的首位。
- jsonb 类型会自动对 json 属性进行排序,导致存储后的属性顺序与序列化时不同。
- 解决方案是将 JsonSerializerOptions 的 AllowOutOfOrderMetadataProperties 属性设置为 true。
- 如果找不到该属性,建议升级到高版本。
➡️