String truncate when performing append

Asked

Viewed 86 times

3

The code below serializes a JSON list and gives a append the variable result. However, from a certain amount of records (350), the text contained in result has a modified part for "..." . I performed the tests directly by selecting the entire list and also through the StringBuilder but getting the same result, only with the "..." in different places.

string result = string.Empty;
foreach(item in collection)
{
    string json = JsonConvert.Serialize(item);
    result += json; 
}

Excerpt from the broken json:

{"cnae1":null,"cnae2":null,"cnae3":null... A)"

As it should be:

{"cnae1":null,"cnae2":null,"cnae3":null,"part":null,"number":null,"type":null}

  • 1

    Where are you viewing this output?

  • 1

    First, you must use StringBuilder, nor try to do as you are doing. Nothing indicates that what you are describing is even possible. Or you need to put more code that indicates this, or what happens depends on the data and not the code.

  • Stringtextvisualizer, by clicking on the magnifying glass next to the variable.

  • Serialize Collection at once...?

  • Yes, the result is the same

1 answer

4

There’s no "nothing wrong"...

I ran a test using the same codebase as yours:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace TesteConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            var lista = new List<Teste>();
            for (int i = 0; i < 300; i++)
                lista.Add(new Teste{id = i, nome = "Teste "+ i});

            var json = JsonConvert.SerializeObject(lista);

            Console.WriteLine(json);


            string result = string.Empty;
            foreach (var item in lista)
            {
                result += JsonConvert.SerializeObject(item);
            }


            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}


public class Teste
{
    public int id { get; set; }
    public string nome { get; set; }
}

Result of var json = JsonConvert.SerializeObject(lista):

[{"id":0,"nome":"Teste 0"},{"id":1,"nome":"Teste 1"},{"id":2,"nome":"Teste 2"},{"id":3,"nome":"Teste 3"},{"id":4,"nome":"Teste 4"}
[...]
{"id":297,"nome":"Teste 297"},{"id":298,"nome":"Teste 298"},{"id":299,"nome":"Teste 299"}]

Result of string result:

{"id":0,"nome":"Teste 0"}{"id":1,"nome":"Teste 1"}{"id":2,"nome":"Teste 2"}{"id":3,"nome":"Teste 3"}{"id":4,"nome":"Teste 4"}
    [...]
    {"id":297,"nome":"Teste 297"}{"id":298,"nome":"Teste 298"}{"id":299,"nome":"Teste 299"}

I believe the problem lies only in the visualization and familiarization of the IDE... If you in debug mode "hovers the mouse" on top of the variable you want to see, an ellipsis will appear in case what you are inspecting has a larger content "than fits" in the inspection preview:

"pré visualização"

If you click "on the magnifying glass":

Visualização completa

EDIT:

Note that in the case of how you are doing, the result is a json invalid, contrary to what is done with the serialization of the entire list: var json = JsonConvert.SerializeObject(lista);

Json INVALID:

{"id":0,"nome":"Teste 0"},{"id":1,"nome":"Teste 1"}

{"id":0,"nome":"Teste 0"}{"id":1,"nome":"Teste 1"}

Json VALID:

[{"id":0,"nome":"Teste 0"},{"id":1,"nome":"Teste 1"}]
  • doing some tests here found the problem. In fact, in Text Visualizer (by clicking on the magnifying glass), the text appears truncated in some parts (with the three dots). This same text does not appear truncated if I copy the direct value of the variable (right-click > copy value). I believe it must be some limitation of Textvisualizer itself.

  • It could be.... which version of visual studio? And watch out not to create an invalid JSON... I hope I’ve helped :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.