MoaidHathot / Dumpify

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

Property support for Dumping Newtonsoft.Json and System.Text.Json objects #14

Open MoaidHathot opened 1 year ago

MoaidHathot commented 1 year ago

Description

@Hejle has pointed out that our current support for dumping Newtonsoft.Json and System.Text.Json objects are lacking.

Examples/reproduction:

image

Suggested solution:

We can either use DumpConfig.Default.AddCustomTypeHandler(typeof(JValue), (obj, _, _, _) => obj.ToString()); or we can create a custom type renderer for Json objects.

I think we should compile a list of objects of both Newtonsoft.Json and System.Text.Json that we would have to custom render and decide how to render each one of them. We may start with a simple solution, which is using AddCustomTypeHandler until we implement a proper custom renderer.

Hejle commented 1 year ago

I can try out a few things on how to render json. One thing I have already noticed, is that it is considerably harder to work with System.Text.Json, as its JValue equiviliant "JsonValue" is created as a JsonValueTrimmable which I can't really get the typeof.

But I can look around a bit, and see if I can figure it out.

Hejle commented 1 year ago

I have tried a few things with Generating JSON, and so far the easiest would be to consider JValue (NewtonSoft) and JsonValue (System.Text.Json) as KnownSingleValues, and then generating them using the SingleValueDescriptor. Alternatively, I can add a separate Descripter + Render-method for the Json-Values, which would hopefully make it easier to customize in the future.

For the following JSON-input, we will then get the follwowing output:

[
  {
    "Sample":"Basic Sample", 
    "Id": 1,
    "Text":"StringValue"
  },
  {
    "Sample": "Object Sample",
    "Id": 2,
    "Nested Object": {
        "Bool": false,
        "Text": "StringValue"
    }        
  },
  {
    "Sample":"Array Sample",
    "Id": 3,
    "Array": [1,2,3]
  }
]

billede

MoaidHathot commented 1 year ago

Great work @Hejle, thanks! I want to keep the descriptors as general as possible, so creating a new type of descriptor only for Json-types should be our last resort. A better option IMO is to create a custom renderer and register all of the Json-related types in our custom type handlers. Currently I don't like that we need to do it in CustomValueGenerator. I would like to first work on a way to register the custom-types directly via the custom renderers, so we only have to specify types that need custom rendering in one place, which is the renderer itself. Once that is done, it will be easier to write a single ICustomTypeRenderer that support the json-related types.

latop2604 commented 10 months ago

Why not render them as json instead of table? Spectre.Console already support it. Doc here

image