serde-rs / json

Strongly typed JSON library for Rust
Apache License 2.0
4.87k stars 556 forks source link

Question: Possible to generate a json stub without providing a value? #880

Open Carter12s opened 2 years ago

Carter12s commented 2 years ago

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:

  1. serde_json should technically be able to generate these stubs without requiring a default I think?
  2. 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?
kangalio commented 1 year ago

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