2

UPDATE: After some research, i've found out that that's an issue with Visual Studio 2015 and Text Visualizer, which you can see here.

To reproduce it, Open QuickWatch (Shift+F9) and in the search box, put new string(' ', 32769). After that click at the magnifier glass and you should see a "..." in the middle of the string...

So, changing my question, is there a way to fix this and make it not truncate, so i can copy without workarounds?



I have this piece of code:

        JArray bundlesJson = (JArray)JObject.Parse(System.IO.File.ReadAllText(Server.MapPath("~/itens.json")))["bundles"]; // file "itens.json" can be found at http://pastebin.com/7K15yAVd
        System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex(@"(?<sNome>.*?)\/(?<sClasse>.*?)\/(?<sDescricao>.*?)(?:\/(?<sExtras>.*?)\n|\n|$)");
        var text = System.IO.File.ReadAllText(Server.MapPath("~/TextFile1.txt")); // TextFile1.txt is too big to put here, so i've uploaded it here: http://pastebin.com/AtxbYPXc

        var matches = rgx.Matches(text);
        var lstItens = new List<Item>();
        var oJson = new JArray();

        foreach (System.Text.RegularExpressions.Match match in matches)
        {
            var item = new Item()
            {
                sClasse = match.Groups["sClasse"].Value.Trim(),
                sDescricao = match.Groups["sDescricao"].Value.Trim(),
                sNome = match.Groups["sNome"].Value.Trim(),
                sExtras = match.Groups["sExtras"].Value.Trim(),
            };
            item.PreencherListaBundles(bundlesJson.ToString());

            lstItens.Add(item);
        }
        var result = JsonConvert.SerializeObject(lstItens, Formatting.Indented);
        var backResult = JsonConvert.DeserializeObject<List<Item>>(result);

The lstItens list has all items correctly (501 items), but the result string returns only 48 objects when searched for "Nome":, which is a mandatory field. Why is this happening?

To exemplify the error, look for the item lstItens[166], in the result var, if you search for "Nome": "Fiddlehead Fern" you can see that the item doesn't exists...

What is weird is that backResult.Count will show 501 results, and appears to have every item, but a simple search in the json generated at the result var using a mandatory field "Nome" will result in 48 results, as showed in the image :48 results


Item.cs:


public class Item
{
    [JsonProperty(PropertyName = "Nome")]
    public string sNome { get; set; }

    [JsonProperty(PropertyName = "Classe")]
    public string sClasse { get; set; }

    [JsonProperty(PropertyName = "Descricao")]
    public string sDescricao { get; set; }

    [JsonProperty(PropertyName = "Extras")]
    public string sExtras { get; set; }

    [JsonProperty(PropertyName = "Bundles")]
    public List<Bundle> lstBundles { get; set; }

    public void PreencherListaBundles(string jsonBundles)
    {
        List<Bundle> lstBundle = JsonConvert.DeserializeObject<List<Bundle>>(jsonBundles.ToString());

        this.lstBundles = new List<Bundle>();

        lstBundle.ForEach(x =>
        {
            if (!lstBundles.Select(y => y.sNome).Contains(x.sNome) && x.lstItens.Select(y => y.sNome).Contains(sNome))
            {
                lstBundles.Add(new Bundle() { sLocal = x.sLocal, sNome = x.sNome, sRecompensa = x.sRecompensa, lstItens = x.lstItens });
            }
        });
    }
}

Bundle.cs


public class Bundle
{
    [JsonProperty(PropertyName = "bundle")]
    public string sNome { get; set; }

    [JsonProperty(PropertyName = "location")]
    public string sLocal { get; set; }

    [JsonProperty(PropertyName = "reward")]
    public string sRecompensa { get; set; }

    [JsonProperty(PropertyName = "items")]
    public List<BundleItem> lstItens { get; set; }

    public class BundleItem
    {
        [JsonProperty(PropertyName = "name")]
        public string sNome { get; set; }

        [JsonProperty(PropertyName = "description")]
        public string sDescricao { get; set; }

        [JsonProperty(PropertyName = "quantity")]
        public int nQuantidade { get; set; }

        [JsonProperty(PropertyName = "quality")]
        public string sQualidade { get; set; }
    }
}

EDIT: Looks like that bug is not happening on some machines, like you can see with the Gerard Sexton's answer, but when i run the same code he ran i still get the 48 results. some more details can be found in this discussion: https://chat.stackoverflow.com/rooms/106307/discussion-between-gerard-sexton-and-gabriel-duarte

Community
  • 1
  • 1
