Dumpify
Dumpify copied to clipboard
Property support for Dumping Newtonsoft.Json and System.Text.Json objects
Description
@Hejle has pointed out that our current support for dumping Newtonsoft.Json
and System.Text.Json
objects are lacking.
Examples/reproduction:
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.
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<T> which I can't really get the typeof.
But I can look around a bit, and see if I can figure it out.
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]
}
]
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.