2

I keep getting this error when trying to create json. This is the full error

Error during serialization or deserialization using the JSON 
JavaScriptSerializer. The length of the string exceeds
the value set on the maxJsonLength property.

My code is as follows

public ActionResult ChangeRequests_Read([DataSourceRequest] DataSourceRequest request)
    {
        var data = GetRequests();
        var serializer = new JavaScriptSerializer();
        var result = new ContentResult();
        serializer.MaxJsonLength = Int32.MaxValue; // Whatever max length you want here
        result.Content = serializer.Serialize(data.ToDataSourceResult(request));
        result.ContentType = "application/json";
        return result;

        //return Json(GetRequests().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
    }

and this pulls from here

public static IEnumerable<ChangeRequestsVM> GetRequests()
    {
        var model = CompanyContextFactory.GetContextPerRequest();
        var chrwVM =  model.ChangeRequestsHDRs.Where(ch=> ch.CompanyID == GlobalVariables.CompanyID).Select(ch=> new ChangeRequestsVM
        {
            RequestID = ch.RequestID, 
            CompanyID = ch.CompanyID,
            ClientID = ch.ClientID,
            EmployeeID = (string.IsNullOrEmpty(ch.EmployeeID)) ? "NA" : ch.EmployeeID,
            AssignmentType = ch.AssignmentType,
            Key1 = ch.Key1,
            Key2 = ch.Key2,
            LogonID = ch.LogonID,
            ProcessDate = ch.ProcessDate,
            ProcessTime = ch.ProcessTime,
            ProcessUserID = ch.ProcessUserID,
            RequestDate = ch.RequestDate,
            RequestExport = ch.RequestExport,
            RequestNote = ch.RequestNote,
            RequestOrigin = Convert.ToChar(ch.RequestOrigin),
            RequestProcess = ch.RequestProcess,
            RequestStatus = ch.RequestStatus,
            RequestTime = ch.RequestTime,
            RequestType = Convert.ToChar(ch.RequestType),
            ResponseNote = ch.ResponseNote,
            TableName = ch.TableName,
            TransferDate = ch.TransferDate,
            TransferTime = ch.TransferTime,
            TransferUserID = ch.TransferUserID,
            dispOrigin = (Convert.ToChar(ch.RequestOrigin) == ChangeRequestOrigin.System) ? "System" : (Convert.ToChar(ch.RequestOrigin) == ChangeRequestOrigin.Client) ? "Client" : "Employee",
            dispRequestType = (Convert.ToChar(ch.RequestType) == ChangeRequestType.Insert) ? "Insert" : (Convert.ToChar(ch.RequestType) == ChangeRequestType.Alter) ? "Change" : "Delete",
            dispStatus = FieldTranslation.GetEnumDescription(typeof(enChangeRequestStatus), ch.RequestStatus ?? 0)
        }).OrderByDescending(ch=> ch.RequestID);
        return chrwVM;
    }

Why is my json exceeding length? I've made it so the method only pulls one item from the database and still get the error.

2
  • Maybe one of your fields has too much data in it, like ch.RequestNote or ch.ResponseNote. Commented Nov 8, 2016 at 19:27
  • If using JavaScriptSerializer is not a hard requirement for you, then I'd suggest you switching over to Json.NET. Commented Nov 8, 2016 at 19:34

1 Answer 1

1

You need to increase the size of the MaxJsonLength property:

https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.maxjsonlength.aspx

This is done in the configuration file. See link above for details.

Update:

Since you are using MVC, you can use the following app setting:

<appSettings>
    <add key="aspnet:MaxJsonDeserializerMembers" value="123456789" />
...
</appSettings>

However, be aware this is a potential security risk. Read more here:

https://msdn.microsoft.com/en-us/library/hh975440(v=vs.120).aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.