String size exceeds the value set in the maxJsonLength property

1

I'm trying to generate a Json file from a SELECT of the SQL SERVER 2016 table. There are about 4713 rows, this is in excess of the value set in the maxJsonLength property. Can I exceed this set value?

Web.config

  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="500000000"/>
      </webServices>
    </scripting>
  </system.web.extensions>  

Class Model:

public class ProcessoMobile
        {
            public string Natureza { get; set; }
            public string Vara { get; set; }
            public string Agencia { get; set; }
            public string Comarca { get; set; }
            public Decimal Valor { get; set; }
            public string GCPJ { get; set; }
            public string ParteContraria { get; set; }
            public string AdvogadoContraria { get; set; }
            public string Telefone { get; set; }
            public DateTime DataDistribuicao { get; set; }
            public string Carteira { get; set; }
            public string Contrato { get; set; }
            public string CidadeAgencia { get; set; }
            public string ProcessoID { get; set; }
            public bool PesquisaBens { get; set; }
            public bool Ativo { get; set; }
            public bool Arquivado { get; set; }
            public bool Irrecuperavel { get; set; }
            public bool Encerrado { get; set; }
            public string CarteiraAtual { get; set; }
            public string ContratoAtual { get; set; }
            public string CpfCnpj { get; set; }
            public string Conta { get; set; }
            public string NomeAdvogado { get; set; }
        }

Function:

public static void CriaJson()
{
    List<ProcessoMobile> processList = new List<ProcessoMobile>();

    using (SqlConnection con = new SqlConnection(MyConnectionString))
    {
        using (SqlCommand command = new SqlCommand("select_Processo_Mobile", con))
        {
            command.CommandType = CommandType.StoredProcedure;

            con.Open();

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                ProcessoMobile p = new ProcessoMobile()
                {

                    Natureza = reader["Natureza"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Natureza"]),
                    Vara = reader["Vara"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Vara"]),
                    Agencia = reader["Agencia"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Agencia"]),
                    Comarca = reader["Comarca"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Comarca"]),
                    Valor = reader["Valor"] ==
                    DBNull.Value ? decimal.MinValue :
                    Convert.ToDecimal(reader["Valor"]),
                    GCPJ = reader["GCPJ"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["GCPJ"]),
                    ParteContraria = reader["ParteContraria"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["ParteContraria"]),
                    AdvogadoContraria = reader["AdvogadoContraria"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["AdvogadoContraria"]),
                    Telefone = reader["Telefone"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Telefone"]),
                    DataDistribuicao = reader["DataDistribuicao"] ==
                    DBNull.Value ? DateTime.MinValue :
                    Convert.ToDateTime(reader["DataDistribuicao"]),
                    Carteira = reader["Carteira"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Carteira"]),
                    Contrato = reader["Contrato"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Contrato"]),
                    CidadeAgencia = reader["CidadeAgencia"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["CidadeAgencia"]),
                    ProcessoID = reader["ProcessoID"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["ProcessoID"]),
                    PesquisaBens = reader["PesquisaBens"] ==
                    DBNull.Value ? false :
                    Convert.ToBoolean(reader["PesquisaBens"]),
                    Ativo = reader["Ativo"] ==
                    DBNull.Value ? false :
                    Convert.ToBoolean(reader["Ativo"]),
                    Arquivado = reader["Arquivado"] ==
                    DBNull.Value ? false :
                    Convert.ToBoolean(reader["Arquivado"]),
                    Irrecuperavel = reader["Irrecuperavel"] ==
                    DBNull.Value ? false :
                    Convert.ToBoolean(reader["Irrecuperavel"]),
                    Encerrado = reader["Encerrado"] ==
                    DBNull.Value ? false :
                    Convert.ToBoolean(reader["Encerrado"]),
                    CarteiraAtual = reader["CarteiraAtual"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["CarteiraAtual"]),
                    ContratoAtual = reader["ContratoAtual"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["ContratoAtual"]),
                    CpfCnpj = reader["CpfCnpj"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["CpfCnpj"]),
                    Conta = reader["Conta"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Conta"]),
                    NomeAdvogado = reader["NomeAdvogado"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["NomeAdvogado"]),

                };
                processList.Add(p);
            }
            reader.Close();
            reader.Dispose();
            con.Close();
            con.Dispose();
            command.Dispose();

            var jsonSerialiser = new System.Web.Script.Serialization.JavaScriptSerializer();
            string jsonString = jsonSerialiser.Serialize(processList);
            System.IO.File.WriteAllText(@"D:\processList.json", jsonString);
        }
    }
}

    
asked by anonymous 13.04.2018 / 00:01

1 answer

2

Normally, both for Serialization and Deserialize I use the Newtonsoft framework of namespace Newtonsoft.Json which, most of the time, is already incorporated into the project. However, if it does not come, you can install it by Nuget

PM> Install-Package Newtonsoft.Json 

At last, let's go to the code.

I do not know the size of the data that is coming from your database, but I have created a class equivalent to yours and assigns them the SerializeObject test with Newtonsoft.Json .

No major issues, I was able to do Serialization of 500000 lines.

follow how to use it

public static void CriaJson()
{
    List<ProcessoMobile> processList = new List<ProcessoMobile>();

    //Código para obter os dados

    string jsonString =  JsonConvert.SerializeObject(processList);
    System.IO.File.WriteAllText(@"D:\processList.json", jsonString);
}
    
13.04.2018 / 02:57