Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MoaidHathot opened this issue Jul 16, 2023 · 4 comments
Labels
enhancement New feature or request Rendering Rendering feature

Comments

@MoaidHathot
Copy link
Owner

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
Copy link
Contributor

Hejle commented Jul 17, 2023

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
Copy link
Contributor

Hejle commented Jul 17, 2023

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
Copy link
Owner Author

MoaidHathot commented Jul 17, 2023

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
Copy link

latop2604 commented Oct 5, 2023

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

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Rendering Rendering feature
Projects
None yet
Development

No branches or pull requests

3 participants