0

I'm writing my first project trying to use System.Text.Json in a .net core app. I'm getting a jsonl file with a particular structure, and my requirements are in effect to UNPIVOT/flatten an array of child objects in one object into a stream of transformed objects.

It was going fine until I put a breakpoint on the routine doing the UNPIVOT and the debugger itself started blowing up with an access violation in JsonElement.DebuggerDisplay.get. Interestingly, a) it floats around which row it blows up on and b) it seems to be somewhat dependent on how long I wait before clicking Continue. In other words, if I wait a couple of seconds, it seems to work; if I click right away it blows up faster.

Just wondering if anyone else had run into something like this. And whether I should just switch back to NewtonSoft to avoid the headache.

Here's what my code looks like:

public static IEnumerable<MyResult> ConvertJson(JsonElement input)
{
    JsonElement transformArray, base_url;
    if (!input.TryGetProperty("child_objects", out transformArray) || transformArray.ValueKind != JsonValueKind.Array)
        yield break;
    if (!input.TryGetProperty("base_url", out base_url) || base_url.ValueKind != JsonValueKind.String)
        yield break;

    int i = 0;
    foreach(var o in transformArray.EnumerateArray())
    {
        // Break point on line below.  Click Continue too quickly, and I get DebuggerDisplay.get access violation
        var result = new MyResult();
        result.BaseURL = base_url.ToString();
        result.PageID = i; i++;
        JsonElement prop;
        if (o.TryGetProperty("prop1", out prop) && prop.ValueKind == JsonValueKind.String) result.Prop1 = prop.ToString();
        if (o.TryGetProperty("text", out prop) && prop.ValueKind == JsonValueKind.String) result.Text = prop.ToString();
        if (o.TryGetProperty("language", out prop) && prop.ValueKind == JsonValueKind.String) result.Language = prop.ToString();

        yield return result;
    }
}

and it's called like this:

string l = JsonlStream.ReadLine();
var json = JsonSerializer.Deserialize<JsonElement>(l);
foreach (var i in ConvertJson(json))
{
    ...
}
user1664043
  • 695
  • 5
  • 14
  • Any chance you previously disposed of the `JsonDocument` that hosts the incoming `JsonElement`? – dbc Jul 08 '21 at 03:31
  • Thanks for responding... I should have posted more of the calling sequence. The short answer is I don't think so. I get the JsonElement with a call to Deserialize() and then I loop over the result of ConvertJson(). I never finish the loop, and as I say the crash seems somewhat tied to how long I pause at the breakpoint. – user1664043 Jul 08 '21 at 13:19
  • Is there any problem when not running in the debugger? Can you [edit] the question to share sample JSON (or [NDJSON](http://ndjson.org/) perhaps?) that reproduces the problem? – dbc Jul 08 '21 at 17:49
  • I took out all the break points, and I can run it in the debugger without problem so long as I don't stop there and click continue too quickly. The json objects have some very large strings in it, so it would be awkward to try and paste it into the ticket. I also switched over to a Newtonsoft implementation - no debugger blow-ups and the same speed. – user1664043 Jul 09 '21 at 14:54

0 Answers0