json.net manual serialization performance











up vote
0
down vote

favorite












I have a large data set that I'm trying to serialize that is about 15MB. I wanted to increase performance so I tried to manually serialize it. I was expecting some performance boost but it actually takes the same amount of time as the built in serializer that uses reflection. Currently it takes around ~7 minutes to serialize the data. Maybe I'm doing something wrong, maybe someone can point it out and maybe there is something else I can do to for a performance boost while serializing the data.



To begin with here is what my controller method looks like



    public void GetGanttDataByUserOrganization()
{

//takes around 5 seconds to get the DTO
var ganttChartDto = _ganttChartService.GetInitialGanttData();

//takes around 7 minutes to manually serialize the DTO it's 15MB
var st = ganttChartDto.ToGanttJson();

HttpContext.Response.WriteJson(st);

}


This part is long it's the ToGanttJson() extension method.



public static string ToGanttJson(this IEnumerable<ProductLineDto> p)
{
StringWriter sw = new StringWriter();
JsonTextWriter writer = new JsonTextWriter(sw);

writer.WritePropertyName("projectType");
writer.WriteStartArray();

foreach (var pLine in p)
{
writer.WriteStartObject();

writer.WritePropertyName("name");
writer.WriteValue(pLine.Name);

writer.WritePropertyName("productLineId");
writer.WriteValue(pLine.ProductLineId);

writer.WritePropertyName("sortOrder");
writer.WriteValue(pLine.SortOrder);

writer.WritePropertyName("projectType");

writer.WriteStartArray();
foreach (var pType in pLine.ProjectType)
{
writer = WriteProjectTypeDto(writer, pType);
}
writer.WriteEndArray();

writer.WriteEndObject();

}

writer.WriteEndArray();

return sw.ToString();
}

private static JsonTextWriter WriteProjectDto(JsonTextWriter writer, ProjectDto pjects)
{
writer.WriteStartObject();

writer.WritePropertyName("projectId");
writer.WriteValue(pjects.ProjectId);

writer.WritePropertyName("projectName");
writer.WriteValue(pjects.ProjectName);

writer.WritePropertyName("parent");
writer.WriteValue("");

writer.WritePropertyName("startDate");
writer.WriteValue(pjects.StartDate);

writer.WritePropertyName("projectStatus");
writer.WriteValue(pjects.ProjectStatus);

writer.WritePropertyName("projectTypeRowId");
writer.WriteValue(pjects.ProjectTypeRowId);

writer.WritePropertyName("projectTypeDescription");
writer.WriteValue(pjects.ProjectTypeDescription);

writer.WritePropertyName("sortOrder");
writer.WriteValue(pjects.SortOrder);

writer.WritePropertyName("projectStatusId");
writer.WriteValue(pjects.ProjectStatusId);

writer.WritePropertyName("productLineId");
writer.WriteValue(pjects.ProductLineId);

writer.WritePropertyName("projectTypeId");
writer.WriteValue(pjects.ProjectTypeId);

writer.WritePropertyName("organizationId");
writer.WriteValue(pjects.OrganizationId);

writer.WritePropertyName("clientId");
writer.WriteValue(pjects.ClientId);

writer.WritePropertyName("program");
writer.WriteValue(pjects.Program);

writer.WritePropertyName("productLineName");
writer.WriteValue(pjects.ProductLineName);

writer.WritePropertyName("isShotgun");
writer.WriteValue(pjects.IsShotgun);


writer.WritePropertyName("shotgunLink");
writer.WriteValue(pjects.ShotgunLink);

writer.WritePropertyName("budgetCode");
writer.WriteValue(pjects.BudgetCode);

writer.WritePropertyName("modelType");
writer.WriteValue(pjects.ModelType);

writer.WritePropertyName("modelYear");
writer.WriteValue(pjects.ModelYear);

writer.WritePropertyName("flow");
writer.WriteValue(pjects.Flow);

writer.WritePropertyName("description");
writer.WriteValue(pjects.Description);

writer = WriteOrganizationDto(writer, pjects.Organization);

writer.WritePropertyName("projectPersons");

writer.WriteStartArray();
foreach (var person in pjects.ProjectPersons)
{
writer = WriteProjectPersonDto(writer, person);
}
writer.WriteEndArray();

writer.WritePropertyName("children");

writer.WriteStartArray();
foreach (var sp in pjects.Children)
{
writer = WriteSubProjectDto(writer, sp);
}
writer.WriteEndArray();

writer.WriteEndObject();

return writer;

}

private static JsonTextWriter WriteSubProjectDto(JsonTextWriter writer, SubProjectDto sp) {

writer.WriteStartObject();

writer.WritePropertyName("projectId");
writer.WriteValue(sp.ProjectId);

writer.WritePropertyName("subProjectId");
writer.WriteValue(sp.SubProjectId);

writer.WritePropertyName("projectName");
writer.WriteValue(sp.ProjectName);


writer.WritePropertyName("parentId");
writer.WriteValue(sp.ParentId);


writer.WritePropertyName("subProjectTypeId");
writer.WriteValue(sp.SubProjectTypeId);

writer.WritePropertyName("organizationId");
writer.WriteValue(sp.OrganizationId);

writer.WritePropertyName("sortOrder");
writer.WriteValue(sp.SortOrder);

writer.WritePropertyName("clientId");
writer.WriteValue(sp.ClientId);

writer.WritePropertyName("projectClientId");
writer.WriteValue(sp.ProjectClientId);

writer.WritePropertyName("subProjectParentClientId");
writer.WriteValue(sp.SubProjectParentClientId);


writer.WritePropertyName("parentSubProjectId");
writer.WriteValue(sp.ParentSubProjectId);

writer.WritePropertyName("isShotgun");
writer.WriteValue(sp.IsShotgun);

writer = WriteOrganizationDto(writer, sp.Organization);

writer.WritePropertyName("children");

writer.WriteStartArray();
foreach (var childSp in sp.Children)
{
writer = WriteSubProjectDto(writer, childSp);
}

writer.WriteEndArray();

writer.WritePropertyName("subProjectPersons");

writer.WriteStartArray();

foreach (var spp in sp.SubProjectPersons)
writer = WriteSubprojectPersonDto(writer, spp);

writer.WriteEndArray();

//activities
writer.WritePropertyName("activities");

writer.WriteStartArray();

foreach (var act in sp.Activities)
writer = WriteActivityDto(writer, act);

writer.WriteEndArray();
writer.WriteEndObject();
return writer;
}

public static JsonTextWriter WriteActivityDto(JsonTextWriter writer, ActivityDto act)
{
writer.WriteStartObject();


writer.WritePropertyName("acivityId");
writer.WriteValue(act.ActivityId);

writer.WritePropertyName("description");
writer.WriteValue(act.Description);

writer.WritePropertyName("activityName");
writer.WriteValue(act.Name);

writer.WritePropertyName("activityDateRange");
writer.WriteValue("");

writer.WritePropertyName("startDate");
writer.WriteValue(act.StartDate);

writer.WritePropertyName("endDate");
writer.WriteValue(act.EndDate);

writer.WritePropertyName("duration");
writer.WriteValue(act.Duration);

writer.WritePropertyName("pctComplete");
writer.WriteValue(act.PctComplete);

writer.WritePropertyName("name");
writer.WriteValue(act.Name);

writer.WritePropertyName("projectId");
writer.WriteValue(act.ProjectId);

writer.WritePropertyName("subProjectId");
writer.WriteValue(act.SubProjectId);

writer.WritePropertyName("organizationId");
writer.WriteValue(act.OrganizationId);

writer.WritePropertyName("activityTypeId");
writer.WriteValue(act.ActivityTypeId);

writer.WritePropertyName("sortOrder");
writer.WriteValue(act.SortOrder);

writer.WritePropertyName("parentClientId");
writer.WriteValue("");

writer.WritePropertyName("clientId");
writer.WriteValue("");

writer.WritePropertyName("projectName");
writer.WriteValue(act.ProjectName);

writer.WritePropertyName("plannedStart");
writer.WriteValue(act.PlannedStart);

writer.WritePropertyName("plannedEnd");
writer.WriteValue(act.PlannedEnd);

writer.WritePropertyName("laserProcessId");
writer.WriteValue(act.LaserProcessId);

writer.WritePropertyName("laserThicknessId");
writer.WriteValue(act.LaserThicknessId);

writer.WritePropertyName("materialId");
writer.WriteValue(act.MaterialId);

writer.WritePropertyName("millingMinimumRadiusId");
writer.WriteValue(act.MillingMinimumRadiusId);

writer.WritePropertyName("millingScallopHeightId");
writer.WriteValue(act.MillingScallopHeightId);

writer.WritePropertyName("modelCategoryId");
writer.WriteValue(act.ModelCategoryId);

writer.WritePropertyName("modelTypeId");
writer.WriteValue(act.ModelTypeId);

writer.WritePropertyName("rpfdmNozzleId");
writer.WriteValue(act.RpfdmNozzleId);

writer.WritePropertyName("rpfillTypeId");
writer.WriteValue(act.RpfillTypeId);

writer.WritePropertyName("varianceId");
writer.WriteValue(act.VarianceId);

writer.WritePropertyName("quantityId");
writer.WriteValue(act.QuantityId);

writer.WritePropertyName("specNotes");
writer.WriteValue(act.SpecNotes);

writer.WritePropertyName("parentActivityID");
writer.WriteValue(act.ParentActivityID);

writer.WritePropertyName("rpsparseId");
writer.WriteValue(act.RpsparseId);

writer = WriteOrganizationDto(writer, act.Organization);

writer.WritePropertyName("children");

writer.WriteStartArray();
foreach(var child in act.Children)
writer = WriteActivityDto(writer, child);
writer.WriteEndArray();

writer.WritePropertyName("activityPersons");

writer.WriteStartArray();
foreach (var actPerson in act.ActivityPersons)
writer = WriteActivityPersonDto(writer, actPerson);
writer.WriteEndArray();

writer.WritePropertyName("activityMachines");

writer.WriteStartArray();
foreach (var am in act.ActivityMachines)
writer = WriteActivitMachineDto(writer, am);
writer.WriteEndArray();

writer.WritePropertyName("customForm");

writer.WriteStartArray();
foreach (var cf in act.CustomForm)
writer = WirteActivityTypeCustomFormDto(writer, cf);
writer.WriteEndArray();

//activitytype


writer.WriteEndObject();
return writer;
}

public static JsonTextWriter WriteActivityTypeDto(JsonTextWriter writer, ActivityTypeDto at)
{
writer.WriteStartObject();

writer.WritePropertyName("activityTypeId");
writer.WriteValue(at.ActivityTypeId);

writer.WritePropertyName("isMileStone");
writer.WriteValue(at.IsMileStone);

writer.WritePropertyName("description");
writer.WriteValue(at.Description);

writer.WritePropertyName("isFlagged");
writer.WriteValue(at.IsFlagged);

writer.WritePropertyName("showInProject");
writer.WriteValue(at.ShowInProject);

writer.WritePropertyName("showInParent");
writer.WriteValue(at.ShowInParent);

writer.WritePropertyName("color");
writer.WriteValue(at.Color);

writer.WritePropertyName("pattern");
writer.WriteValue(at.Pattern);

writer.WritePropertyName("milestoneImage");
writer.WriteValue(at.MilestoneImage);

writer.WritePropertyName("activityCount");
writer.WriteValue(at.ActivityCount);

writer.WritePropertyName("replacedBy");
writer.WriteValue(at.ReplacedBy);

writer = WirteResourceTypeControlDto(writer, at.ResourceType);

writer.WritePropertyName("modelTypes");

writer.WriteStartArray();
foreach (var mt in at.ModelTypes)
writer = WirteGanttControlDto(writer, mt);
writer.WriteEndArray();

writer.WritePropertyName("activitySpecs");

writer.WriteStartArray();
foreach (var spec in at.ActivitySpecs)
writer = WirteActivityQuantityDto(writer, spec);
writer.WriteEndArray();

writer.WriteEndObject();
return writer;
}

