我有一个对象,我正在使用 NewtonSoft Json.Net 将其序列化为 Json。对象比较大,生成的Json约300kb,但序列化过程耗时60秒左右。
要序列化的对象只是普通的 POCO。
我使用的代码是
string json = Newtonsoft.Json.JsonConvert.SerializeObject(data, Formatting.Indented);
有什么可以加速序列化,添加属性等吗
编辑:我刚刚使用 ServiceStack.Text Json 序列化程序进行了测试,这需要 48 秒,仍然很慢。
[Serializable]
public class AppointmentItemViewModel
{
public AppointmentItemViewModel()
{
Data = new AppointmentData();
Statuses = new List<Status>();
ClosedDays = new List<ClosedDay>();
OpenHours = new List<OpenHours>();
}
public int CurrentDay { get; set; }
public int CurrentMonth { get; set; }
public int CurrentYear { get; set; }
public int Day { get; set; }
public int Month { get; set; }
public int Year { get; set; }
public int FirstHour { get; set; }
public int LastHour { get; set; }
public int CurrentHour { get; set; }
public int Step { get; set; }
public bool StaffOnlyBookOwn { get; set; }
public bool AllowPastAppointments { get; set; }
public bool AllowBlocks { get; set; }
public bool AllowGoogleCalendarSync { get; set; }
public long CurrentUser { get; set; }
public string DebugInfo { get; set; }
public bool HasResources { get; set; }
public string OrganisationId { get; set; }
public string DefaultTab { get; set; }
public string StartDay { get; set; }
public bool AppointmentBreaksOnWeek { get; set; }
public bool AppointmentBreaksOnMonth { get; set; }
public AppointmentData Data { get; set; }
public IEnumerable<Status> Statuses { get; set; }
public IEnumerable<LocationStaff> Staff { get; set; }
public IEnumerable<ClosedDay> ClosedDays { get; set; }
public IEnumerable<OpenHours> OpenHours { get; set; }
public IUserContext UserContext()
{
return ServiceLocator.Current.GetInstance<IUserContext>();
}
public override string ToString()
{
// Serialize the Json
var sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.WriteStartObject();
WriteProperty(writer, "CurrentDay", this.CurrentDay);
WriteProperty(writer, "CurrentMonth", this.CurrentMonth);
WriteProperty(writer, "CurrentYear", this.CurrentYear);
WriteProperty(writer, "Day", this.Day);
WriteProperty(writer, "Month", this.Month);
WriteProperty(writer, "Year", this.Year);
WriteProperty(writer, "FirstHour", this.FirstHour);
WriteProperty(writer, "LastHour", this.LastHour);
WriteProperty(writer, "CurrentHour", this.CurrentHour);
WriteProperty(writer, "Step", this.Step);
WriteProperty(writer, "StaffOnlyBookOwn", this.StaffOnlyBookOwn);
WriteProperty(writer, "AllowPastAppointments", this.AllowPastAppointments);
WriteProperty(writer, "AllowBlocks", this.AllowBlocks);
WriteProperty(writer, "AllowGoogleCalendarSync", this.AllowGoogleCalendarSync);
WriteProperty(writer, "CurrentUser", this.CurrentUser);
WriteProperty(writer, "HasResources", this.HasResources);
WriteProperty(writer, "OrganisationId", this.OrganisationId);
WriteProperty(writer, "DefaultTab", this.DefaultTab);
WriteProperty(writer, "StartDay", this.StartDay);
WriteProperty(writer, "AppointmentBreaksOnWeek", this.AppointmentBreaksOnWeek);
WriteProperty(writer, "AppointmentBreaksOnMonth", this.AppointmentBreaksOnMonth);
writer.WritePropertyName("Statuses");
writer.WriteStartArray();
foreach (var item in this.Statuses)
{
writer.WriteStartObject();
WriteProperty(writer, "Id", item.Id);
WriteProperty(writer, "Description", item.Description);
WriteProperty(writer, "Color", item.Color);
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WritePropertyName("Staff");
writer.WriteStartArray();
foreach (var item in this.Staff)
{
writer.WriteStartObject();
WriteProperty(writer, "Id", item.Id);
WriteProperty(writer, "Name", item.Name);
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WritePropertyName("ClosedDays");
writer.WriteStartArray();
foreach (var item in this.ClosedDays)
{
writer.WriteStartObject();
WriteProperty(writer, "Year", item.Year);
WriteProperty(writer, "Month", item.Month);
WriteProperty(writer, "Day", item.Day);
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WritePropertyName("OpenHours");
writer.WriteStartArray();
foreach (var item in this.OpenHours)
{
writer.WriteStartObject();
WriteProperty(writer, "DayOfWeek", item.DayOfWeek);
WriteProperty(writer, "OpenHour", item.OpenHour);
WriteProperty(writer, "CloseHour", item.CloseHour);
writer.WriteEndObject();
}
writer.WriteEndArray();
// Main data
writer.WritePropertyName("Data");
writer.WriteStartObject();
writer.WritePropertyName("Appointments");
writer.WriteStartArray();
foreach (var item in this.Data.Appointments)
{
writer.WriteStartObject();
WriteProperty(writer, "Id", item.Id);
WriteProperty(writer, "AppointmentId", item.AppointmentId);
WriteProperty(writer, "Year", item.Year);
WriteProperty(writer, "Month", item.Month);
WriteProperty(writer, "Day", item.Day);
WriteProperty(writer, "StartHour", item.StartHour);
WriteProperty(writer, "StartMinute", item.StartMinute);
WriteProperty(writer, "EndHour", item.EndHour);
WriteProperty(writer, "EndMinute", item.EndMinute);
WriteProperty(writer, "ResourceId", item.ResourceId);
WriteProperty(writer, "Description", item.Description);
WriteProperty(writer, "Status", item.Status);
WriteProperty(writer, "IsClass", item.IsClass);
WriteProperty(writer, "ProcessingLength", item.ProcessingLength);
WriteProperty(writer, "ClientId", item.ClientId);
WriteProperty(writer, "ClientName", item.ClientName);
WriteProperty(writer, "ClientPhone", item.ClientPhone);
WriteProperty(writer, "ClientNotes", item.ClientNotes);
WriteProperty(writer, "ClientHasMobile", item.ClientHasMobile);
WriteProperty(writer, "ClassFull", item.ClassFull);
WriteProperty(writer, "ClientWaiting", item.ClientWaiting);
WriteProperty(writer, "PromotionCode", item.PromotionCode);
WriteProperty(writer, "ArrivalNote", item.ArrivalNote);
WriteProperty(writer, "Labels", item.Labels);
WriteProperty(writer, "ReminderSent", item.ReminderSent);
WriteProperty(writer, "Cancelled", item.Cancelled);
writer.WritePropertyName("Items");
writer.WriteStartArray();
foreach (var appointmentItem in item.Items)
{
writer.WriteStartObject();
WriteProperty(writer, "Name", appointmentItem.Name);
WriteProperty(writer, "Length", appointmentItem.Length);
WriteProperty(writer, "ProcessingTime", appointmentItem.ProcessingTime);
WriteProperty(writer, "Resource", appointmentItem.Resource);
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WritePropertyName("Resources");
writer.WriteStartArray();
foreach (var item in this.Data.Resources)
{
writer.WriteStartObject();
WriteProperty(writer, "Id", item.Id);
WriteProperty(writer, "Name", item.Name);
WriteProperty(writer, "BlockLength", item.BlockLength);
WriteProperty(writer, "StartHour", item.StartHour);
WriteProperty(writer, "EndHour", item.EndHour);
writer.WritePropertyName("Breaks");
writer.WriteStartArray();
foreach (var breakItem in item.Breaks)
{
writer.WriteStartObject();
WriteProperty(writer, "Year", breakItem.Year);
WriteProperty(writer, "Month", breakItem.Month);
WriteProperty(writer, "Day", breakItem.Day);
WriteProperty(writer, "DayOfWeek", breakItem.DayOfWeek);
WriteProperty(writer, "StartHour", breakItem.StartHour);
WriteProperty(writer, "StartMinute", breakItem.StartMinute);
WriteProperty(writer, "Length", breakItem.Length);
WriteProperty(writer, "Description", breakItem.Description);
WriteProperty(writer, "OtherBreak", breakItem.OtherBreak);
WriteProperty(writer, "UserBreak", breakItem.UserBreak);
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WritePropertyName("OpenCloseBreaks");
writer.WriteStartArray();
foreach (var breakItem in item.OpenCloseBreaks)
{
writer.WriteStartObject();
WriteProperty(writer, "Year", breakItem.Year);
WriteProperty(writer, "Month", breakItem.Month);
WriteProperty(writer, "Day", breakItem.Day);
WriteProperty(writer, "DayOfWeek", breakItem.DayOfWeek);
WriteProperty(writer, "StartHour", breakItem.StartHour);
WriteProperty(writer, "StartMinute", breakItem.StartMinute);
WriteProperty(writer, "Length", breakItem.Length);
WriteProperty(writer, "Description", breakItem.Description);
WriteProperty(writer, "OtherBreak", breakItem.OtherBreak);
WriteProperty(writer, "UserBreak", breakItem.UserBreak);
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WriteEndObject();
}
return sb.ToString();
}
private void WriteProperty(JsonWriter writer, string name, object value)
{
writer.WritePropertyName(name);
if (value == null)
{
writer.WriteNull();
}
else
{
writer.WriteValue(value);
}
}
}
[Serializable]
public class AppointmentData
{
public IEnumerable<ExternalEvent> ExteralEvents { get; set; }
public IEnumerable<Appointment> Appointments { get; set; }
public IEnumerable<Resource> Resources { get; set; }
}
[Serializable]
public class ClosedDay
{
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
}
[Serializable]
public class Appointment
{
public long Id { get; set; }
public long AppointmentId { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
public int StartHour { get; set; }
public int StartMinute { get; set; }
public int EndHour { get; set; }
public int EndMinute { get; set; }
public long ResourceId { get; set; }
public string Description { get; set; }
public long Status { get; set; }
public bool IsClass { get; set; }
public int ProcessingLength { get; set; }
public long ClientId { get; set; }
public string ClientName { get; set; }
public string ClientPhone { get; set; }
public string ClientNotes { get; set; }
public bool ClientHasMobile { get; set; }
public bool ClassFull { get; set; }
public string ClientWaiting { get; set; }
public string PromotionCode { get; set; }
public string ArrivalNote { get; set; }
public string Labels { get; set; }
public bool ReminderSent { get; set; }
public bool Cancelled { get; set; }
public IEnumerable<AppointmentItems> Items { get; set; }
}
[Serializable]
public class AppointmentItems
{
public string Name { get; set; }
public int Length { get; set; }
public int ProcessingTime { get; set; }
public string Resource { get; set; }
}
[Serializable]
public class OpenHours
{
public int DayOfWeek { get; set; }
public int? OpenHour { get; set; }
public int? CloseHour { get; set; }
}
[Serializable]
public class Resource
{
public Resource()
{
Breaks = new List<ResourceBreak>();
Blocks = new List<ResourceBlock>();
OpenCloseBreaks = new List<ResourceBreak>();
}
public long Id { get; set; }
public string Name { get; set; }
public int BlockLength { get; set; }
public int StartHour { get; set; }
public int EndHour { get; set; }
public IEnumerable<ResourceBreak> Breaks { get; set; }
public IEnumerable<ResourceBlock> Blocks { get; set; }
public IEnumerable<ResourceBreak> OpenCloseBreaks { get; set; }
}
[Serializable]
public class ExternalEvent
{
public long Id { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
public int DayOfWeek { get; set; }
public int StartHour { get; set; }
public int StartMinute { get; set; }
public int EndHour { get; set; }
public int EndMinute { get; set; }
public int Length { get; set; }
public string Description { get; set; }
}
[Serializable]
public class ResourceBreak
{
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
public int DayOfWeek { get; set; }
public int StartHour { get; set; }
public int StartMinute { get; set; }
public int Length { get; set; }
public string Description { get; set; }
public bool OtherBreak { get; set; }
public bool UserBreak { get; set; }
}
[Serializable]
public class ResourceBlock
{
public int StartHour { get; set; }
public int StartMinute { get; set; }
public int Length { get; set; }
}
[Serializable]
public class Status
{
public long Id { get; set; }
public string Description { get; set; }
public int Color { get; set; }
}
[Serializable]
public class LocationStaff
{
public long Id { get; set; }
public string Name { get; set; }
}
最佳答案
您是否尝试过使用 JSON.NET 将您的对象手动序列化为 JSON?当您拥有大量数据和许多属性时,我发现它要快得多。下面是一个例子:
public static string Serialise(YourObject data)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.WriteStartObject();
writer.WritePropertyName("propertyName1");
if (data.Property1 == null)
{
writer.WriteNull();
}
else
{
writer.WriteValue(data.Property1);
}
writer.WritePropertyName("propertyName2");
writer.WriteStartArray();
foreach (var something in data.CollectionProperty)
{
writer.WriteStartObject();
writer.WritePropertyName("p1");
writer.WriteValue(something.prop1);
writer.WritePropertyName("p2");
writer.WriteValue(something.prop2);
writer.WritePropertyName("p3");
writer.WriteValue(something.prop3);
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WriteEndObject();
}
return sb.ToString();
}
这意味着更多的工作,但如果性能符合您的目标,您将找不到更快的选择。
关于c# - NewtonSoft Json 序列化器性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23183550/
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.
假设我必须(小型到中型)阵列:tokens=["aaa","ccc","xxx","bbb","ccc","yyy","zzz"]template=["aaa","bbb","ccc"]如何确定tokens是否以相同的顺序包含template的所有条目?(请注意,在上面的示例中,应忽略第一个“ccc”,从而由于最后一个“ccc”而导致匹配。) 最佳答案 这适用于您的示例数据。tokens=["aaa","ccc","xxx","bbb","ccc","yyy","zzz"]template=["aaa","bbb","ccc"]po
我正在使用Ruby解决一些ProjectEuler问题,特别是这里我要讨论的问题25(Fibonacci数列中包含1000位数字的第一项的索引是多少?)。起初,我使用的是Ruby2.2.3,我将问题编码为:number=3a=1b=2whileb.to_s.length但后来我发现2.4.2版本有一个名为digits的方法,这正是我需要的。我转换为代码:whileb.digits.length当我比较这两种方法时,digits慢得多。时间./025/problem025.rb0.13s用户0.02s系统80%cpu0.190总计./025/problem025.rb2.19s用户0.0
我正在寻找一个用ruby演示计时器的在线示例,并发现了下面的代码。它按预期工作,但这个简单的程序使用30Mo内存(如Windows任务管理器中所示)和太多CPU有意义吗?非常感谢deftime_blockstart_time=Time.nowThread.new{yield}Time.now-start_timeenddefrepeat_every(seconds)whiletruedotime_spent=time_block{yield}#Tohandle-vesleepinteravalsleep(seconds-time_spent)iftime_spent
首先,我使用的是rails3.1.3和来自master的carrierwavegithub仓库的分支。我使用after_init钩子(Hook)来确定基于属性的字段页面模型实例并为这些字段定义属性访问器将值存储在序列化哈希中(希望它清楚我是什么谈论)。这是我正在做的事情的精简版:classPage省略mount_uploader命令让我可以访问我想要的属性。但是当我安装uploader时出现错误消息说“nil类的未定义新方法”我在源代码中读到有方法read_uploader和扩展模块中的write_uploader。我如何必须覆盖这些来制作mount_uploader命令使用我的“虚拟
如果用户是所有者,我有一个条件来检查说删除和文章。delete_articleifuser.owner?另一种方式是user.owner?&&delete_article选择它有什么好处还是它只是一种写作风格 最佳答案 性能不太可能成为该声明的问题。第一个要好得多-它更容易阅读。您future的自己和其他将开始编写代码的人会为此感谢您。 关于ruby-on-rails-如果条件与&&,是否有任何性能提升,我们在StackOverflow上找到一个类似的问题:
我如何做Ruby方法"Flatten"RubyMethod在C#中。此方法将锯齿状数组展平为一维数组。例如:s=[1,2,3]#=>[1,2,3]t=[4,5,6,[7,8]]#=>[4,5,6,[7,8]]a=[s,t,9,10]#=>[[1,2,3],[4,5,6,[7,8]],9,10]a.flatten#=>[1,2,3,4,5,6,7,8,9,10 最佳答案 递归解决方案:IEnumerableFlatten(IEnumerablearray){foreach(variteminarray){if(itemisIEnume