c# – Serialization of a large list in JSON.Net

Question:

Guys, I have a problem to get the serialization of an object (a list of a class with approximately 5000 items).

I'm using JSON.NET to generate the Json string but it's getting the following problem, in the middle of it is a text like this:

,{"State":0,"DataAlteracao":null,"Id":0,"IdDadosRastreamento":0,"CodigoPeriferico":"0","ValorPeriferico":"0"}
,{"State":0,"DataAlteracao":null,"Id":0,"IdDadosRastreamento":0,"CodigoPeriferico":"0","ValorPeriferico":"0"}
,{"State":0,"DataAl:..."0","ValorPeriferico":"1840"}
,{"State":0,"DataAlteracao":null,"Id":0,"IdDadosRastreamento":0,"CodigoPeriferico":"0","ValorPeriferico":"1380"}
,{"State":0,"DataAlteracao":null,"Id":0,"IdDadosRastreamento":0,"CodigoPeriferico":"0","ValorPeriferico":"62"}

Note in bold that he cut the tag name as well as put "…" and then continued creating the file normally.

does anyone know what this problem may be and how can i solve it? the code to perform the serialization is as follows:

string jsonReq = Newtonsoft.Json.JsonConvert.SerializeObject(request);

Where request is the list with 5000 positions.

Answer:

As mentioned, I believe that if you are returning a very large list, the ideal is to add the following properties to your server:

  1. Receive variables for paging: requested page number, number of records per page (the client can specify this, but on the server you verify that both the page number starts with 1 and does not pass the number of existing records and also that the number of records per page is not greater than, for example, 100 records, or something that allows the response not to be too large);

  2. Stipulate a default ordering even if the client does not have the authority to ask for this information. In general, it depends on the context. But assuming it's an incremental list, it might be nice to sort by descending date. If the customer asks for the first page with 100 records, it takes the first 100 most recent. Then if it verifies that he didn't have any of this information, he uses it, and then goes to the next page. Until you get to a record he has locally. Then he doesn't need to ask for the previous pages. In general this approach makes more sense than starting from the earliest to the latest date. If the return list is never sorted, your client will never be able to perform this operation.

  3. In general it's nice to add this information with the response Headers of your request. So your client can see, for example, that it asked for the first 100 records but the server returned that the response's X-Record-Count says there are 1 million records. If the idea is to always page until the end, you'll have an idea of ​​how much remains to be done. Using Headers also makes it easier by not having to encapsulate the result in another object with this information.

Scroll to Top
AllEscort