Gabriel Duarte
  • 974
  • 1
  • 13
  • 28
  • I can't reproduce your problem. If I do `JToken.Parse(oJson.ToString()).Count()` I get 501 as expected. And `oJson[166]` can be found easily in the JSON string, just do `Debug.WriteLine(oJson.ToString().Substring(oJson.ToString().IndexOf("Fiddlehead Fern") - 18, 150))` and you will see it. – dbc Mar 14 '16 at 17:21
  • 1
    Can you create an [complete example](http://stackoverflow.com/help/mcve) that shows how you determine that `oJson[166]` is missing from the output JSON string `sJson`? – dbc Mar 14 '16 at 17:22
  • Are you using a custom resolver? – StingyJack Mar 14 '16 at 17:23
  • @StingyJack I don't know what that is, so i suppose i'm not using that hahah – Gabriel Duarte Mar 15 '16 at 02:38
  • @dbc I've added more details... When i run `Debug.WriteLine(oJson.ToString().Substring(oJson.ToString().IndexOf("Fiddlehead Fern") - 18, 150))` it indeed returns me a part of the object, but it is not returned when i use the `oJson.ToString()` and search for `"Nome": "Fiddlehead Fern"`. I've added in the question the complete json that i recieve from `var result = JsonConvert.SerializeObject(lstItens, Formatting.Indented);`, which i beleve is the same as `oJson.ToString()`, and i've added some classes that i use to serialize the json... – Gabriel Duarte Mar 15 '16 at 03:04
  • @GabrielDuarte How are you getting your JSON result into Notepad++? Copy and paste? Are you sure you are copying the entire result before searching? Have you tried writing the JSON result to a new file and then opening that file in Notepad++? When I try your code, I get all 501 results and I am able to find `"Nome": "Fiddlehead Fern"` as expected (line 2555). – Brian Rogers Mar 15 '16 at 04:19
  • @BrianRogers Yes, i'm editing the var using visual studio's text visualizer (http://imgur.com/IAV5DZK), ctrl+a to select all and ctrl+c to copy, then i paste it to notepad++ and search for "Nome": "Fiddlehead Fern", which returns me no results... i've put an edit in the end of the question – Gabriel Duarte Mar 15 '16 at 04:23
  • @GabrielDuarte - an example of what I mean is here - http://stackoverflow.com/a/33290710/16391. You can have the JSON serialization ignore or rename properties or objects. – StingyJack Mar 15 '16 at 15:03
  • @StingyJack if it's just that, then I'm not using a Custom Resolver. The only thing i'm using is the code that is in the question – Gabriel Duarte Mar 15 '16 at 15:50

2 Answers2

0

Try this:

var result = JsonConvert.SerializeObject(lstItens, Formatting.Indented);
//Testing deserialization
var backResult = JsonConvert.DeserializeObject<List<Item>>(result);

The result variable is a string which contains the properly formatted string.

Tamas Ionut
  • 4,240
  • 5
  • 36
  • 59
  • I've already tried this yesterday, no luck, still the same results (or even less results, can't remember now)... – Gabriel Duarte Mar 14 '16 at 16:47
  • Nope, works fine for me. Added deserialization and the backResult has 501 items. backResult [166] has all the fields as expected and is the same as oJson[166]. – Tamas Ionut Mar 14 '16 at 17:16
  • in the deserialization, it indeed returns 501 results, but i double checked now with the serialization line that you passed, it returns me just 48 results. I checked it via the Count option on Notepad++, searching for "Nome": (A property that is mandatory in all root items). If you search for "Nome": "Fiddlehead Fern" in the json (name of the oJson[166]) it doesn't find anything... I'll update my question to include the "Item" object and related objects... By the way, this is the json that i got: http://pastebin.com/nQRXaPpr – Gabriel Duarte Mar 15 '16 at 02:34
  • Nope, searched by "Nome" in the "result" string and it gets back 501 finds. Also the "backResult" variable gives 501 items. – Tamas Ionut Mar 15 '16 at 10:28
  • PS: I'm using a ConsoleApplication to test out. So, no truncation is being performed on my end. – Tamas Ionut Mar 15 '16 at 10:30
  • I tried creating a ConsoleApplication just to test this out, and it still gives me less results. This bug seems to happen just in some machines... I'm using the same code and in the same way and still get the 48 results on the result var... backresult returns me all 501 items thought (i have not checked yet if it has the same item names, descriptions, etc). It's weird, i'm starting to think that this is a json.net bug... – Gabriel Duarte Mar 15 '16 at 12:46
0

I was not able to reproduce the bug using VS2015, JSON.NET 8.0.3. I have included my debugger output.

Using your code and files, unchanged, I got 501 results and sJson contains Fiddlehead Fern and the visualizer shows item 166. Deserialization also works correctly.

Please check character encoding and these kind of things. Your system locale might cause something to act differently. Just a thought.

enter image description here enter image description here

Gerard Sexton
  • 3,114
  • 27
  • 36
  • Can you post the json string you got, so i can compare with the one i've got? is it the same as this one: http://pastebin.com/nQRXaPpr ? – Gabriel Duarte Mar 15 '16 at 03:31
  • See that in the backResult var i got the 501 items, but when i search for "Nome": in the result string if returns only 48 items, i've updated my question, see the screenshot of the results count... By the way, i'm using VS2015 and JSON.NET 8.0.3 too. – Gabriel Duarte Mar 15 '16 at 03:37
  • Here is my output from sJson http://pastebin.com/WNMYFUtV. It is quite different from your screenshot. This http://pastebin.com/nQRXaPpr is very different. – Gerard Sexton Mar 15 '16 at 03:39
  • this one you posted seems to lack the Bundles property that i added later in the question, did you tried it with the updated code? – Gabriel Duarte Mar 15 '16 at 03:42
  • I found your issue with your output. Line 579 of your output is `...mines or Mushroom Cave.",`. It breaks the JSON formatting somewhere. Here is my output to compare http://pastebin.com/zLmtuJfd – Gerard Sexton Mar 15 '16 at 03:57
  • Did you fix something in the code to get that json? it is weird that i get a different json... I'm using Brazilian locale, but i'm not sure if that could influentiate at the result... – Gabriel Duarte Mar 15 '16 at 04:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106307/discussion-between-gerard-sexton-and-gabriel-duarte). – Gerard Sexton Mar 15 '16 at 04:01