前段时间使用了net.json保存对象数据。添加完成后,测试发现300多实例数据保存加载json文件,速度比原方式(binaryformatter)慢。但是功能加上后也懒再删掉代码了,索性就采用两种方式。怎么说两种方式也比一种多不是?^_^
测试类student
[jsonobject(memberserialization.fields)] class student { const int count = 1024; public student(int id, string name, int age, string sex) { this.id = id; this.name = name; this.age = age; this.sex = sex; buffer = new byte[count]; for (int i = 0; i < count; i++) { buffer[i] = (byte)i; } } byte[] buffer; int id { get; set; } string name { get; set; } int age { get; set; } string sex { get; set; } [jsonignore] intptr hwnd; [ondeserialized] private void setvaluesondeserialized(streamingcontext context) { string stringa = "i seem to be turned around!"; hwnd = system.runtime.interopservices.marshal.stringtohglobalansi(stringa); } }
测试用例
arraylist stumodel = new arraylist() { new student(1,"王小二",18,"男"), new student(2,"丽丽",16,"女") }; using (streamwriter file = file.createtext(@"test.json")) { jsonserializer serializer = new jsonserializer(); serializer.serialize(file, stumodel); } using (streamreader file = file.opentext(@"test.json")) { jsonserializer serializer = new jsonserializer(); var movie2 = (arraylist)serializer.deserialize(file, typeof(arraylist)); for (int i = 0; i < movie2.count; i++) { movie2[i] = jsonconvert.deserializeobject<student>(movie2[i].tostring()); } }
list<student> stumodel = new list<student>() { new student(1,"王小二",18,"男"), new student(2,"丽丽",16,"女") }; using (streamwriter file = file.createtext(@"test.json")) { jsonserializer serializer = new jsonserializer(); serializer.serialize(file, stumodel); } using (streamreader file = file.opentext(@"test.json")) { jsonserializer serializer = new jsonserializer(); var movie2 = (list<student>)serializer.deserialize(file, typeof(list<student>)); }
如对本文有疑问, 点击进行留言回复!!
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
网友评论