There is a type in crate
JsonString. Here is its definition:
/// track json serialization with the rust type system! /// JsonString wraps a string containing JSON serialized data /// avoid accidental double-serialization or forgetting to serialize /// serialize any type consistently including hard-to-reach places like Option<Entry> and Result /// JsonString must not itself be serialized/deserialized /// instead, implement and use the native `From` trait to move between types /// - moving to/from String, str, JsonString and JsonString simply (un)wraps it as raw JSON data /// - moving to/from any other type must offer a reliable serialization/deserialization strategy #[derive(Debug, PartialEq, Clone, Hash, Eq, Serialize, Deserialize)] pub struct JsonString(String);
I don’t understand why this type should exist. In my mind, you would create this type if you had functions that want to send or receive serialized JSON across their boundaries (as arguments or return vals). I don’t think you need this type within a function boundary because you could tell from the variable name whether the string contains JSON or not.
Here are some situations in which you would deal with JSON strings
- If you have a function that reads in JSON from some interface, then you want to deserialize it immediately, before it even leaves the function boundary
- If you have a function that outputs JSON into some interface, then you want to take a struct that has the data in the right format, and then serialize as you write out the JSON.
- If you have a function that is agnostic to the content coming between interfaces, in other words, it just passes data through, then you’re not going to validate that it’s JSON. You probably don’t even need it to be UTF8. You want to use a
Those are all the cases that I can think of, and in all of them, you don’t want to use a
Am I missing something? Is this type actually as useless as it is in my imagination?