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

C#中使用Newtonsoft.Json多态正反序列化

一、在通讯中,使用json字符串作为命令进行传递,发送接收方都需要序列化。

常见方法有两种,

1)使用TypeNameHandling = TypeNameHandling.All ,但是这种序列化的内容中包含命令空间信息,这种不利于两个应用统一。

JsonSerializerSettings jsonSet = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
string testStr = Newtonsoft.Json.JsonConvert.SerializeObject(cmd0, jsonSet);

2)重载JsonConverter类.

二、这里讲第2种方法

有三个命令类



    public class VMCmdBase
    {
        [JsonProperty(Order =-99)]
        public string mType = typeof(VMCmdBase).Name;

        [JsonProperty(Order = -98)]
        public int mCmdID = 1;

        public int mPara1 = 0;
        public int mPara2 = 0;
        public int mPara3 = 0;
        public int mPara4 = 0;
        public int mPara5 = 0;


        public virtual string ToJsonStr()
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(this);
        }

        public virtual void GetAllValue(object theObj)
        {
            Type thisTy = this.GetType();
            Type canTy = theObj.GetType();

            if (!thisTy.IsAssignableFrom(canTy)) {
                throw new ArgumentException("参数类型不正确");
            }
            VMCmdBase tempC = (VMCmdBase)theObj;
            mType = tempC.mType;
            mCmdID = tempC.mCmdID;
            mPara1 = tempC.mPara1;
            mPara2 = tempC.mPara2;
            mPara3 = tempC.mPara3;
            mPara4 = tempC.mPara4;
            mPara5 = tempC.mPara5;
        }
    }

    public class VMCmdResp1 : VMCmdBase
    {
        public bool mOKNG = false;

        public VMCmdResp1()
        {
            mType = this.GetType().Name;
        }
        public override string ToJsonStr()
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(this);
        }

        public override void GetAllValue(object theObj)
        {
            Type thisTy = this.GetType();
            Type canTy = theObj.GetType();

            if (!thisTy.IsAssignableFrom(canTy)) {
                throw new ArgumentException("参数类型不正确");
            }
            VMCmdResp1 tempC = (VMCmdResp1)theObj;
            base.GetAllValue(tempC);
            mOKNG = tempC.mOKNG;
        }
    }

    public class VMCmdResp2 : VMCmdBase
    {
        public bool mOKNG = false;
        public string mSN = "";

        public VMCmdResp2()
        {
            mType = this.GetType().Name;
        }

        public override string ToJsonStr()
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(this);
        }

        public override void GetAllValue(object theObj)
        {
            Type thisTy = this.GetType();
            Type canTy = theObj.GetType();

            if (!thisTy.IsAssignableFrom(canTy)) {
                throw new ArgumentException("参数类型不正确");
            }
            VMCmdResp2 tempC = (VMCmdResp2)theObj;
            base.GetAllValue(tempC);
            mOKNG = tempC.mOKNG;
            mSN = tempC.mSN;
        }
    }

重载类

public abstract class FHJsonCreationConverter<T> : JsonConverter
{
    protected abstract T Create(Type objectType, JObject jsonObject);
    public override bool CanConvert(Type objectType)
    {
        return typeof(T).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var jsonObject = JObject.Load(reader);
        var target = Create(objectType, jsonObject);
        serializer.Populate(jsonObject.CreateReader(), target);
        return target;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

public class FHJsonConverter_VMCmd : FHJsonCreationConverter<VMCmdBase>
{
    protected override VMCmdBase Create(Type objectType, JObject jsonObject)
    {
        var typeName = jsonObject["mType"].ToString();
        switch (typeName) {
            case "VMCmdBase":
                return new VMCmdBase();
            case "VMCmdResp1":
                return new VMCmdResp1();
            case "VMCmdResp2":
                return new VMCmdResp2();
            default: return null;
        }
    }
}

最后主体使用

//=================================
VMCmdBase cmd0 = new VMCmdBase() { mCmdID = 1, mPara1 = 11 };
VMCmdResp1 cmd1 = new VMCmdResp1() { mCmdID = 2, mPara1 = 22, mOKNG=false };
VMCmdResp2 cmd2 = new VMCmdResp2() { mCmdID = 3, mPara1=33, mOKNG = true , mSN="abc"};

string cmd0Json = "";
string cmd1Json = "";
string cmd2Json = "";
 
//1.序列化
cmd0Json = Newtonsoft.Json.JsonConvert.SerializeObject(cmd0);
cmd1Json = Newtonsoft.Json.JsonConvert.SerializeObject(cmd1);
cmd2Json = Newtonsoft.Json.JsonConvert.SerializeObject(cmd2);

JsonSerializerSettings jsonSet = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
string testStr = Newtonsoft.Json.JsonConvert.SerializeObject(cmd0, jsonSet);

//2.反序列化 
VMCmdBase baseCmd = null; 
try {
    baseCmd = Newtonsoft.Json.JsonConvert.DeserializeObject<VMCmdBase>(cmd1Json, new FHJsonConverter_VMCmd());
} catch (Exception e) {
    baseCmd = null;
}
string asdf = baseCmd.GetType().Name;


http://www.kler.cn/a/568201.html

相关文章:

  • 基于 MetaGPT 自部署一个类似 MGX 的多智能体协作框架
  • 内容中台的企业CMS架构是什么?
  • 对话式AI引擎:DeepSeek技术引领多模态交互新篇章
  • RabbitMq延时队列的实现
  • 【漫话机器学习系列】107.线性组合(Linear Combination)
  • 【论文阅读笔记】SL-YOLO(2025/1/13) | 小目标检测 | HEPAN、C2fDCB轻量化模块
  • 检索增强生成(RAG)技术解析:大模型时代的“知识导航系统”
  • 基于Selenium的Python淘宝评论爬取教程
  • 【AI+智造】基于SKF IMAX-16+PT1000与Odoo18工业物联网架构智慧生产诊断系统集成方案
  • ubuntu 20.04 安装labelmg
  • C# Unity 唐老狮 No.1 模拟面试题
  • 【论文阅读笔记】FcaNet: Frequency Channel Attention Networks(2021/7/23)
  • Deepseek开源周第四天:从 DualPipe 到 EPLB
  • 查找Excel包含关键字的行(の几种简单快速方法)
  • 北京中烟创新科技有限公司:荣誉与创新并行
  • 考研复试问题总结-数据结构(1)
  • 【一条龙教程】用AI DS+创作原创音乐 (配合Midjourney漫画)制作原创MTV
  • 构建神经网络之Matplotlib(持续完善)
  • 为什么Agent会失败?2025年对AI的预测
  • wordpress按不同页调用不同的标题3种形式