json
json copied to clipboard
Question: Possible to generate a json stub without providing a value?
I'm working on a command line utility that takes json data payloads as an argument.
An incredibly convenient feature of this utility is to generate a "stub" json of the correct structure for the user to then fill with valid data.
Example:
cli_tool publish endpoint_name <TAB>
# Results in tab completion of:
cli_tool publish endpoint_name "{\"user_id\": \"\" }"
# cli user then fills out user_id and can call endpoint
I've achieved this functionality so far by requiring all structs that interact with this CLI to impl Default and passing their default value into serde_json to generate the stub.
I'm curious about the following:
- serde_json should technically be able to generate these stubs without requiring a default I think?
- If this is technically possible, would it be an worthwhile API to add to serde_json, and if I created an MR would it be accepted?
serde_json should technically be able to generate these stubs without requiring a default I think?
You mean, serde_json would generate the default values for each field itself? Wouldn't that reinvent the wheel of the Default trait? The Default trait is intended for exactly this: a stub value of a given type