Производительность сериализатора NewtonSoft Json

У меня есть объект, который я сериализую в Json, используя NewtonSoft Json.Net. Объект относительно велик, в результате Json составляет около 300 КБ, но процесс сериализации занимает около 60 секунд.

Объекты, которые будут сериализованы, - это просто POCO.

Используемый мной код

string json = Newtonsoft.Json.JsonConvert.SerializeObject(data, Formatting.Indented);

Есть ли что-то, что можно сделать для ускорения сериализации, добавления атрибутов и т.д.

EDIT: Я только что протестировал с помощью сериализатора сериализации 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; }
}

Ответ 1

Вы пытались вручную сериализовать свой объект в JSON с помощью JSON.NET? Я нашел это намного быстрее, когда у вас есть большие данные и многие свойства. Ниже приведен пример:

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();
}

Это означает больше работы, но если производительность в вашей цели, вы не найдете более быстрый вариант.

Ответ 2

Вы должны дать Джону Беллами баллы за ответ, но здесь немного подробнее:

У меня была та же проблема с проектом, над которым я работаю, и я решил ее, следуя советам на этой странице:

http://www.newtonsoft.com/json/help/html/Performance.htm

В частности, они рекомендуют вручную сериализовать ваши объекты, когда производительность критична:

public static string ToJson(this Person p)
{
    StringWriter sw = new StringWriter();
    JsonTextWriter writer = new JsonTextWriter(sw);

    // {
    writer.WriteStartObject();

    // "name" : "Jerry"
    writer.WritePropertyName("name");
    writer.WriteValue(p.Name);

    // "likes": ["Comedy", "Superman"]
    writer.WritePropertyName("likes");
    writer.WriteStartArray();
    foreach (string like in p.Likes)
    {
        writer.WriteValue(like);
    }
    writer.WriteEndArray();

    // }
    writer.WriteEndObject();

    return sw.ToString();
}

Мой пример в VB выглядит так:

    Public Function SerializeWords(ByRef oWords As List(Of Word))
        Dim sb As New StringBuilder
        Dim sw As New IO.StringWriter(sb)
        Using oWriter As Newtonsoft.Json.JsonWriter = New Newtonsoft.Json.JsonTextWriter(sw)
            With oWriter
                .WriteStartArray()
                For Each oWord As Word In oWords
                    .WriteStartObject()

                    .WritePropertyName("ID")
                    .WriteValue(oWord.ID)

                    .WritePropertyName("Phonics")
                    .WriteValue(oWord.Phonics)

                    .WritePropertyName("Word_")
                    .WriteValue(oWord.Word_)

                    .WritePropertyName("WordLength")
                    .WriteValue(oWord.WordLength)

                    .WriteEndObject()
                Next
                .WriteEndArray()

            End With
        End Using
        Return sb.ToString

    End Function

Обратите внимание, как обе функции строго типизированы. Я полагаю, что когда вы используете Newtonsoft.Json.JsonConvert.SerializeObject() он использует рефлексию для выполнения работы (которая действительно может Newtonsoft.Json.JsonConvert.SerializeObject() когда у вас много объектов со многими свойствами).

В любом случае... как только я написал свой собственный сериализатор, мое время сериализации списка из 250 объектов Word сократилось с 28 секунд с помощью метода JsonConvert.SerializeObject() до 31 миллисекунды с использованием моей собственной функции.

Ответ 3

Это по-прежнему использует сериализатор (который использует отражение внутри), и это привело к решению моей проблемы с производительностью + Мне не пришлось вручную вводить все. Я не знаю, отличается ли JsonConvert.SerializeObject(), чем serializer.Serialize(), но Я полагал, что разделю в надежде, что это поможет

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);

using(JsonWriter writer = new JsonTextWriter(sw))
 {
  var serializer = new JsonSerializer();
  serializer.Serialize(writer, data);
 }