- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个对象,我正在使用 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/
最近开始学习MongoDB。今天老师教了我们 mongoexport 命令。在练习时,我遇到了一个典型的问题,包括教练在内的其他同学都没有遇到过。我在我的 Windows 10 机器上使用 Mongo
我是 JSON Schema 的新手,读过什么是 JSON Schema 等等。但我不知道如何将 JSON Schema 链接到 JSON 以针对该 JSON Schema 进行验证。谁能解释一下?
在 xml 中,我可以在另一个 xml 文件中包含一个文件并使用它。如果您的软件从 xml 获取配置文件但没有任何方法来分离配置,如 apache/ngnix(nginx.conf - site-av
我有一个 JSON 对象,其中包含一个本身是 JSON 对象的字符串。我如何反序列化它? 我希望能够做类似的事情: #[derive(Deserialize)] struct B { c: S
考虑以下 JSON { "a": "{\"b\": 12, \"c\": \"test\"}" } 我想定义一个泛型读取 Reads[Outer[T]]对于这种序列化的 Json import
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 11 个月前关闭。 Improve
我的旧项目在 MySQL 中有 Standard JSON 格式的数据。 对于我在 JS (Node.js) 和 DynamoDB 中的全新项目,关于 Standard JSON格式: 是否建议将其转
JSON 值字符串、数字、true、false、null 是否是有效的 JSON? 即,是 true 一个有效的 JSON 文档?还是必须是数组/对象? 一些验证器接受这个(例如 http://jso
我有一个 JSON 字符串,其中一个字段是文本字段。这个文本字段可以包含用户在 UI 中输入的文本,如果他们输入的文本是 JSON 文本,也许是为了说明一些编码,我需要对他们的文本进行编码,以便它不会
我正在通过 IBM MQ 调用处理数据,当由 ColdFusion 10 (10,0,11,285437) 序列化时,0 将作为 +0.0 返回,它会导致无效的 JSON并且无法反序列化。 stPol
我正在从三个数组中生成一个散列,然后尝试构建一个 json。我通过 json object has array 成功了。 require 'json' A = [['A1', 'A2', 'A3'],
我从 API 接收 JSON,响应可以是 30 种类型之一。每种类型都有一组唯一的字段,但所有响应都有一个字段 type 说明它是哪种类型。 我的方法是使用serde .我为每种响应类型创建一个结构并
我正在下载一个 JSON 文件,我已将其检查为带有“https://jsonlint.com”的有效 JSON 到文档目录。然后我打开文件并再次检查,结果显示为无效的 JSON。这怎么可能????这是
我正在尝试根据从 API 接收到的数据动态创建一个 JSON 对象。 收到的示例数据:将数据解码到下面给出的 CiItems 结构中 { "class_name": "test", "
我想从字符串转换为对象。 来自 {"key1": "{\n \"key2\": \"value2\",\n \"key3\": {\n \"key4\": \"value4\"\n }\n
目前我正在使用以下代码将嵌套的 json 转换为扁平化的 json: import ( "fmt" "github.com/nytlabs/gojsonexplode" ) func
我有一个使用来自第三方 API 的数据的应用程序。我需要将 json 解码为一个结构,这需要该结构具有“传入”json 字段的 json 标签。传出的 json 字段具有不同的命名约定,因此我需要不同
我想使用 JSON 架构来验证某些值。我有两个对象,称它们为 trackedItems 和 trackedItemGroups。 trackedItemGroups 是组名称和 trackedItem
考虑以下案例类模式, case class Y (a: String, b: String) case class X (dummy: String, b: Y) 字段b是可选的,我的一些数据集没有字
我正在存储 cat ~/path/to/file/blah | 的输出jq tojson 在一个变量中,稍后在带有 JSON 内容的 curl POST 中使用。它运作良好,但它删除了所有换行符。我知
我是一名优秀的程序员,十分优秀!