Files
barcodeManager/LibShapes/Core/Serialize/JsonSerialize.cs
2026-01-24 08:45:54 +08:00

42 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Io.Github.Kerwinxu.LibShapes.Core.Serialize
{
/// <summary>
/// json的序列化
/// </summary>
public class JsonSerialize : AbstractSerialize
{
public override T DeserializeObject<T>(string value)
{
return JsonConvert.DeserializeObject<T>(value,jsonSerializerSettings);
//throw new NotImplementedException();
}
public override string SerializeObject(object obj)
{
return JsonConvert.SerializeObject(obj, Formatting.Indented, jsonSerializerSettings);
//throw new NotImplementedException();
}
/// <summary>
/// 有这个是确保反序列化到正确的类型。
/// </summary>
private JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.Auto, // 自动的All的话会有问题。
DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat,
DateFormatString = "yyyy-MM-dd HH:mm:ss", //空值处理
//NullValueHandling = NullValueHandling.Ignore, //高级用法九中的`Bool`类型转换设置
ReferenceLoopHandling = ReferenceLoopHandling.Serialize, // 循环引用的的解决方式,如下如下两种设置。
PreserveReferencesHandling = PreserveReferencesHandling.Objects, //
Formatting = Formatting.Indented, // 缩进的
};
}
}