1. 通过 JsonSerializer 类的 Serialize/Deserialize 函数,示例代码如下:
#region 序列化测试 RegAccessInfo testInfo = new RegAccessInfo(); testInfo.DeviceId = "dev01"; testInfo.RegAddr = 0xC0; testInfo.RegData = 0x2018; string jsonString = JsonSerializer.Serialize(testInfo); RegAccessInfo testInfo2 = JsonSerializer.Deserialize<RegAccessInfo>(jsonString); byte[] jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(testInfo); var readOnlySpan = new ReadOnlySpan<byte>(jsonUtf8Bytes); RegAccessInfo testInfo3 = JsonSerializer.Deserialize<RegAccessInfo>(readOnlySpan); Console.WriteLine("Test end."); #endregion