public static JsonTextWriter WirteActivityQuantityDto(JsonTextWriter writer, ActivityQuantityDto spec)
{
writer.WriteStartObject();

writer.WritePropertyName("activityQuantityID");
writer.WriteValue(spec.ActivityQuantityID);

writer.WritePropertyName("description");
writer.WriteValue(spec.Description);

writer.WritePropertyName("activityTypeID");
writer.WriteValue(spec.ActivityTypeID);

writer.WritePropertyName("sortOrder");
writer.WriteValue(spec.SortOrder);

//list

writer.WriteEndObject();
return writer;
}

public static JsonTextWriter WirteActivityTypeQuantityListDto(JsonTextWriter writer, ActivityTypeQuantityListDto value)
{
writer.WriteStartObject();

writer.WritePropertyName("activityTypeQuantityListID");
writer.WriteValue(value.ActivityTypeQuantityListID);

writer.WritePropertyName("description");
writer.WriteValue(value.Description);

writer.WritePropertyName("activityTypeQuantityID");
writer.WriteValue(value.ActivityTypeQuantityID);

writer.WritePropertyName("sortOrder");
writer.WriteValue(value.SortOrder);

writer.WritePropertyName("isDefaultValue");
writer.WriteValue(value.IsDefaultValue);

writer.WriteEndObject();

return writer;
}

public static JsonTextWriter WirteResourceTypeControlDto(JsonTextWriter writer, ResourceTypeControlDto at)
{
writer.WritePropertyName("resourceType");

writer.WriteStartObject();

writer.WritePropertyName("id");
writer.WriteValue(at.Id);

writer.WritePropertyName("name");
writer.WriteValue(at.Name);

writer.WriteEndObject();
return writer;
}

public static JsonTextWriter WirteGanttControlDto(JsonTextWriter writer, GanttControlDto at)
{
writer.WriteStartObject();

writer.WritePropertyName("id");
writer.WriteValue(at.ID);

writer.WritePropertyName("name");
writer.WriteValue(at.Name);

writer.WriteEndObject();
return writer;
}

public static JsonTextWriter WirteActivityTypeCustomFormDto(JsonTextWriter writer, ActivityTypeCustomFormDto at)
{
writer.WriteStartObject();

writer.WritePropertyName("activityQuantityID");
writer.WriteValue(at.activityQuantityID);

writer.WritePropertyName("activityTypeQuantityListID");
writer.WriteValue(at.ActivityTypeQuantityListID);

writer.WriteEndObject();
return writer;
}

public static JsonTextWriter WriteActivitMachineDto(JsonTextWriter writer, ActivityMachinesDto at)
{

writer.WriteStartObject();

writer.WritePropertyName("activityMachineID");
writer.WriteValue(at.ActivityMachineID);


writer.WritePropertyName("activityID");
writer.WriteValue(at.ActivityID);


writer.WritePropertyName("effort");
writer.WriteValue(at.Effort);


writer.WritePropertyName("machineID");
writer.WriteValue(at.MachineID);


writer.WritePropertyName("machineTypeID");
writer.WriteValue(at.MachineTypeID);


writer.WritePropertyName("name");
writer.WriteValue(at.Name);

writer.WritePropertyName("machineType");
writer = WriteMachineTypeDto(writer, at.MachineType);

writer.WriteEndObject();
return writer;
}

public static JsonTextWriter WriteMachineTypeDto(JsonTextWriter writer, MachineTypeDto at) {
writer.WriteStartObject();


writer.WritePropertyName("machineTypeID");
writer.WriteValue(at.MachineTypeID);


writer.WritePropertyName("description");
writer.WriteValue(at.Description);


writer.WritePropertyName("isFlagged");
writer.WriteValue(at.IsFlagged);

writer.WriteEndObject();
return writer;
}

public static JsonTextWriter WriteActivityPersonDto(JsonTextWriter writer, ActivityPersonDto ap) {
writer.WriteStartObject();

writer.WritePropertyName("activityPersonId");
writer.WriteValue(ap.ActivityPersonId);

writer.WritePropertyName("activityID");
writer.WriteValue(ap.ActivityID);

writer.WritePropertyName("resourceTypeID");
writer.WriteValue(ap.ResourceTypeID);

writer.WritePropertyName("personID");
writer.WriteValue(ap.PersonID);

writer.WritePropertyName("effort");
writer.WriteValue(ap.Effort);

writer.WritePropertyName("cost");
writer.WriteValue(ap.Cost);

writer.WritePropertyName("resourceType");
writer = WriteResourceTypeDto(writer, ap.ResourceType);

writer.WritePropertyName("person");
writer = WritePersonDto(writer, ap.Person);
//activitydto

writer.WriteEndObject();
return writer;
}

public static JsonTextWriter WritePersonDto(JsonTextWriter writer, PersonDto p)
{
writer.WriteStartObject();

writer.WritePropertyName("personId");
writer.WriteValue(p.PersonId);

writer.WritePropertyName("firstName");
writer.WriteValue(p.FirstName);

writer.WritePropertyName("lastName");
writer.WriteValue(p.LastName);

writer.WriteEndObject();
return writer;
}

public static JsonTextWriter WriteResourceTypeDto(JsonTextWriter writer, ResourceTypeDto rt) {
writer.WriteStartObject();

writer.WritePropertyName("resourceTypeId");
writer.WriteValue(rt.ResourceTypeId);

writer.WritePropertyName("description");
writer.WriteValue(rt.Description);

writer.WritePropertyName("isFlagged");
writer.WriteValue(rt.IsFlagged);

writer.WritePropertyName("resourceTypeCount");
writer.WriteValue(rt.ResourceTypeCount);

writer.WritePropertyName("replacedBy");
writer.WriteValue(rt.ReplacedBy);

writer.WriteEndObject();
return writer;
}

public static JsonTextWriter WriteSubprojectPersonDto(JsonTextWriter writer, SubProjectPersonDto spp) {
writer.WriteStartObject();

writer.WritePropertyName("subProjectPersonId");
writer.WriteValue(spp.SubProjectPersonId);

writer.WritePropertyName("subProjectId");
writer.WriteValue(spp.SubProjectId);

writer.WritePropertyName("personId");
writer.WriteValue(spp.PersonId);

writer.WritePropertyName("subProjectPersonRoles");

writer.WriteStartArray();

foreach (var spr in spp.SubProjectPersonRoles)
writer = WriteProjectPersonRoleDto(writer, spr);

writer.WriteEndArray();


writer.WriteEndObject();

return writer;
}

private static JsonTextWriter WriteProjectPersonDto(JsonTextWriter writer, ProjectPersonDto person)
{

writer.WriteStartObject();

writer.WritePropertyName("projectPersonId");
writer.WriteValue(person.ProjectPersonId);

writer.WritePropertyName("projectId");
writer.WriteValue(person.ProjectId);

writer.WritePropertyName("personId");
writer.WriteValue(person.PersonId);

writer.WritePropertyName("projectPersonRoles");

writer.WriteStartArray();

foreach (var ppr in person.ProjectPersonRoles)
writer = WriteProjectPersonRoleDto(writer, ppr);

writer.WriteEndArray();


writer.WriteEndObject();
return writer;
}

private static JsonTextWriter WriteProjectPersonRoleDto(JsonTextWriter writer, ProjectPersonRoleDto ppr)
{
writer.WriteStartObject();

writer.WritePropertyName("projectPersonRoleID");
writer.WriteValue(ppr.ProjectPersonRoleID);

writer.WritePropertyName("projectPersonID");
writer.WriteValue(ppr.ProjectPersonID);

writer.WritePropertyName("roleTypeID");
writer.WriteValue(ppr.RoleTypeID);

writer.WritePropertyName("description");
writer.WriteValue(ppr.Description);


writer.WriteEndObject();
return writer;
}

private static JsonTextWriter WriteOrganizationDto(JsonTextWriter writer, OrganizationDto org)
{

writer.WritePropertyName("organization");


if (org == null)
{
writer.WriteNull();
return writer;
}

writer.WriteStartObject();

writer.WritePropertyName("OrganizationId");
writer.WriteValue(org.OrganizationId);

writer.WritePropertyName("OrganizationName");
writer.WriteValue(org.OrganizationName);

writer.WritePropertyName("employees");
writer.WriteStartArray();
writer.WriteEndArray();

writer.WritePropertyName("machines");
writer.WriteStartArray();
writer.WriteEndArray();

writer.WriteEndObject();

return writer;
}

private static JsonTextWriter WriteProjectTypeDto(JsonTextWriter writer, ProjectTypeDto pType) {


writer.WriteStartObject();
writer.WritePropertyName("projectTypeId");
writer.WriteValue(pType.ProjectTypeId);

writer.WritePropertyName("description");
writer.WriteValue(pType.Description);

writer.WritePropertyName("rowId");
writer.WriteValue(pType.RowId);

writer.WritePropertyName("sortOrder");
writer.WriteValue(pType.SortOrder);

writer.WritePropertyName("projects");

writer.WriteStartArray();
foreach (var pjects in pType.Projects)
{
writer = WriteProjectDto(writer, pjects);
}
writer.WriteEndArray();

writer.WriteEndObject();

return writer;
}


Now this is what my current code looks like with the out of the box serializer that takes the same amount of time.



Here is the controller method.



public virtual BetterJsonResult GetGanttDataByUserOrganization()
{

var ganttChartDto = _ganttChartService.GetInitialGanttData();

return BetterJson(ganttChartDto);
}


This is what the BetterJson which basically returns this



   ...
protected virtual void SerializeData(HttpResponseBase response)
{
if (ErrorMessages.Any())
{
Data = new
{
ErrorMessage = string.Join("n", ErrorMessages),
ErrorMessages = ErrorMessages.ToArray()
};

response.StatusCode = 400;
}

if (Data == null) return;

response.Write(Data.ToJson());
}

public static string ToJson<T>(this T obj, bool includeNull = true)
{
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = new JsonConverter { new StringEnumConverter() },
NullValueHandling = includeNull ? NullValueHandling.Include : NullValueHandling.Ignore,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};

return JsonConvert.SerializeObject(obj, settings);
}








