C#簡單快速的json組件fastJSON使用介紹
更新時間:2012年11月19日 11:47:12 作者:
JSON數據格式簡潔,用于數據的持久化和對象傳輸很實用。最近在做一個Razor代碼生成器,需要把數據庫的表和列的信息修改后保存下來,想到用JSON序列化對象并保存,需要時再反序列化成對象會簡單一些
JSON數據格式簡潔,用于數據的持久化和對象傳輸很實用。最近在做一個Razor代碼生成器,需要把數據庫的表和列的信息修改后保存下來,想到用JSON序列化對象并保存,需要時再反序列化成對象會簡單一些。codeplex上發(fā)現(xiàn)了fastJSON項目,好像很不錯的樣子。這里是作者做的性能測試:

代碼調用
namespace test
{
class Program
{
static void Main(string[] args)
{
var zoo1 = new zoo();
zoo1.animals = new List<animal>();
zoo1.animals.Add(new cat() { Name = "hello kitty", legs = 4 });
zoo1.animals.Add(new dog() { Name = "dog1", tail = true });
string json= fastJSON.JSON.Instance.ToJSON(zoo1); //序列化
var z = fastJSON.JSON.Instance.ToObject<zoo>(json); //反序列化
Console.WriteLine(z.animals[0].Name);
Console.Read();
}
}
public class animal { public string Name { get; set; } }
public class cat : animal { public int legs { get; set; } }
public class dog : animal { public bool tail { get; set; } }
public class zoo { public List<animal> animals { get; set; }
}
基本的調用就是這么簡單! 需要注意的是要反序列化的類好像必須聲明為public的。
快速的秘密
大體瀏覽了一下代碼,發(fā)現(xiàn)之所以快速的原因是作者利用反射時Emit了大量的IL代碼:
internal object FastCreateInstance(Type objtype)
{
try
{
CreateObject c = null;
if (_constrcache.TryGetValue(objtype, out c))
{
return c();
}
else
{
if (objtype.IsClass)
{
DynamicMethod dynMethod = new DynamicMethod("_", objtype, null);
ILGenerator ilGen = dynMethod.GetILGenerator();
ilGen.Emit(OpCodes.Newobj, objtype.GetConstructor(Type.EmptyTypes));
ilGen.Emit(OpCodes.Ret);
c = (CreateObject)dynMethod.CreateDelegate(typeof(CreateObject));
_constrcache.Add(objtype, c);
}
else // structs
{
DynamicMethod dynMethod = new DynamicMethod("_",
MethodAttributes.Public | MethodAttributes.Static,
CallingConventions.Standard,
typeof(object),
null,
objtype, false);
ILGenerator ilGen = dynMethod.GetILGenerator();
var lv = ilGen.DeclareLocal(objtype);
ilGen.Emit(OpCodes.Ldloca_S, lv);
ilGen.Emit(OpCodes.Initobj, objtype);
ilGen.Emit(OpCodes.Ldloc_0);
ilGen.Emit(OpCodes.Box, objtype);
ilGen.Emit(OpCodes.Ret);
c = (CreateObject)dynMethod.CreateDelegate(typeof(CreateObject));
_constrcache.Add(objtype, c);
}
return c();
}
}
catch (Exception exc)
{
throw new Exception(string.Format("Failed to fast create instance for type '{0}' from assemebly '{1}'",
objtype.FullName, objtype.AssemblyQualifiedName), exc);
}
}

代碼調用
復制代碼 代碼如下:
namespace test
{
class Program
{
static void Main(string[] args)
{
var zoo1 = new zoo();
zoo1.animals = new List<animal>();
zoo1.animals.Add(new cat() { Name = "hello kitty", legs = 4 });
zoo1.animals.Add(new dog() { Name = "dog1", tail = true });
string json= fastJSON.JSON.Instance.ToJSON(zoo1); //序列化
var z = fastJSON.JSON.Instance.ToObject<zoo>(json); //反序列化
Console.WriteLine(z.animals[0].Name);
Console.Read();
}
}
public class animal { public string Name { get; set; } }
public class cat : animal { public int legs { get; set; } }
public class dog : animal { public bool tail { get; set; } }
public class zoo { public List<animal> animals { get; set; }
}
基本的調用就是這么簡單! 需要注意的是要反序列化的類好像必須聲明為public的。
快速的秘密
大體瀏覽了一下代碼,發(fā)現(xiàn)之所以快速的原因是作者利用反射時Emit了大量的IL代碼:
復制代碼 代碼如下:
internal object FastCreateInstance(Type objtype)
{
try
{
CreateObject c = null;
if (_constrcache.TryGetValue(objtype, out c))
{
return c();
}
else
{
if (objtype.IsClass)
{
DynamicMethod dynMethod = new DynamicMethod("_", objtype, null);
ILGenerator ilGen = dynMethod.GetILGenerator();
ilGen.Emit(OpCodes.Newobj, objtype.GetConstructor(Type.EmptyTypes));
ilGen.Emit(OpCodes.Ret);
c = (CreateObject)dynMethod.CreateDelegate(typeof(CreateObject));
_constrcache.Add(objtype, c);
}
else // structs
{
DynamicMethod dynMethod = new DynamicMethod("_",
MethodAttributes.Public | MethodAttributes.Static,
CallingConventions.Standard,
typeof(object),
null,
objtype, false);
ILGenerator ilGen = dynMethod.GetILGenerator();
var lv = ilGen.DeclareLocal(objtype);
ilGen.Emit(OpCodes.Ldloca_S, lv);
ilGen.Emit(OpCodes.Initobj, objtype);
ilGen.Emit(OpCodes.Ldloc_0);
ilGen.Emit(OpCodes.Box, objtype);
ilGen.Emit(OpCodes.Ret);
c = (CreateObject)dynMethod.CreateDelegate(typeof(CreateObject));
_constrcache.Add(objtype, c);
}
return c();
}
}
catch (Exception exc)
{
throw new Exception(string.Format("Failed to fast create instance for type '{0}' from assemebly '{1}'",
objtype.FullName, objtype.AssemblyQualifiedName), exc);
}
}
您可能感興趣的文章:
- fastjson生成json時Null屬性不顯示的解決方法
- 關于fastjson的@JSONField注解的一些問題(詳解)
- java使用FastJson解析Json數據
- 淺談fastjson的常用使用方法
- Spring Boot使用FastJson解析JSON數據的方法
- springmvc fastjson 反序列化時間格式化方法(推薦)
- springMVC利用FastJson接口返回json數據相關配置詳解
- java中fastjson生成和解析json數據(序列化和反序列化數據)
- springboot實現(xiàn)FastJson解析json數據的方法
- java如何利用FastJSON、Gson、Jackson三種Json格式工具自定義時間序列化
- 詳談fastjson將對象格式化成json時的兩個問題
- Fastjson 常用API介紹及下載地址(推薦)
- 解決FastJson中"$ref重復引用"的問題方法
- FastJson對于JSON格式字符串、JSON對象及JavaBean之間的相互轉換操作
- Spring MVC+FastJson+Swagger集成的完整實例教程
- 詳解在springmvc中解決FastJson循環(huán)引用的問題
- SpringBoot Redis配置Fastjson進行序列化和反序列化實現(xiàn)
- 解決fastjson從1.1.41升級到1.2.28后報錯問題詳解