A simple web tool that converts Rust's Debug
output to JSON, making it easier to work with Rust data structures in JSON format.
URL: https://jimmygchen.github.io/rust-debug-to-json
While logging complex structures with Debug
output is typically avoided due to clutter, it can be useful during testing and debugging. For specific tasks, like checking list lengths or finding elements in large nested structs, it may be useful to convert the data to a common parsable format like JSON for ease of analysis.
Input:
The enum name is not part of the default Debug
output, e.g. Deneb
is the enum variant name here. Therefore it's treated similarly to a struct.
Deneb(BeaconBlockHeader { field: 123 })
Converted JSON:
{
"type": "Deneb",
"values": [
{
"type": "BeaconBlockHeader",
"field": 123
}
]
}
Input:
MyStruct { optional_field: Some("present") }
Converted JSON:
{
"type": "MyStruct",
"optional_field": "present" // or `null` for `None`
}
Input:
Slot(13024)
Converted JSON:
{
"type": "Slot",
"values": [13024]
}
This is a naive implementation mostly generated by AI. It works for basic structures, but there are some known issues:
- It does not currently parse nested complex types correctly.
PhantomData
is not currently handled.