public classDemo
{
public int Id { get; set; }
public string Name { get; set; }
public staticvoidMain()
{
var json = JsonFormatter.SerializeObject(new { Id = 1, Name = "Dogwei" });
var dictionary = JsonFormatter.DeserializeObject<Dictionary<string, object>>(json);
var obj = JsonFormatter.DeserializeObject<Demo>(json);
}
}
(2): 处理重复引用
public classDemo
{
public int Id { get; set; }
public string Name { get; set; }
public Demo Instance { get; set; }
public staticvoidMain()
{
var jsonFormatter = new JsonFormatter(JsonFormatterOptions.MultiReferencingReference);
var obj = new Demo() { Id = 1, Name = "Dogwei" };
obj.Instance = obj;
var json = jsonFormatter.Serialize(obj);
var deser = jsonFormatter.Deserialize<Demo>(json);
Console.WriteLine(json); // {"Id":1,"Instance":{"$ref":"#"},"Name":"Dogwei"}
Console.WriteLine(deser.Instance == deser); // True
}
}
(3): RWField 特性
public classDemo
{
[RWField("First Name")]
public string Name { get; set; }
[RWField]
public int Age;
[RWField(Access = RWFieldAccess.Ignore)]
public int Sex { get; set; }
[RWField(Order = 1)]
public int Id { get; set; }
public staticvoidMain()
{
var obj = new Demo() { Id = 1, Name = "Dogwei", Age = 22, Sex = 1 };
var json = JsonFormatter.SerializeObject(obj);
Console.WriteLine(json); // {"Id":1,"Age":22,"First Name":"Dogwei"}
}
}
(4): 设置日期格式
public classDemo
{
public staticvoidMain()
{
var jsonFormatter = new JsonFormatter();
jsonFormatter.SetDateTimeFormat("yyyy-MM-dd HH:mm:ss");
var json = jsonFormatter.Serialize(DateTime.Now);
Console.WriteLine(json); // "2019-02-13 11:03:46"
}
}
(5): 自定义类型的行为
public classDemo
{
public string Name { get; set; }
public int Sex { get; set; }
public bool IsMan { get => Sex == 1; }
public unsafe staticvoidMain()
{
var jsonFormatter = new JsonFormatter();
jsonFormatter.SetValueInterface<bool>(new MyBooleanInterface());
var obj = new Demo() { Name = "Dogwei", Sex = 1 };
var json = jsonFormatter.Serialize(obj);
Console.WriteLine(json); // {"IsMan":"yes","Name":"Dogwei","Sex":1}
}
}
public classMyBooleanInterface : IValueInterface<bool>
{
public boolReadValue(IValueReader valueReader)
{
var value = valueReader.ReadString();
switch (value)
{
case"yes":
case"true":
returntrue;
case"no":
case"false":
returnfalse;
default:
return Convert.ToBoolean(value);
}
}
public voidWriteValue(IValueWriter valueWriter, bool value)
{
valueWriter.WriteString(value ? "yes" : "no");
}
}
(6): 设置缓存大小
public classDemo
{
public staticvoidMain()
{
HGlobalCache.MaxSize = 1024 * 500; // 500KB
var json = JsonFormatter.SerializeObject(new { MaxJsonLength = 256000 });
}
}
(7): 序列化超大文件
public classDemo
{
public staticvoidMain()
{
var bigObject = new BigObject();
using (FileStream fileStream = new FileStream("/BigObject.json", FileMode.Create, FileAccess.ReadWrite))
{
using (StreamWriter streamWriter = new StreamWriter(fileStream))
{
JsonFormatter.SerializeObject(bigObject, streamWriter);
}
}
}
}
(8): 使用适用小型应用程序的 XObjectRW<T>
public classDemo
{
public staticvoidMain()
{
// Default (FastObjectInterface) : 初始化开销较大,内存较大,性能优异。// XObjectInterface : 初始化开销小,内存占用少,性能也不错。
ValueInterface.DefaultObjectInterfaceType = typeof(XObjectInterface<>);
var json = JsonFormatter.SerializeObject(new { Id = 1, Name = "Dogwei" });
Console.WriteLine(json); // {"Id":1,"Name":"Dogwei"}
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 依赖注入中的 Captive Dependency
· .NET Core 对象分配(Alloc)底层原理浅谈
· 聊一聊 C#异步 任务延续的三种底层玩法
· 敏捷开发:如何高效开每日站会
· 为什么 .NET8线程池 容易引发线程饥饿
· 终于决定:把自己家的能源管理系统开源了!
· 外部H5唤起常用小程序链接规则整理
· C#实现 Winform 程序在系统托盘显示图标 & 开机自启动
· 了解 ASP.NET Core 中的中间件
· 详解:订单履约系统规划