号称.Net下最快的json组件fastJSON.zip

时间:2022-08-07 21:55:46
【文件属性】:
文件名称:号称.Net下最快的json组件fastJSON.zip
文件大小:71KB
文件格式:NONE
更新时间:2022-08-07 21:55:46
开源项目 JSON数据格式简洁,用于数据的持久化和对象传输很实用。 java下有个大名鼎鼎的阿里巴巴开源的Java的JSON处理器 fastjson,.net也有个.net版的fastjson。这里是作者做的性能测试:代码调用namespace test {     class Program     {         static void Main(string[] args)         {             var zoo1 = new zoo();             zoo1.animals = new List();             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(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 animals { get; set; }  }基本的调用就是这么简单! 需要注意的是要反序列化的类好像必须声明为public的。快速的秘密 大体浏览了一下代码,发现之所以快速的原因是作者利用反射时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);             }         }更多教程请参考:http://www.codeproject.com/Articles/159450/fastJSON 标签:fastjson  json
【文件预览】:
fastJSON-master
----history.txt(15KB)
----README.md(470B)
----Consoletest()
--------mytests.cs(9KB)
--------Program.cs(25KB)
--------consoletest.csproj(4KB)
--------dataobjects.cs(2KB)
--------AssemblyInfo.cs(1KB)
----fastJSONcore()
--------fastJSON.csproj(2KB)
----fastJSON.nuspec(1KB)
----fastJSON.sln(4KB)
----UnitTests.nunit(191B)
----Howto.md(4KB)
----UnitTests()
--------UnitTests.csproj(3KB)
--------Properties()
--------Tests.cs(92KB)
----fastJSON 3.5()
--------fastJSON 3.5.csproj(6KB)
----fastJSONCore.sln(1KB)
----fastJSON()
--------Reflection.cs(33KB)
--------JsonParser.cs(14KB)
--------fastJSON-android.csproj(2KB)
--------Formatter.cs(3KB)
--------fastJSON.csproj(6KB)
--------Getters.cs(215B)
--------JsonSerializer.cs(23KB)
--------dynamic.cs(3KB)
--------JSON.cs(42KB)
--------fastJSON.snk(596B)
--------SafeDictionary.cs(1KB)
--------Helper.cs(9KB)
--------fastJSON-SL.csproj(4KB)
--------AssemblyInfo.cs(324B)

网友评论