share


























    up vote
    0
    down vote

    favorite












    I have a large data set that I'm trying to serialize that is about 15MB. I wanted to increase performance so I tried to manually serialize it. I was expecting some performance boost but it actually takes the same amount of time as the built in serializer that uses reflection. Currently it takes around ~7 minutes to serialize the data. Maybe I'm doing something wrong, maybe someone can point it out and maybe there is something else I can do to for a performance boost while serializing the data.



    To begin with here is what my controller method looks like



        public void GetGanttDataByUserOrganization()
    {

    //takes around 5 seconds to get the DTO
    var ganttChartDto = _ganttChartService.GetInitialGanttData();

    //takes around 7 minutes to manually serialize the DTO it's 15MB
    var st = ganttChartDto.ToGanttJson();

    HttpContext.Response.WriteJson(st);

    }


    This part is long it's the ToGanttJson() extension method.



    public static string ToGanttJson(this IEnumerable<ProductLineDto> p)
    {
    StringWriter sw = new StringWriter();
    JsonTextWriter writer = new JsonTextWriter(sw);

    writer.WritePropertyName("projectType");
    writer.WriteStartArray();

    foreach (var pLine in p)
    {
    writer.WriteStartObject();

    writer.WritePropertyName("name");
    writer.WriteValue(pLine.Name);

    writer.WritePropertyName("productLineId");
    writer.WriteValue(pLine.ProductLineId);

    writer.WritePropertyName("sortOrder");
    writer.WriteValue(pLine.SortOrder);

    writer.WritePropertyName("projectType");

    writer.WriteStartArray();
    foreach (var pType in pLine.ProjectType)
    {
    writer = WriteProjectTypeDto(writer, pType);
    }
    writer.WriteEndArray();

    writer.WriteEndObject();

    }

    writer.WriteEndArray();

    return sw.ToString();
    }

    private static JsonTextWriter WriteProjectDto(JsonTextWriter writer, ProjectDto pjects)
    {
    writer.WriteStartObject();

    writer.WritePropertyName("projectId");
    writer.WriteValue(pjects.ProjectId);

    writer.WritePropertyName("projectName");
    writer.WriteValue(pjects.ProjectName);

    writer.WritePropertyName("parent");
    writer.WriteValue("");

    writer.WritePropertyName("startDate");
    writer.WriteValue(pjects.StartDate);

    writer.WritePropertyName("projectStatus");
    writer.WriteValue(pjects.ProjectStatus);

    writer.WritePropertyName("projectTypeRowId");
    writer.WriteValue(pjects.ProjectTypeRowId);

    writer.WritePropertyName("projectTypeDescription");
    writer.WriteValue(pjects.ProjectTypeDescription);

    writer.WritePropertyName("sortOrder");
    writer.WriteValue(pjects.SortOrder);

    writer.WritePropertyName("projectStatusId");
    writer.WriteValue(pjects.ProjectStatusId);

    writer.WritePropertyName("productLineId");
    writer.WriteValue(pjects.ProductLineId);

    writer.WritePropertyName("projectTypeId");
    writer.WriteValue(pjects.ProjectTypeId);

    writer.WritePropertyName("organizationId");
    writer.WriteValue(pjects.OrganizationId);

    writer.WritePropertyName("clientId");
    writer.WriteValue(pjects.ClientId);

    writer.WritePropertyName("program");
    writer.WriteValue(pjects.Program);

    writer.WritePropertyName("productLineName");
    writer.WriteValue(pjects.ProductLineName);

    writer.WritePropertyName("isShotgun");
    writer.WriteValue(pjects.IsShotgun);


    writer.WritePropertyName("shotgunLink");
    writer.WriteValue(pjects.ShotgunLink);

    writer.WritePropertyName("budgetCode");
    writer.WriteValue(pjects.BudgetCode);

    writer.WritePropertyName("modelType");
    writer.WriteValue(pjects.ModelType);

    writer.WritePropertyName("modelYear");
    writer.WriteValue(pjects.ModelYear);

    writer.WritePropertyName("flow");
    writer.WriteValue(pjects.Flow);

    writer.WritePropertyName("description");
    writer.WriteValue(pjects.Description);

    writer = WriteOrganizationDto(writer, pjects.Organization);

    writer.WritePropertyName("projectPersons");

    writer.WriteStartArray();
    foreach (var person in pjects.ProjectPersons)
    {
    writer = WriteProjectPersonDto(writer, person);
    }
    writer.WriteEndArray();

    writer.WritePropertyName("children");

    writer.WriteStartArray();
    foreach (var sp in pjects.Children)
    {
    writer = WriteSubProjectDto(writer, sp);
    }
    writer.WriteEndArray();

    writer.WriteEndObject();

    return writer;

    }

    private static JsonTextWriter WriteSubProjectDto(JsonTextWriter writer, SubProjectDto sp) {

    writer.WriteStartObject();

    writer.WritePropertyName("projectId");
    writer.WriteValue(sp.ProjectId);

    writer.WritePropertyName("subProjectId");
    writer.WriteValue(sp.SubProjectId);

    writer.WritePropertyName("projectName");
    writer.WriteValue(sp.ProjectName);


    writer.WritePropertyName("parentId");
    writer.WriteValue(sp.ParentId);


    writer.WritePropertyName("subProjectTypeId");
    writer.WriteValue(sp.SubProjectTypeId);

    writer.WritePropertyName("organizationId");
    writer.WriteValue(sp.OrganizationId);

    writer.WritePropertyName("sortOrder");
    writer.WriteValue(sp.SortOrder);

    writer.WritePropertyName("clientId");
    writer.WriteValue(sp.ClientId);

    writer.WritePropertyName("projectClientId");
    writer.WriteValue(sp.ProjectClientId);

    writer.WritePropertyName("subProjectParentClientId");
    writer.WriteValue(sp.SubProjectParentClientId);


    writer.WritePropertyName("parentSubProjectId");
    writer.WriteValue(sp.ParentSubProjectId);

    writer.WritePropertyName("isShotgun");
    writer.WriteValue(sp.IsShotgun);

    writer = WriteOrganizationDto(writer, sp.Organization);

    writer.WritePropertyName("children");

    writer.WriteStartArray();
    foreach (var childSp in sp.Children)
    {
    writer = WriteSubProjectDto(writer, childSp);
    }

    writer.WriteEndArray();

    writer.WritePropertyName("subProjectPersons");

    writer.WriteStartArray();

    foreach (var spp in sp.SubProjectPersons)
    writer = WriteSubprojectPersonDto(writer, spp);

    writer.WriteEndArray();

    //activities
    writer.WritePropertyName("activities");

    writer.WriteStartArray();

    foreach (var act in sp.Activities)
    writer = WriteActivityDto(writer, act);

    writer.WriteEndArray();
    writer.WriteEndObject();
    return writer;
    }

    public static JsonTextWriter WriteActivityDto(JsonTextWriter writer, ActivityDto act)
    {
    writer.WriteStartObject();


    writer.WritePropertyName("acivityId");
    writer.WriteValue(act.ActivityId);

    writer.WritePropertyName("description");
    writer.WriteValue(act.Description);

    writer.WritePropertyName("activityName");
    writer.WriteValue(act.Name);

    writer.WritePropertyName("activityDateRange");
    writer.WriteValue("");

    writer.WritePropertyName("startDate");
    writer.WriteValue(act.StartDate);

    writer.WritePropertyName("endDate");
    writer.WriteValue(act.EndDate);

    writer.WritePropertyName("duration");
    writer.WriteValue(act.Duration);

    writer.WritePropertyName("pctComplete");
    writer.WriteValue(act.PctComplete);

    writer.WritePropertyName("name");
    writer.WriteValue(act.Name);

    writer.WritePropertyName("projectId");
    writer.WriteValue(act.ProjectId);

    writer.WritePropertyName("subProjectId");
    writer.WriteValue(act.SubProjectId);

    writer.WritePropertyName("organizationId");
    writer.WriteValue(act.OrganizationId);

    writer.WritePropertyName("activityTypeId");
    writer.WriteValue(act.ActivityTypeId);

    writer.WritePropertyName("sortOrder");
    writer.WriteValue(act.SortOrder);

    writer.WritePropertyName("parentClientId");
    writer.WriteValue("");

    writer.WritePropertyName("clientId");
    writer.WriteValue("");

    writer.WritePropertyName("projectName");
    writer.WriteValue(act.ProjectName);

    writer.WritePropertyName("plannedStart");
    writer.WriteValue(act.PlannedStart);

    writer.WritePropertyName("plannedEnd");
    writer.WriteValue(act.PlannedEnd);

    writer.WritePropertyName("laserProcessId");
    writer.WriteValue(act.LaserProcessId);

    writer.WritePropertyName("laserThicknessId");
    writer.WriteValue(act.LaserThicknessId);

    writer.WritePropertyName("materialId");
    writer.WriteValue(act.MaterialId);

    writer.WritePropertyName("millingMinimumRadiusId");
    writer.WriteValue(act.MillingMinimumRadiusId);

    writer.WritePropertyName("millingScallopHeightId");
    writer.WriteValue(act.MillingScallopHeightId);

    writer.WritePropertyName("modelCategoryId");
    writer.WriteValue(act.ModelCategoryId);

    writer.WritePropertyName("modelTypeId");
    writer.WriteValue(act.ModelTypeId);

    writer.WritePropertyName("rpfdmNozzleId");
    writer.WriteValue(act.RpfdmNozzleId);

    writer.WritePropertyName("rpfillTypeId");
    writer.WriteValue(act.RpfillTypeId);

    writer.WritePropertyName("varianceId");
    writer.WriteValue(act.VarianceId);

    writer.WritePropertyName("quantityId");
    writer.WriteValue(act.QuantityId);

    writer.WritePropertyName("specNotes");
    writer.WriteValue(act.SpecNotes);

    writer.WritePropertyName("parentActivityID");
    writer.WriteValue(act.ParentActivityID);

    writer.WritePropertyName("rpsparseId");
    writer.WriteValue(act.RpsparseId);

    writer = WriteOrganizationDto(writer, act.Organization);

    writer.WritePropertyName("children");

    writer.WriteStartArray();
    foreach(var child in act.Children)
    writer = WriteActivityDto(writer, child);
    writer.WriteEndArray();

    writer.WritePropertyName("activityPersons");

    writer.WriteStartArray();
    foreach (var actPerson in act.ActivityPersons)
    writer = WriteActivityPersonDto(writer, actPerson);
    writer.WriteEndArray();

    writer.WritePropertyName("activityMachines");

    writer.WriteStartArray();
    foreach (var am in act.ActivityMachines)
    writer = WriteActivitMachineDto(writer, am);
    writer.WriteEndArray();

    writer.WritePropertyName("customForm");

    writer.WriteStartArray();
    foreach (var cf in act.CustomForm)
    writer = WirteActivityTypeCustomFormDto(writer, cf);
    writer.WriteEndArray();

    //activitytype


    writer.WriteEndObject();
    return writer;
    }

    public static JsonTextWriter WriteActivityTypeDto(JsonTextWriter writer, ActivityTypeDto at)
    {
    writer.WriteStartObject();

    writer.WritePropertyName("activityTypeId");
    writer.WriteValue(at.ActivityTypeId);

    writer.WritePropertyName("isMileStone");
    writer.WriteValue(at.IsMileStone);

    writer.WritePropertyName("description");
    writer.WriteValue(at.Description);

    writer.WritePropertyName("isFlagged");
    writer.WriteValue(at.IsFlagged);

    writer.WritePropertyName("showInProject");
    writer.WriteValue(at.ShowInProject);

    writer.WritePropertyName("showInParent");
    writer.WriteValue(at.ShowInParent);

    writer.WritePropertyName("color");
    writer.WriteValue(at.Color);

    writer.WritePropertyName("pattern");
    writer.WriteValue(at.Pattern);

    writer.WritePropertyName("milestoneImage");
    writer.WriteValue(at.MilestoneImage);

    writer.WritePropertyName("activityCount");
    writer.WriteValue(at.ActivityCount);

    writer.WritePropertyName("replacedBy");
    writer.WriteValue(at.ReplacedBy);

    writer = WirteResourceTypeControlDto(writer, at.ResourceType);

    writer.WritePropertyName("modelTypes");

    writer.WriteStartArray();
    foreach (var mt in at.ModelTypes)
    writer = WirteGanttControlDto(writer, mt);
    writer.WriteEndArray();

    writer.WritePropertyName("activitySpecs");

    writer.WriteStartArray();
    foreach (var spec in at.ActivitySpecs)
    writer = WirteActivityQuantityDto(writer, spec);
    writer.WriteEndArray();

    writer.WriteEndObject();
    return writer;
    }

    public static JsonTextWriter WirteActivityQuantityDto(JsonTextWriter writer, ActivityQuantityDto spec)
    {
    writer.WriteStartObject();

    writer.WritePropertyName("activityQuantityID");
    writer.WriteValue(spec.ActivityQuantityID);

    writer.WritePropertyName("description");
    writer.WriteValue(spec.Description);

    writer.WritePropertyName("activityTypeID");
    writer.WriteValue(spec.ActivityTypeID);

    writer.WritePropertyName("sortOrder");
    writer.WriteValue(spec.SortOrder);

    //list

    writer.WriteEndObject();
    return writer;
    }

    public static JsonTextWriter WirteActivityTypeQuantityListDto(JsonTextWriter writer, ActivityTypeQuantityListDto value)
    {
    writer.WriteStartObject();

    writer.WritePropertyName("activityTypeQuantityListID");
    writer.WriteValue(value.ActivityTypeQuantityListID);

    writer.WritePropertyName("description");
    writer.WriteValue(value.Description);

    writer.WritePropertyName("activityTypeQuantityID");
    writer.WriteValue(value.ActivityTypeQuantityID);

    writer.WritePropertyName("sortOrder");
    writer.WriteValue(value.SortOrder);

    writer.WritePropertyName("isDefaultValue");
    writer.WriteValue(value.IsDefaultValue);

    writer.WriteEndObject();

    return writer;
    }

    public static JsonTextWriter WirteResourceTypeControlDto(JsonTextWriter writer, ResourceTypeControlDto at)
    {
    writer.WritePropertyName("resourceType");

    writer.WriteStartObject();

    writer.WritePropertyName("id");
    writer.WriteValue(at.Id);

    writer.WritePropertyName("name");
    writer.WriteValue(at.Name);

    writer.WriteEndObject();
    return writer;
    }

    public static JsonTextWriter WirteGanttControlDto(JsonTextWriter writer, GanttControlDto at)
    {
    writer.WriteStartObject();

    writer.WritePropertyName("id");
    writer.WriteValue(at.ID);

    writer.WritePropertyName("name");
    writer.WriteValue(at.Name);

    writer.WriteEndObject();
    return writer;
    }

    public static JsonTextWriter WirteActivityTypeCustomFormDto(JsonTextWriter writer, ActivityTypeCustomFormDto at)
    {
    writer.WriteStartObject();

    writer.WritePropertyName("activityQuantityID");
    writer.WriteValue(at.activityQuantityID);

    writer.WritePropertyName("activityTypeQuantityListID");
    writer.WriteValue(at.ActivityTypeQuantityListID);

    writer.WriteEndObject();
    return writer;
    }

    public static JsonTextWriter WriteActivitMachineDto(JsonTextWriter writer, ActivityMachinesDto at)
    {

    writer.WriteStartObject();

    writer.WritePropertyName("activityMachineID");
    writer.WriteValue(at.ActivityMachineID);


    writer.WritePropertyName("activityID");
    writer.WriteValue(at.ActivityID);


    writer.WritePropertyName("effort");
    writer.WriteValue(at.Effort);


    writer.WritePropertyName("machineID");
    writer.WriteValue(at.MachineID);


    writer.WritePropertyName("machineTypeID");
    writer.WriteValue(at.MachineTypeID);


    writer.WritePropertyName("name");
    writer.WriteValue(at.Name);

    writer.WritePropertyName("machineType");
    writer = WriteMachineTypeDto(writer, at.MachineType);

    writer.WriteEndObject();
    return writer;
    }

    public static JsonTextWriter WriteMachineTypeDto(JsonTextWriter writer, MachineTypeDto at) {
    writer.WriteStartObject();


    writer.WritePropertyName("machineTypeID");
    writer.WriteValue(at.MachineTypeID);


    writer.WritePropertyName("description");
    writer.WriteValue(at.Description);


    writer.WritePropertyName("isFlagged");
    writer.WriteValue(at.IsFlagged);

    writer.WriteEndObject();
    return writer;
    }

    public static JsonTextWriter WriteActivityPersonDto(JsonTextWriter writer, ActivityPersonDto ap) {
    writer.WriteStartObject();

    writer.WritePropertyName("activityPersonId");
    writer.WriteValue(ap.ActivityPersonId);

    writer.WritePropertyName("activityID");
    writer.WriteValue(ap.ActivityID);

    writer.WritePropertyName("resourceTypeID");
    writer.WriteValue(ap.ResourceTypeID);

    writer.WritePropertyName("personID");
    writer.WriteValue(ap.PersonID);

    writer.WritePropertyName("effort");
    writer.WriteValue(ap.Effort);

    writer.WritePropertyName("cost");
    writer.WriteValue(ap.Cost);

    writer.WritePropertyName("resourceType");
    writer = WriteResourceTypeDto(writer, ap.ResourceType);

    writer.WritePropertyName("person");
    writer = WritePersonDto(writer, ap.Person);
    //activitydto

    writer.WriteEndObject();
    return writer;
    }

    public static JsonTextWriter WritePersonDto(JsonTextWriter writer, PersonDto p)
    {
    writer.WriteStartObject();

    writer.WritePropertyName("personId");
    writer.WriteValue(p.PersonId);

    writer.WritePropertyName("firstName");
    writer.WriteValue(p.FirstName);

    writer.WritePropertyName("lastName");
    writer.WriteValue(p.LastName);

    writer.WriteEndObject();
    return writer;
    }

    public static JsonTextWriter WriteResourceTypeDto(JsonTextWriter writer, ResourceTypeDto rt) {
    writer.WriteStartObject();

    writer.WritePropertyName("resourceTypeId");
    writer.WriteValue(rt.ResourceTypeId);

    writer.WritePropertyName("description");
    writer.WriteValue(rt.Description);

    writer.WritePropertyName("isFlagged");
    writer.WriteValue(rt.IsFlagged);

    writer.WritePropertyName("resourceTypeCount");
    writer.WriteValue(rt.ResourceTypeCount);

    writer.WritePropertyName("replacedBy");
    writer.WriteValue(rt.ReplacedBy);

    writer.WriteEndObject();
    return writer;
    }

    public static JsonTextWriter WriteSubprojectPersonDto(JsonTextWriter writer, SubProjectPersonDto spp) {
    writer.WriteStartObject();

    writer.WritePropertyName("subProjectPersonId");
    writer.WriteValue(spp.SubProjectPersonId);

    writer.WritePropertyName("subProjectId");
    writer.WriteValue(spp.SubProjectId);

    writer.WritePropertyName("personId");
    writer.WriteValue(spp.PersonId);

    writer.WritePropertyName("subProjectPersonRoles");

    writer.WriteStartArray();

    foreach (var spr in spp.SubProjectPersonRoles)
    writer = WriteProjectPersonRoleDto(writer, spr);

    writer.WriteEndArray();


    writer.WriteEndObject();

    return writer;
    }

    private static JsonTextWriter WriteProjectPersonDto(JsonTextWriter writer, ProjectPersonDto person)
    {

    writer.WriteStartObject();

    writer.WritePropertyName("projectPersonId");
    writer.WriteValue(person.ProjectPersonId);

    writer.WritePropertyName("projectId");
    writer.WriteValue(person.ProjectId);

    writer.WritePropertyName("personId");
    writer.WriteValue(person.PersonId);

    writer.WritePropertyName("projectPersonRoles");

    writer.WriteStartArray();

    foreach (var ppr in person.ProjectPersonRoles)
    writer = WriteProjectPersonRoleDto(writer, ppr);

    writer.WriteEndArray();


    writer.WriteEndObject();
    return writer;
    }

    private static JsonTextWriter WriteProjectPersonRoleDto(JsonTextWriter writer, ProjectPersonRoleDto ppr)
    {
    writer.WriteStartObject();

    writer.WritePropertyName("projectPersonRoleID");
    writer.WriteValue(ppr.ProjectPersonRoleID);

    writer.WritePropertyName("projectPersonID");
    writer.WriteValue(ppr.ProjectPersonID);

    writer.WritePropertyName("roleTypeID");
    writer.WriteValue(ppr.RoleTypeID);

    writer.WritePropertyName("description");
    writer.WriteValue(ppr.Description);


    writer.WriteEndObject();
    return writer;
    }

    private static JsonTextWriter WriteOrganizationDto(JsonTextWriter writer, OrganizationDto org)
    {

    writer.WritePropertyName("organization");


    if (org == null)
    {
    writer.WriteNull();
    return writer;
    }

    writer.WriteStartObject();

    writer.WritePropertyName("OrganizationId");
    writer.WriteValue(org.OrganizationId);

    writer.WritePropertyName("OrganizationName");
    writer.WriteValue(org.OrganizationName);

    writer.WritePropertyName("employees");
    writer.WriteStartArray();
    writer.WriteEndArray();

    writer.WritePropertyName("machines");
    writer.WriteStartArray();
    writer.WriteEndArray();

    writer.WriteEndObject();

    return writer;
    }

    private static JsonTextWriter WriteProjectTypeDto(JsonTextWriter writer, ProjectTypeDto pType) {


    writer.WriteStartObject();
    writer.WritePropertyName("projectTypeId");
    writer.WriteValue(pType.ProjectTypeId);

    writer.WritePropertyName("description");
    writer.WriteValue(pType.Description);

    writer.WritePropertyName("rowId");
    writer.WriteValue(pType.RowId);

    writer.WritePropertyName("sortOrder");
    writer.WriteValue(pType.SortOrder);

    writer.WritePropertyName("projects");

    writer.WriteStartArray();
    foreach (var pjects in pType.Projects)
    {
    writer = WriteProjectDto(writer, pjects);
    }
    writer.WriteEndArray();

    writer.WriteEndObject();

    return writer;
    }


    Now this is what my current code looks like with the out of the box serializer that takes the same amount of time.



    Here is the controller method.



    public virtual BetterJsonResult GetGanttDataByUserOrganization()
    {

    var ganttChartDto = _ganttChartService.GetInitialGanttData();

    return BetterJson(ganttChartDto);
    }


    This is what the BetterJson which basically returns this



       ...
    protected virtual void SerializeData(HttpResponseBase response)
    {
    if (ErrorMessages.Any())
    {
    Data = new
    {
    ErrorMessage = string.Join("n", ErrorMessages),
    ErrorMessages = ErrorMessages.ToArray()
    };

    response.StatusCode = 400;
    }

    if (Data == null) return;

    response.Write(Data.ToJson());
    }

    public static string ToJson<T>(this T obj, bool includeNull = true)
    {
    var settings = new JsonSerializerSettings
    {
    ContractResolver = new CamelCasePropertyNamesContractResolver(),
    Converters = new JsonConverter { new StringEnumConverter() },
    NullValueHandling = includeNull ? NullValueHandling.Include : NullValueHandling.Ignore,
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    };

    return JsonConvert.SerializeObject(obj, settings);
    }








    share
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a large data set that I'm trying to serialize that is about 15MB. I wanted to increase performance so I tried to manually serialize it. I was expecting some performance boost but it actually takes the same amount of time as the built in serializer that uses reflection. Currently it takes around ~7 minutes to serialize the data. Maybe I'm doing something wrong, maybe someone can point it out and maybe there is something else I can do to for a performance boost while serializing the data.



      To begin with here is what my controller method looks like



          public void GetGanttDataByUserOrganization()
      {

      //takes around 5 seconds to get the DTO
      var ganttChartDto = _ganttChartService.GetInitialGanttData();

      //takes around 7 minutes to manually serialize the DTO it's 15MB
      var st = ganttChartDto.ToGanttJson();

      HttpContext.Response.WriteJson(st);

      }


      This part is long it's the ToGanttJson() extension method.



      public static string ToGanttJson(this IEnumerable<ProductLineDto> p)
      {
      StringWriter sw = new StringWriter();
      JsonTextWriter writer = new JsonTextWriter(sw);

      writer.WritePropertyName("projectType");
      writer.WriteStartArray();

      foreach (var pLine in p)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("name");
      writer.WriteValue(pLine.Name);

      writer.WritePropertyName("productLineId");
      writer.WriteValue(pLine.ProductLineId);

      writer.WritePropertyName("sortOrder");
      writer.WriteValue(pLine.SortOrder);

      writer.WritePropertyName("projectType");

      writer.WriteStartArray();
      foreach (var pType in pLine.ProjectType)
      {
      writer = WriteProjectTypeDto(writer, pType);
      }
      writer.WriteEndArray();

      writer.WriteEndObject();

      }

      writer.WriteEndArray();

      return sw.ToString();
      }

      private static JsonTextWriter WriteProjectDto(JsonTextWriter writer, ProjectDto pjects)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("projectId");
      writer.WriteValue(pjects.ProjectId);

      writer.WritePropertyName("projectName");
      writer.WriteValue(pjects.ProjectName);

      writer.WritePropertyName("parent");
      writer.WriteValue("");

      writer.WritePropertyName("startDate");
      writer.WriteValue(pjects.StartDate);

      writer.WritePropertyName("projectStatus");
      writer.WriteValue(pjects.ProjectStatus);

      writer.WritePropertyName("projectTypeRowId");
      writer.WriteValue(pjects.ProjectTypeRowId);

      writer.WritePropertyName("projectTypeDescription");
      writer.WriteValue(pjects.ProjectTypeDescription);

      writer.WritePropertyName("sortOrder");
      writer.WriteValue(pjects.SortOrder);

      writer.WritePropertyName("projectStatusId");
      writer.WriteValue(pjects.ProjectStatusId);

      writer.WritePropertyName("productLineId");
      writer.WriteValue(pjects.ProductLineId);

      writer.WritePropertyName("projectTypeId");
      writer.WriteValue(pjects.ProjectTypeId);

      writer.WritePropertyName("organizationId");
      writer.WriteValue(pjects.OrganizationId);

      writer.WritePropertyName("clientId");
      writer.WriteValue(pjects.ClientId);

      writer.WritePropertyName("program");
      writer.WriteValue(pjects.Program);

      writer.WritePropertyName("productLineName");
      writer.WriteValue(pjects.ProductLineName);

      writer.WritePropertyName("isShotgun");
      writer.WriteValue(pjects.IsShotgun);


      writer.WritePropertyName("shotgunLink");
      writer.WriteValue(pjects.ShotgunLink);

      writer.WritePropertyName("budgetCode");
      writer.WriteValue(pjects.BudgetCode);

      writer.WritePropertyName("modelType");
      writer.WriteValue(pjects.ModelType);

      writer.WritePropertyName("modelYear");
      writer.WriteValue(pjects.ModelYear);

      writer.WritePropertyName("flow");
      writer.WriteValue(pjects.Flow);

      writer.WritePropertyName("description");
      writer.WriteValue(pjects.Description);

      writer = WriteOrganizationDto(writer, pjects.Organization);

      writer.WritePropertyName("projectPersons");

      writer.WriteStartArray();
      foreach (var person in pjects.ProjectPersons)
      {
      writer = WriteProjectPersonDto(writer, person);
      }
      writer.WriteEndArray();

      writer.WritePropertyName("children");

      writer.WriteStartArray();
      foreach (var sp in pjects.Children)
      {
      writer = WriteSubProjectDto(writer, sp);
      }
      writer.WriteEndArray();

      writer.WriteEndObject();

      return writer;

      }

      private static JsonTextWriter WriteSubProjectDto(JsonTextWriter writer, SubProjectDto sp) {

      writer.WriteStartObject();

      writer.WritePropertyName("projectId");
      writer.WriteValue(sp.ProjectId);

      writer.WritePropertyName("subProjectId");
      writer.WriteValue(sp.SubProjectId);

      writer.WritePropertyName("projectName");
      writer.WriteValue(sp.ProjectName);


      writer.WritePropertyName("parentId");
      writer.WriteValue(sp.ParentId);


      writer.WritePropertyName("subProjectTypeId");
      writer.WriteValue(sp.SubProjectTypeId);

      writer.WritePropertyName("organizationId");
      writer.WriteValue(sp.OrganizationId);

      writer.WritePropertyName("sortOrder");
      writer.WriteValue(sp.SortOrder);

      writer.WritePropertyName("clientId");
      writer.WriteValue(sp.ClientId);

      writer.WritePropertyName("projectClientId");
      writer.WriteValue(sp.ProjectClientId);

      writer.WritePropertyName("subProjectParentClientId");
      writer.WriteValue(sp.SubProjectParentClientId);


      writer.WritePropertyName("parentSubProjectId");
      writer.WriteValue(sp.ParentSubProjectId);

      writer.WritePropertyName("isShotgun");
      writer.WriteValue(sp.IsShotgun);

      writer = WriteOrganizationDto(writer, sp.Organization);

      writer.WritePropertyName("children");

      writer.WriteStartArray();
      foreach (var childSp in sp.Children)
      {
      writer = WriteSubProjectDto(writer, childSp);
      }

      writer.WriteEndArray();

      writer.WritePropertyName("subProjectPersons");

      writer.WriteStartArray();

      foreach (var spp in sp.SubProjectPersons)
      writer = WriteSubprojectPersonDto(writer, spp);

      writer.WriteEndArray();

      //activities
      writer.WritePropertyName("activities");

      writer.WriteStartArray();

      foreach (var act in sp.Activities)
      writer = WriteActivityDto(writer, act);

      writer.WriteEndArray();
      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WriteActivityDto(JsonTextWriter writer, ActivityDto act)
      {
      writer.WriteStartObject();


      writer.WritePropertyName("acivityId");
      writer.WriteValue(act.ActivityId);

      writer.WritePropertyName("description");
      writer.WriteValue(act.Description);

      writer.WritePropertyName("activityName");
      writer.WriteValue(act.Name);

      writer.WritePropertyName("activityDateRange");
      writer.WriteValue("");

      writer.WritePropertyName("startDate");
      writer.WriteValue(act.StartDate);

      writer.WritePropertyName("endDate");
      writer.WriteValue(act.EndDate);

      writer.WritePropertyName("duration");
      writer.WriteValue(act.Duration);

      writer.WritePropertyName("pctComplete");
      writer.WriteValue(act.PctComplete);

      writer.WritePropertyName("name");
      writer.WriteValue(act.Name);

      writer.WritePropertyName("projectId");
      writer.WriteValue(act.ProjectId);

      writer.WritePropertyName("subProjectId");
      writer.WriteValue(act.SubProjectId);

      writer.WritePropertyName("organizationId");
      writer.WriteValue(act.OrganizationId);

      writer.WritePropertyName("activityTypeId");
      writer.WriteValue(act.ActivityTypeId);

      writer.WritePropertyName("sortOrder");
      writer.WriteValue(act.SortOrder);

      writer.WritePropertyName("parentClientId");
      writer.WriteValue("");

      writer.WritePropertyName("clientId");
      writer.WriteValue("");

      writer.WritePropertyName("projectName");
      writer.WriteValue(act.ProjectName);

      writer.WritePropertyName("plannedStart");
      writer.WriteValue(act.PlannedStart);

      writer.WritePropertyName("plannedEnd");
      writer.WriteValue(act.PlannedEnd);

      writer.WritePropertyName("laserProcessId");
      writer.WriteValue(act.LaserProcessId);

      writer.WritePropertyName("laserThicknessId");
      writer.WriteValue(act.LaserThicknessId);

      writer.WritePropertyName("materialId");
      writer.WriteValue(act.MaterialId);

      writer.WritePropertyName("millingMinimumRadiusId");
      writer.WriteValue(act.MillingMinimumRadiusId);

      writer.WritePropertyName("millingScallopHeightId");
      writer.WriteValue(act.MillingScallopHeightId);

      writer.WritePropertyName("modelCategoryId");
      writer.WriteValue(act.ModelCategoryId);

      writer.WritePropertyName("modelTypeId");
      writer.WriteValue(act.ModelTypeId);

      writer.WritePropertyName("rpfdmNozzleId");
      writer.WriteValue(act.RpfdmNozzleId);

      writer.WritePropertyName("rpfillTypeId");
      writer.WriteValue(act.RpfillTypeId);

      writer.WritePropertyName("varianceId");
      writer.WriteValue(act.VarianceId);

      writer.WritePropertyName("quantityId");
      writer.WriteValue(act.QuantityId);

      writer.WritePropertyName("specNotes");
      writer.WriteValue(act.SpecNotes);

      writer.WritePropertyName("parentActivityID");
      writer.WriteValue(act.ParentActivityID);

      writer.WritePropertyName("rpsparseId");
      writer.WriteValue(act.RpsparseId);

      writer = WriteOrganizationDto(writer, act.Organization);

      writer.WritePropertyName("children");

      writer.WriteStartArray();
      foreach(var child in act.Children)
      writer = WriteActivityDto(writer, child);
      writer.WriteEndArray();

      writer.WritePropertyName("activityPersons");

      writer.WriteStartArray();
      foreach (var actPerson in act.ActivityPersons)
      writer = WriteActivityPersonDto(writer, actPerson);
      writer.WriteEndArray();

      writer.WritePropertyName("activityMachines");

      writer.WriteStartArray();
      foreach (var am in act.ActivityMachines)
      writer = WriteActivitMachineDto(writer, am);
      writer.WriteEndArray();

      writer.WritePropertyName("customForm");

      writer.WriteStartArray();
      foreach (var cf in act.CustomForm)
      writer = WirteActivityTypeCustomFormDto(writer, cf);
      writer.WriteEndArray();

      //activitytype


      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WriteActivityTypeDto(JsonTextWriter writer, ActivityTypeDto at)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("activityTypeId");
      writer.WriteValue(at.ActivityTypeId);

      writer.WritePropertyName("isMileStone");
      writer.WriteValue(at.IsMileStone);

      writer.WritePropertyName("description");
      writer.WriteValue(at.Description);

      writer.WritePropertyName("isFlagged");
      writer.WriteValue(at.IsFlagged);

      writer.WritePropertyName("showInProject");
      writer.WriteValue(at.ShowInProject);

      writer.WritePropertyName("showInParent");
      writer.WriteValue(at.ShowInParent);

      writer.WritePropertyName("color");
      writer.WriteValue(at.Color);

      writer.WritePropertyName("pattern");
      writer.WriteValue(at.Pattern);

      writer.WritePropertyName("milestoneImage");
      writer.WriteValue(at.MilestoneImage);

      writer.WritePropertyName("activityCount");
      writer.WriteValue(at.ActivityCount);

      writer.WritePropertyName("replacedBy");
      writer.WriteValue(at.ReplacedBy);

      writer = WirteResourceTypeControlDto(writer, at.ResourceType);

      writer.WritePropertyName("modelTypes");

      writer.WriteStartArray();
      foreach (var mt in at.ModelTypes)
      writer = WirteGanttControlDto(writer, mt);
      writer.WriteEndArray();

      writer.WritePropertyName("activitySpecs");

      writer.WriteStartArray();
      foreach (var spec in at.ActivitySpecs)
      writer = WirteActivityQuantityDto(writer, spec);
      writer.WriteEndArray();

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WirteActivityQuantityDto(JsonTextWriter writer, ActivityQuantityDto spec)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("activityQuantityID");
      writer.WriteValue(spec.ActivityQuantityID);

      writer.WritePropertyName("description");
      writer.WriteValue(spec.Description);

      writer.WritePropertyName("activityTypeID");
      writer.WriteValue(spec.ActivityTypeID);

      writer.WritePropertyName("sortOrder");
      writer.WriteValue(spec.SortOrder);

      //list

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WirteActivityTypeQuantityListDto(JsonTextWriter writer, ActivityTypeQuantityListDto value)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("activityTypeQuantityListID");
      writer.WriteValue(value.ActivityTypeQuantityListID);

      writer.WritePropertyName("description");
      writer.WriteValue(value.Description);

      writer.WritePropertyName("activityTypeQuantityID");
      writer.WriteValue(value.ActivityTypeQuantityID);

      writer.WritePropertyName("sortOrder");
      writer.WriteValue(value.SortOrder);

      writer.WritePropertyName("isDefaultValue");
      writer.WriteValue(value.IsDefaultValue);

      writer.WriteEndObject();

      return writer;
      }

      public static JsonTextWriter WirteResourceTypeControlDto(JsonTextWriter writer, ResourceTypeControlDto at)
      {
      writer.WritePropertyName("resourceType");

      writer.WriteStartObject();

      writer.WritePropertyName("id");
      writer.WriteValue(at.Id);

      writer.WritePropertyName("name");
      writer.WriteValue(at.Name);

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WirteGanttControlDto(JsonTextWriter writer, GanttControlDto at)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("id");
      writer.WriteValue(at.ID);

      writer.WritePropertyName("name");
      writer.WriteValue(at.Name);

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WirteActivityTypeCustomFormDto(JsonTextWriter writer, ActivityTypeCustomFormDto at)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("activityQuantityID");
      writer.WriteValue(at.activityQuantityID);

      writer.WritePropertyName("activityTypeQuantityListID");
      writer.WriteValue(at.ActivityTypeQuantityListID);

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WriteActivitMachineDto(JsonTextWriter writer, ActivityMachinesDto at)
      {

      writer.WriteStartObject();

      writer.WritePropertyName("activityMachineID");
      writer.WriteValue(at.ActivityMachineID);


      writer.WritePropertyName("activityID");
      writer.WriteValue(at.ActivityID);


      writer.WritePropertyName("effort");
      writer.WriteValue(at.Effort);


      writer.WritePropertyName("machineID");
      writer.WriteValue(at.MachineID);


      writer.WritePropertyName("machineTypeID");
      writer.WriteValue(at.MachineTypeID);


      writer.WritePropertyName("name");
      writer.WriteValue(at.Name);

      writer.WritePropertyName("machineType");
      writer = WriteMachineTypeDto(writer, at.MachineType);

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WriteMachineTypeDto(JsonTextWriter writer, MachineTypeDto at) {
      writer.WriteStartObject();


      writer.WritePropertyName("machineTypeID");
      writer.WriteValue(at.MachineTypeID);


      writer.WritePropertyName("description");
      writer.WriteValue(at.Description);


      writer.WritePropertyName("isFlagged");
      writer.WriteValue(at.IsFlagged);

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WriteActivityPersonDto(JsonTextWriter writer, ActivityPersonDto ap) {
      writer.WriteStartObject();

      writer.WritePropertyName("activityPersonId");
      writer.WriteValue(ap.ActivityPersonId);

      writer.WritePropertyName("activityID");
      writer.WriteValue(ap.ActivityID);

      writer.WritePropertyName("resourceTypeID");
      writer.WriteValue(ap.ResourceTypeID);

      writer.WritePropertyName("personID");
      writer.WriteValue(ap.PersonID);

      writer.WritePropertyName("effort");
      writer.WriteValue(ap.Effort);

      writer.WritePropertyName("cost");
      writer.WriteValue(ap.Cost);

      writer.WritePropertyName("resourceType");
      writer = WriteResourceTypeDto(writer, ap.ResourceType);

      writer.WritePropertyName("person");
      writer = WritePersonDto(writer, ap.Person);
      //activitydto

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WritePersonDto(JsonTextWriter writer, PersonDto p)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("personId");
      writer.WriteValue(p.PersonId);

      writer.WritePropertyName("firstName");
      writer.WriteValue(p.FirstName);

      writer.WritePropertyName("lastName");
      writer.WriteValue(p.LastName);

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WriteResourceTypeDto(JsonTextWriter writer, ResourceTypeDto rt) {
      writer.WriteStartObject();

      writer.WritePropertyName("resourceTypeId");
      writer.WriteValue(rt.ResourceTypeId);

      writer.WritePropertyName("description");
      writer.WriteValue(rt.Description);

      writer.WritePropertyName("isFlagged");
      writer.WriteValue(rt.IsFlagged);

      writer.WritePropertyName("resourceTypeCount");
      writer.WriteValue(rt.ResourceTypeCount);

      writer.WritePropertyName("replacedBy");
      writer.WriteValue(rt.ReplacedBy);

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WriteSubprojectPersonDto(JsonTextWriter writer, SubProjectPersonDto spp) {
      writer.WriteStartObject();

      writer.WritePropertyName("subProjectPersonId");
      writer.WriteValue(spp.SubProjectPersonId);

      writer.WritePropertyName("subProjectId");
      writer.WriteValue(spp.SubProjectId);

      writer.WritePropertyName("personId");
      writer.WriteValue(spp.PersonId);

      writer.WritePropertyName("subProjectPersonRoles");

      writer.WriteStartArray();

      foreach (var spr in spp.SubProjectPersonRoles)
      writer = WriteProjectPersonRoleDto(writer, spr);

      writer.WriteEndArray();


      writer.WriteEndObject();

      return writer;
      }

      private static JsonTextWriter WriteProjectPersonDto(JsonTextWriter writer, ProjectPersonDto person)
      {

      writer.WriteStartObject();

      writer.WritePropertyName("projectPersonId");
      writer.WriteValue(person.ProjectPersonId);

      writer.WritePropertyName("projectId");
      writer.WriteValue(person.ProjectId);

      writer.WritePropertyName("personId");
      writer.WriteValue(person.PersonId);

      writer.WritePropertyName("projectPersonRoles");

      writer.WriteStartArray();

      foreach (var ppr in person.ProjectPersonRoles)
      writer = WriteProjectPersonRoleDto(writer, ppr);

      writer.WriteEndArray();


      writer.WriteEndObject();
      return writer;
      }

      private static JsonTextWriter WriteProjectPersonRoleDto(JsonTextWriter writer, ProjectPersonRoleDto ppr)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("projectPersonRoleID");
      writer.WriteValue(ppr.ProjectPersonRoleID);

      writer.WritePropertyName("projectPersonID");
      writer.WriteValue(ppr.ProjectPersonID);

      writer.WritePropertyName("roleTypeID");
      writer.WriteValue(ppr.RoleTypeID);

      writer.WritePropertyName("description");
      writer.WriteValue(ppr.Description);


      writer.WriteEndObject();
      return writer;
      }

      private static JsonTextWriter WriteOrganizationDto(JsonTextWriter writer, OrganizationDto org)
      {

      writer.WritePropertyName("organization");


      if (org == null)
      {
      writer.WriteNull();
      return writer;
      }

      writer.WriteStartObject();

      writer.WritePropertyName("OrganizationId");
      writer.WriteValue(org.OrganizationId);

      writer.WritePropertyName("OrganizationName");
      writer.WriteValue(org.OrganizationName);

      writer.WritePropertyName("employees");
      writer.WriteStartArray();
      writer.WriteEndArray();

      writer.WritePropertyName("machines");
      writer.WriteStartArray();
      writer.WriteEndArray();

      writer.WriteEndObject();

      return writer;
      }

      private static JsonTextWriter WriteProjectTypeDto(JsonTextWriter writer, ProjectTypeDto pType) {


      writer.WriteStartObject();
      writer.WritePropertyName("projectTypeId");
      writer.WriteValue(pType.ProjectTypeId);

      writer.WritePropertyName("description");
      writer.WriteValue(pType.Description);

      writer.WritePropertyName("rowId");
      writer.WriteValue(pType.RowId);

      writer.WritePropertyName("sortOrder");
      writer.WriteValue(pType.SortOrder);

      writer.WritePropertyName("projects");

      writer.WriteStartArray();
      foreach (var pjects in pType.Projects)
      {
      writer = WriteProjectDto(writer, pjects);
      }
      writer.WriteEndArray();

      writer.WriteEndObject();

      return writer;
      }


      Now this is what my current code looks like with the out of the box serializer that takes the same amount of time.



      Here is the controller method.



      public virtual BetterJsonResult GetGanttDataByUserOrganization()
      {

      var ganttChartDto = _ganttChartService.GetInitialGanttData();

      return BetterJson(ganttChartDto);
      }


      This is what the BetterJson which basically returns this



         ...
      protected virtual void SerializeData(HttpResponseBase response)
      {
      if (ErrorMessages.Any())
      {
      Data = new
      {
      ErrorMessage = string.Join("n", ErrorMessages),
      ErrorMessages = ErrorMessages.ToArray()
      };

      response.StatusCode = 400;
      }

      if (Data == null) return;

      response.Write(Data.ToJson());
      }

      public static string ToJson<T>(this T obj, bool includeNull = true)
      {
      var settings = new JsonSerializerSettings
      {
      ContractResolver = new CamelCasePropertyNamesContractResolver(),
      Converters = new JsonConverter { new StringEnumConverter() },
      NullValueHandling = includeNull ? NullValueHandling.Include : NullValueHandling.Ignore,
      ReferenceLoopHandling = ReferenceLoopHandling.Ignore
      };

      return JsonConvert.SerializeObject(obj, settings);
      }








      share













      I have a large data set that I'm trying to serialize that is about 15MB. I wanted to increase performance so I tried to manually serialize it. I was expecting some performance boost but it actually takes the same amount of time as the built in serializer that uses reflection. Currently it takes around ~7 minutes to serialize the data. Maybe I'm doing something wrong, maybe someone can point it out and maybe there is something else I can do to for a performance boost while serializing the data.



      To begin with here is what my controller method looks like



          public void GetGanttDataByUserOrganization()
      {

      //takes around 5 seconds to get the DTO
      var ganttChartDto = _ganttChartService.GetInitialGanttData();

      //takes around 7 minutes to manually serialize the DTO it's 15MB
      var st = ganttChartDto.ToGanttJson();

      HttpContext.Response.WriteJson(st);

      }


      This part is long it's the ToGanttJson() extension method.



      public static string ToGanttJson(this IEnumerable<ProductLineDto> p)
      {
      StringWriter sw = new StringWriter();
      JsonTextWriter writer = new JsonTextWriter(sw);

      writer.WritePropertyName("projectType");
      writer.WriteStartArray();

      foreach (var pLine in p)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("name");
      writer.WriteValue(pLine.Name);

      writer.WritePropertyName("productLineId");
      writer.WriteValue(pLine.ProductLineId);

      writer.WritePropertyName("sortOrder");
      writer.WriteValue(pLine.SortOrder);

      writer.WritePropertyName("projectType");

      writer.WriteStartArray();
      foreach (var pType in pLine.ProjectType)
      {
      writer = WriteProjectTypeDto(writer, pType);
      }
      writer.WriteEndArray();

      writer.WriteEndObject();

      }

      writer.WriteEndArray();

      return sw.ToString();
      }

      private static JsonTextWriter WriteProjectDto(JsonTextWriter writer, ProjectDto pjects)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("projectId");
      writer.WriteValue(pjects.ProjectId);

      writer.WritePropertyName("projectName");
      writer.WriteValue(pjects.ProjectName);

      writer.WritePropertyName("parent");
      writer.WriteValue("");

      writer.WritePropertyName("startDate");
      writer.WriteValue(pjects.StartDate);

      writer.WritePropertyName("projectStatus");
      writer.WriteValue(pjects.ProjectStatus);

      writer.WritePropertyName("projectTypeRowId");
      writer.WriteValue(pjects.ProjectTypeRowId);

      writer.WritePropertyName("projectTypeDescription");
      writer.WriteValue(pjects.ProjectTypeDescription);

      writer.WritePropertyName("sortOrder");
      writer.WriteValue(pjects.SortOrder);

      writer.WritePropertyName("projectStatusId");
      writer.WriteValue(pjects.ProjectStatusId);

      writer.WritePropertyName("productLineId");
      writer.WriteValue(pjects.ProductLineId);

      writer.WritePropertyName("projectTypeId");
      writer.WriteValue(pjects.ProjectTypeId);

      writer.WritePropertyName("organizationId");
      writer.WriteValue(pjects.OrganizationId);

      writer.WritePropertyName("clientId");
      writer.WriteValue(pjects.ClientId);

      writer.WritePropertyName("program");
      writer.WriteValue(pjects.Program);

      writer.WritePropertyName("productLineName");
      writer.WriteValue(pjects.ProductLineName);

      writer.WritePropertyName("isShotgun");
      writer.WriteValue(pjects.IsShotgun);


      writer.WritePropertyName("shotgunLink");
      writer.WriteValue(pjects.ShotgunLink);

      writer.WritePropertyName("budgetCode");
      writer.WriteValue(pjects.BudgetCode);

      writer.WritePropertyName("modelType");
      writer.WriteValue(pjects.ModelType);

      writer.WritePropertyName("modelYear");
      writer.WriteValue(pjects.ModelYear);

      writer.WritePropertyName("flow");
      writer.WriteValue(pjects.Flow);

      writer.WritePropertyName("description");
      writer.WriteValue(pjects.Description);

      writer = WriteOrganizationDto(writer, pjects.Organization);

      writer.WritePropertyName("projectPersons");

      writer.WriteStartArray();
      foreach (var person in pjects.ProjectPersons)
      {
      writer = WriteProjectPersonDto(writer, person);
      }
      writer.WriteEndArray();

      writer.WritePropertyName("children");

      writer.WriteStartArray();
      foreach (var sp in pjects.Children)
      {
      writer = WriteSubProjectDto(writer, sp);
      }
      writer.WriteEndArray();

      writer.WriteEndObject();

      return writer;

      }

      private static JsonTextWriter WriteSubProjectDto(JsonTextWriter writer, SubProjectDto sp) {

      writer.WriteStartObject();

      writer.WritePropertyName("projectId");
      writer.WriteValue(sp.ProjectId);

      writer.WritePropertyName("subProjectId");
      writer.WriteValue(sp.SubProjectId);

      writer.WritePropertyName("projectName");
      writer.WriteValue(sp.ProjectName);


      writer.WritePropertyName("parentId");
      writer.WriteValue(sp.ParentId);


      writer.WritePropertyName("subProjectTypeId");
      writer.WriteValue(sp.SubProjectTypeId);

      writer.WritePropertyName("organizationId");
      writer.WriteValue(sp.OrganizationId);

      writer.WritePropertyName("sortOrder");
      writer.WriteValue(sp.SortOrder);

      writer.WritePropertyName("clientId");
      writer.WriteValue(sp.ClientId);

      writer.WritePropertyName("projectClientId");
      writer.WriteValue(sp.ProjectClientId);

      writer.WritePropertyName("subProjectParentClientId");
      writer.WriteValue(sp.SubProjectParentClientId);


      writer.WritePropertyName("parentSubProjectId");
      writer.WriteValue(sp.ParentSubProjectId);

      writer.WritePropertyName("isShotgun");
      writer.WriteValue(sp.IsShotgun);

      writer = WriteOrganizationDto(writer, sp.Organization);

      writer.WritePropertyName("children");

      writer.WriteStartArray();
      foreach (var childSp in sp.Children)
      {
      writer = WriteSubProjectDto(writer, childSp);
      }

      writer.WriteEndArray();

      writer.WritePropertyName("subProjectPersons");

      writer.WriteStartArray();

      foreach (var spp in sp.SubProjectPersons)
      writer = WriteSubprojectPersonDto(writer, spp);

      writer.WriteEndArray();

      //activities
      writer.WritePropertyName("activities");

      writer.WriteStartArray();

      foreach (var act in sp.Activities)
      writer = WriteActivityDto(writer, act);

      writer.WriteEndArray();
      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WriteActivityDto(JsonTextWriter writer, ActivityDto act)
      {
      writer.WriteStartObject();


      writer.WritePropertyName("acivityId");
      writer.WriteValue(act.ActivityId);

      writer.WritePropertyName("description");
      writer.WriteValue(act.Description);

      writer.WritePropertyName("activityName");
      writer.WriteValue(act.Name);

      writer.WritePropertyName("activityDateRange");
      writer.WriteValue("");

      writer.WritePropertyName("startDate");
      writer.WriteValue(act.StartDate);

      writer.WritePropertyName("endDate");
      writer.WriteValue(act.EndDate);

      writer.WritePropertyName("duration");
      writer.WriteValue(act.Duration);

      writer.WritePropertyName("pctComplete");
      writer.WriteValue(act.PctComplete);

      writer.WritePropertyName("name");
      writer.WriteValue(act.Name);

      writer.WritePropertyName("projectId");
      writer.WriteValue(act.ProjectId);

      writer.WritePropertyName("subProjectId");
      writer.WriteValue(act.SubProjectId);

      writer.WritePropertyName("organizationId");
      writer.WriteValue(act.OrganizationId);

      writer.WritePropertyName("activityTypeId");
      writer.WriteValue(act.ActivityTypeId);

      writer.WritePropertyName("sortOrder");
      writer.WriteValue(act.SortOrder);

      writer.WritePropertyName("parentClientId");
      writer.WriteValue("");

      writer.WritePropertyName("clientId");
      writer.WriteValue("");

      writer.WritePropertyName("projectName");
      writer.WriteValue(act.ProjectName);

      writer.WritePropertyName("plannedStart");
      writer.WriteValue(act.PlannedStart);

      writer.WritePropertyName("plannedEnd");
      writer.WriteValue(act.PlannedEnd);

      writer.WritePropertyName("laserProcessId");
      writer.WriteValue(act.LaserProcessId);

      writer.WritePropertyName("laserThicknessId");
      writer.WriteValue(act.LaserThicknessId);

      writer.WritePropertyName("materialId");
      writer.WriteValue(act.MaterialId);

      writer.WritePropertyName("millingMinimumRadiusId");
      writer.WriteValue(act.MillingMinimumRadiusId);

      writer.WritePropertyName("millingScallopHeightId");
      writer.WriteValue(act.MillingScallopHeightId);

      writer.WritePropertyName("modelCategoryId");
      writer.WriteValue(act.ModelCategoryId);

      writer.WritePropertyName("modelTypeId");
      writer.WriteValue(act.ModelTypeId);

      writer.WritePropertyName("rpfdmNozzleId");
      writer.WriteValue(act.RpfdmNozzleId);

      writer.WritePropertyName("rpfillTypeId");
      writer.WriteValue(act.RpfillTypeId);

      writer.WritePropertyName("varianceId");
      writer.WriteValue(act.VarianceId);

      writer.WritePropertyName("quantityId");
      writer.WriteValue(act.QuantityId);

      writer.WritePropertyName("specNotes");
      writer.WriteValue(act.SpecNotes);

      writer.WritePropertyName("parentActivityID");
      writer.WriteValue(act.ParentActivityID);

      writer.WritePropertyName("rpsparseId");
      writer.WriteValue(act.RpsparseId);

      writer = WriteOrganizationDto(writer, act.Organization);

      writer.WritePropertyName("children");

      writer.WriteStartArray();
      foreach(var child in act.Children)
      writer = WriteActivityDto(writer, child);
      writer.WriteEndArray();

      writer.WritePropertyName("activityPersons");

      writer.WriteStartArray();
      foreach (var actPerson in act.ActivityPersons)
      writer = WriteActivityPersonDto(writer, actPerson);
      writer.WriteEndArray();

      writer.WritePropertyName("activityMachines");

      writer.WriteStartArray();
      foreach (var am in act.ActivityMachines)
      writer = WriteActivitMachineDto(writer, am);
      writer.WriteEndArray();

      writer.WritePropertyName("customForm");

      writer.WriteStartArray();
      foreach (var cf in act.CustomForm)
      writer = WirteActivityTypeCustomFormDto(writer, cf);
      writer.WriteEndArray();

      //activitytype


      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WriteActivityTypeDto(JsonTextWriter writer, ActivityTypeDto at)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("activityTypeId");
      writer.WriteValue(at.ActivityTypeId);

      writer.WritePropertyName("isMileStone");
      writer.WriteValue(at.IsMileStone);

      writer.WritePropertyName("description");
      writer.WriteValue(at.Description);

      writer.WritePropertyName("isFlagged");
      writer.WriteValue(at.IsFlagged);

      writer.WritePropertyName("showInProject");
      writer.WriteValue(at.ShowInProject);

      writer.WritePropertyName("showInParent");
      writer.WriteValue(at.ShowInParent);

      writer.WritePropertyName("color");
      writer.WriteValue(at.Color);

      writer.WritePropertyName("pattern");
      writer.WriteValue(at.Pattern);

      writer.WritePropertyName("milestoneImage");
      writer.WriteValue(at.MilestoneImage);

      writer.WritePropertyName("activityCount");
      writer.WriteValue(at.ActivityCount);

      writer.WritePropertyName("replacedBy");
      writer.WriteValue(at.ReplacedBy);

      writer = WirteResourceTypeControlDto(writer, at.ResourceType);

      writer.WritePropertyName("modelTypes");

      writer.WriteStartArray();
      foreach (var mt in at.ModelTypes)
      writer = WirteGanttControlDto(writer, mt);
      writer.WriteEndArray();

      writer.WritePropertyName("activitySpecs");

      writer.WriteStartArray();
      foreach (var spec in at.ActivitySpecs)
      writer = WirteActivityQuantityDto(writer, spec);
      writer.WriteEndArray();

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WirteActivityQuantityDto(JsonTextWriter writer, ActivityQuantityDto spec)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("activityQuantityID");
      writer.WriteValue(spec.ActivityQuantityID);

      writer.WritePropertyName("description");
      writer.WriteValue(spec.Description);

      writer.WritePropertyName("activityTypeID");
      writer.WriteValue(spec.ActivityTypeID);

      writer.WritePropertyName("sortOrder");
      writer.WriteValue(spec.SortOrder);

      //list

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WirteActivityTypeQuantityListDto(JsonTextWriter writer, ActivityTypeQuantityListDto value)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("activityTypeQuantityListID");
      writer.WriteValue(value.ActivityTypeQuantityListID);

      writer.WritePropertyName("description");
      writer.WriteValue(value.Description);

      writer.WritePropertyName("activityTypeQuantityID");
      writer.WriteValue(value.ActivityTypeQuantityID);

      writer.WritePropertyName("sortOrder");
      writer.WriteValue(value.SortOrder);

      writer.WritePropertyName("isDefaultValue");
      writer.WriteValue(value.IsDefaultValue);

      writer.WriteEndObject();

      return writer;
      }

      public static JsonTextWriter WirteResourceTypeControlDto(JsonTextWriter writer, ResourceTypeControlDto at)
      {
      writer.WritePropertyName("resourceType");

      writer.WriteStartObject();

      writer.WritePropertyName("id");
      writer.WriteValue(at.Id);

      writer.WritePropertyName("name");
      writer.WriteValue(at.Name);

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WirteGanttControlDto(JsonTextWriter writer, GanttControlDto at)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("id");
      writer.WriteValue(at.ID);

      writer.WritePropertyName("name");
      writer.WriteValue(at.Name);

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WirteActivityTypeCustomFormDto(JsonTextWriter writer, ActivityTypeCustomFormDto at)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("activityQuantityID");
      writer.WriteValue(at.activityQuantityID);

      writer.WritePropertyName("activityTypeQuantityListID");
      writer.WriteValue(at.ActivityTypeQuantityListID);

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WriteActivitMachineDto(JsonTextWriter writer, ActivityMachinesDto at)
      {

      writer.WriteStartObject();

      writer.WritePropertyName("activityMachineID");
      writer.WriteValue(at.ActivityMachineID);


      writer.WritePropertyName("activityID");
      writer.WriteValue(at.ActivityID);


      writer.WritePropertyName("effort");
      writer.WriteValue(at.Effort);


      writer.WritePropertyName("machineID");
      writer.WriteValue(at.MachineID);


      writer.WritePropertyName("machineTypeID");
      writer.WriteValue(at.MachineTypeID);


      writer.WritePropertyName("name");
      writer.WriteValue(at.Name);

      writer.WritePropertyName("machineType");
      writer = WriteMachineTypeDto(writer, at.MachineType);

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WriteMachineTypeDto(JsonTextWriter writer, MachineTypeDto at) {
      writer.WriteStartObject();


      writer.WritePropertyName("machineTypeID");
      writer.WriteValue(at.MachineTypeID);


      writer.WritePropertyName("description");
      writer.WriteValue(at.Description);


      writer.WritePropertyName("isFlagged");
      writer.WriteValue(at.IsFlagged);

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WriteActivityPersonDto(JsonTextWriter writer, ActivityPersonDto ap) {
      writer.WriteStartObject();

      writer.WritePropertyName("activityPersonId");
      writer.WriteValue(ap.ActivityPersonId);

      writer.WritePropertyName("activityID");
      writer.WriteValue(ap.ActivityID);

      writer.WritePropertyName("resourceTypeID");
      writer.WriteValue(ap.ResourceTypeID);

      writer.WritePropertyName("personID");
      writer.WriteValue(ap.PersonID);

      writer.WritePropertyName("effort");
      writer.WriteValue(ap.Effort);

      writer.WritePropertyName("cost");
      writer.WriteValue(ap.Cost);

      writer.WritePropertyName("resourceType");
      writer = WriteResourceTypeDto(writer, ap.ResourceType);

      writer.WritePropertyName("person");
      writer = WritePersonDto(writer, ap.Person);
      //activitydto

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WritePersonDto(JsonTextWriter writer, PersonDto p)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("personId");
      writer.WriteValue(p.PersonId);

      writer.WritePropertyName("firstName");
      writer.WriteValue(p.FirstName);

      writer.WritePropertyName("lastName");
      writer.WriteValue(p.LastName);

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WriteResourceTypeDto(JsonTextWriter writer, ResourceTypeDto rt) {
      writer.WriteStartObject();

      writer.WritePropertyName("resourceTypeId");
      writer.WriteValue(rt.ResourceTypeId);

      writer.WritePropertyName("description");
      writer.WriteValue(rt.Description);

      writer.WritePropertyName("isFlagged");
      writer.WriteValue(rt.IsFlagged);

      writer.WritePropertyName("resourceTypeCount");
      writer.WriteValue(rt.ResourceTypeCount);

      writer.WritePropertyName("replacedBy");
      writer.WriteValue(rt.ReplacedBy);

      writer.WriteEndObject();
      return writer;
      }

      public static JsonTextWriter WriteSubprojectPersonDto(JsonTextWriter writer, SubProjectPersonDto spp) {
      writer.WriteStartObject();

      writer.WritePropertyName("subProjectPersonId");
      writer.WriteValue(spp.SubProjectPersonId);

      writer.WritePropertyName("subProjectId");
      writer.WriteValue(spp.SubProjectId);

      writer.WritePropertyName("personId");
      writer.WriteValue(spp.PersonId);

      writer.WritePropertyName("subProjectPersonRoles");

      writer.WriteStartArray();

      foreach (var spr in spp.SubProjectPersonRoles)
      writer = WriteProjectPersonRoleDto(writer, spr);

      writer.WriteEndArray();


      writer.WriteEndObject();

      return writer;
      }

      private static JsonTextWriter WriteProjectPersonDto(JsonTextWriter writer, ProjectPersonDto person)
      {

      writer.WriteStartObject();

      writer.WritePropertyName("projectPersonId");
      writer.WriteValue(person.ProjectPersonId);

      writer.WritePropertyName("projectId");
      writer.WriteValue(person.ProjectId);

      writer.WritePropertyName("personId");
      writer.WriteValue(person.PersonId);

      writer.WritePropertyName("projectPersonRoles");

      writer.WriteStartArray();

      foreach (var ppr in person.ProjectPersonRoles)
      writer = WriteProjectPersonRoleDto(writer, ppr);

      writer.WriteEndArray();


      writer.WriteEndObject();
      return writer;
      }

      private static JsonTextWriter WriteProjectPersonRoleDto(JsonTextWriter writer, ProjectPersonRoleDto ppr)
      {
      writer.WriteStartObject();

      writer.WritePropertyName("projectPersonRoleID");
      writer.WriteValue(ppr.ProjectPersonRoleID);

      writer.WritePropertyName("projectPersonID");
      writer.WriteValue(ppr.ProjectPersonID);

      writer.WritePropertyName("roleTypeID");
      writer.WriteValue(ppr.RoleTypeID);

      writer.WritePropertyName("description");
      writer.WriteValue(ppr.Description);


      writer.WriteEndObject();
      return writer;
      }

      private static JsonTextWriter WriteOrganizationDto(JsonTextWriter writer, OrganizationDto org)
      {

      writer.WritePropertyName("organization");


      if (org == null)
      {
      writer.WriteNull();
      return writer;
      }

      writer.WriteStartObject();

      writer.WritePropertyName("OrganizationId");
      writer.WriteValue(org.OrganizationId);

      writer.WritePropertyName("OrganizationName");
      writer.WriteValue(org.OrganizationName);

      writer.WritePropertyName("employees");
      writer.WriteStartArray();
      writer.WriteEndArray();

      writer.WritePropertyName("machines");
      writer.WriteStartArray();
      writer.WriteEndArray();

      writer.WriteEndObject();

      return writer;
      }

      private static JsonTextWriter WriteProjectTypeDto(JsonTextWriter writer, ProjectTypeDto pType) {


      writer.WriteStartObject();
      writer.WritePropertyName("projectTypeId");
      writer.WriteValue(pType.ProjectTypeId);

      writer.WritePropertyName("description");
      writer.WriteValue(pType.Description);

      writer.WritePropertyName("rowId");
      writer.WriteValue(pType.RowId);

      writer.WritePropertyName("sortOrder");
      writer.WriteValue(pType.SortOrder);

      writer.WritePropertyName("projects");

      writer.WriteStartArray();
      foreach (var pjects in pType.Projects)
      {
      writer = WriteProjectDto(writer, pjects);
      }
      writer.WriteEndArray();

      writer.WriteEndObject();

      return writer;
      }


      Now this is what my current code looks like with the out of the box serializer that takes the same amount of time.



      Here is the controller method.



      public virtual BetterJsonResult GetGanttDataByUserOrganization()
      {

      var ganttChartDto = _ganttChartService.GetInitialGanttData();

      return BetterJson(ganttChartDto);
      }


      This is what the BetterJson which basically returns this



         ...
      protected virtual void SerializeData(HttpResponseBase response)
      {
      if (ErrorMessages.Any())
      {
      Data = new
      {
      ErrorMessage = string.Join("n", ErrorMessages),
      ErrorMessages = ErrorMessages.ToArray()
      };

      response.StatusCode = 400;
      }

      if (Data == null) return;

      response.Write(Data.ToJson());
      }

      public static string ToJson<T>(this T obj, bool includeNull = true)
      {
      var settings = new JsonSerializerSettings
      {
      ContractResolver = new CamelCasePropertyNamesContractResolver(),
      Converters = new JsonConverter { new StringEnumConverter() },
      NullValueHandling = includeNull ? NullValueHandling.Include : NullValueHandling.Ignore,
      ReferenceLoopHandling = ReferenceLoopHandling.Ignore
      };

      return JsonConvert.SerializeObject(obj, settings);
      }






      c# json serialization json.net





      share












      share










      share



      share










      asked 4 mins ago









      Ortho Home Defense

      1898




      1898



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208802%2fjson-net-manual-serialization-performance%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208802%2fjson-net-manual-serialization-performance%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'