C#中的JSON序列化方法
在C#中的使用JSON序列化及反序列化时,推荐使用Json.NET——NET的流行高性能JSON框架,当然也可以使用.NET自带的 System.Text.Json(.NET5)、DataContractJsonSerializer、JavaScriptSerializer(不推荐)。
下面是Json.NET、DataContractJsonSerializer、JavaScriptSerializer的性能对比(来自Json.NET官网):
Json.NET的扩展方法类如下(需引用Newtonsoft.Json命名空间):
using Newtonsoft.Json;
/// <summary>
/// Newtonsoft.Json的扩展方法类
/// </summary>
public static class NewtonsoftJsonExtension
{
/// <summary>
/// 将Json字符串反序列化为对象实例——Newtonsoft.Json
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="jsonString">Json字符串</param>
/// <returns>对象实例</returns>
public static T DeserializeObjectFromJson_NJ<T>(this string jsonString)
{
return JsonConvert.DeserializeObject<T>(jsonString);
}
/// <summary>
/// 将对象实例序列化为Json字符串——Newtonsoft.Json
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="obj">对象实例</param>
/// <returns>Json字符串</returns>
public static string SerializeObjectToJson_NJ<T>(this T obj)
{
return JsonConvert.SerializeObject(obj);
}
}
DataContractJsonSerializer的扩展方法类如下(需引用System.Runtime.Serialization.Json命名空间):
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
/// <summary>
/// System.Runtime.Serialization.Json扩展方法类([DataContract]和[DataMember]联合使用来标记被序列化的字段)
/// </summary>
public static class SerializationJsonExtension
{
private static Dictionary<Type, DataContractJsonSerializer> serDic = new Dictionary<Type, DataContractJsonSerializer>();
private static DataContractJsonSerializer GetSerializer(Type type)
{
if (!serDic.ContainsKey(type))
{
serDic.Add(type, new DataContractJsonSerializer(type));
}
return serDic[type];
}
/// <summary>
/// 将Json字符串反序列化为对象实例——System.Runtime.Serialization.Json([DataContract]和[DataMember]联合使用来标记被序列化的字段)
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="jsonString">Json字符串</param>
/// <returns>对象实例</returns>
public static T DeserializeObjectFromJson_SJ<T>(this string jsonString)
{
var ser = GetSerializer(typeof(T));
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
T jsonObject = (T)ser.ReadObject(ms);
return jsonObject;
}
}
/// <summary>
/// 将对象实例序列化为Json字符串——System.Runtime.Serialization.Json([DataContract]和[DataMember]联合使用来标记被序列化的字段)
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="obj">对象实例</param>
/// <returns>Json字符串</returns>
public static string SerializeObjectToJson_SJ<T>(this T obj)
{
var ser = GetSerializer(typeof(T));
using (var ms = new MemoryStream())
{
ser.WriteObject(ms, obj);
ms.Position = 0;
using (var sr = new StreamReader(ms, Encoding.UTF8))
{
return sr.ReadToEnd();
}
}
}
}