当前位置: 首页 > news >正文

C# Newtonsoft.Json 反序列化子类数据丢失问题

问题描述:

代码中对象类型是父类:

public List<EmbossingActionConfigBase> EmbossingActionConfigBaseList; 

但是保存的数据是子类数据,然后序列化或者反序列化的时候要求对象类型不丢失。

解决方法:


 

var settings = new JsonSerializerSettings
{TypeNameHandling = TypeNameHandling.Auto
};
string json = JsonConvert.SerializeObject(list, settings);

效果:

json字符串:

"EmbossingActionConfigBaseList": {"$type": "System.Collections.Generic.List`1[[FluentVision.Control.EmbossingActionConfigBase, FluentVision.Controls]], mscorlib","$values": [{"$id": "24","$type": "FluentVision.Control.StageZActionConfig, FluentVision.Controls","Gap": 500,"Speed": 100.0,"MaxForce": 4.0,"MinForce": 1.0,"Overtime": 100,"EmbossingActionType": 0,"Delay": 100},{"$id": "25","$type": "FluentVision.Control.StageZActionConfig, FluentVision.Controls","Gap": 100,"Speed": 50.0,"MaxForce": 4.0,"MinForce": 1.0,"Overtime": 100,"EmbossingActionType": 0,"Delay": 100},{"$id": "26","$type": "FluentVision.Control.StageZActionConfig, FluentVision.Controls","Gap": 30,"Speed": 20.0,"MaxForce": 4.0,"MinForce": 1.0,"Overtime": 100,"EmbossingActionType": 0,"Delay": 100},{"$id": "27","$type": "FluentVision.Control.LevelingActionConfig, FluentVision.Controls","ToleranceError": 10.0,"BaseGap": 30,"EmbossingActionType": 1,"Delay": 100},{"$id": "28","$type": "FluentVision.Control.DetectMaskActionConfig, FluentVision.Controls","MaskAlignmentPositionDescription": "下视觉产品拍照位","EmbossingActionType": 2,"Delay": 10},{"$id": "29","$type": "FluentVision.Control.AlignmentActionConfig, FluentVision.Controls","ProductAlignmentPositionDescription": "下视觉相机压合位置产品拍照","EmbossingActionType": 3,"Delay": 10}]
}

其中type字段就是类型描述,多个子类信息分开保存,反序列化的时候即使使用基类类型承接也不会丢失信息。

参考:C# Newtonsoft.Json 反序列化派生类数据丢失问题_c# newtonsoft json 派生类型-CSDN博客

http://www.dtcms.com/a/336648.html

相关文章:

  • DBeaver连接MySQL 8报错连接丢失
  • HTTP协议-3-HTTP/2是如何维持长连接的?
  • JAVA后端开发——Token自动续期机制的必要性
  • 【Linux内核】Linux信号机制
  • 【Linux】五种IO模型
  • JVM学习笔记-----StringTable
  • react 错误边界
  • Python 内置模块 collections 常用工具
  • 【撸靶笔记】第二关:GET -Error based -Intiger based
  • Spring Framework :IoC 容器的原理与实践
  • CW32L011_电机驱动器开发板试用
  • 工作中使用到的时序指标异常检测算法 TRPS 【Temporal Residual Pattern Similarity】和 K-sigma 算法
  • 区块链:数字时代信任基石的构建与创新
  • 25年第十本【金钱心理学】
  • 1. Docker的介绍和安装
  • 洛谷 P2324 [SCOI2005] 骑士精神-提高+/省选-
  • CE桥接MuMu模拟器
  • 计算机网络 Session 劫持 原理和防御措施
  • IC验证 AHB-RAM 项目(一)——项目理解
  • 【leetcode】58. 最后一个单词的长度
  • Python大模型应用开发-核心技术与项目开发
  • 【165页PPT】基于IPD的研发项目管理(附下载方式)
  • vue路由懒加载
  • 数据链路层(1)
  • Linux操作系统软件编程——多线程
  • 基于飞算JavaAI实现高端算法性能优化:从理论到落地的性能跃迁实践
  • C++---迭代器删除元素避免索引混乱
  • 【Golang】:函数和包
  • 因果语义知识图谱如何革新文本预处理
  • os详解,从上面是‘os‘模块?到核心组成和常用函数