MoaidHathot / Dumpify

Adding `.Dump()` extension methods to Console Applications, similar to LinqPad's.
MIT License
959 stars 40 forks source link

Problem rendering Dictionary classs which implements an Enumerator which is not castable to IEnumerable #12

Closed Hejle closed 1 year ago

Hejle commented 1 year ago

I was trying to dump a NewtonSoft Json object, when I ran into the following error: Unable to cast object of type 'd__64' to type 'System.Collections.IEnumerable'.

I tried the same with System.Text.Json and got a familiar exception: Unable to cast object of type 'd__18[System.Text.Json.Nodes.JsonNode]' to type 'System.Collections.IEnumerable'.

[Failed to Render Newtonsoft.Json.Linq.JObject - {
  "key1": "value1",
  "key2": "value2"
}]. Unable to cast object of type '<GetEnumerator>d__64' to type 'System.Collections.IEnumerable'.
[Failed to Render System.Text.Json.Nodes.JsonObject - {
  "key1": "value1",
  "key2": "value2"
}]. Unable to cast object of type '<GetEnumerator>d__18[System.Text.Json.Nodes.JsonNode]' to type 'System.Collections.IEnumerable'.

I have created a fix for this which I will create a pullrequest for in a bit.


Note: Even with the fix, I wouldn't say that Json is Supported. The output after my Pull Request would look like this: billede

To get NewtonSofts output to look nice, it is possible to add a CustomRenderer like this, which will format the output nicely:

DumpConfig.Default.AddCustomTypeHandler(typeof(JValue), (obj, _, _, _) => obj.ToString());

       JObject
┌────────┬──────────┐
│ Key    │ Value    │
├────────┼──────────┤
│ "key1" │ "value1" │
│ "key2" │ "value2" │
└────────┴──────────┘
MoaidHathot commented 1 year ago

Good catch @Hejle! Thank you for reporting this and creating the PR, it is awesome, I merged it into the Main branch :)

Regarding better renderers for Json support- I agree, this is something that we should take care of. If you want, you can be the champion of this feature. Although first we should outline which changes we want to do, which types we want to custom-render and how much we want to "hide", since every time we add custom renderers for types we hide some of the members of that types, and which otherwise will be shown, we should find the balance.

Let's continue the discussion in #14. I'm closing this issue, since the reported bug has been resolved :)