I'm new to rust, and I have a problem while I use serde_json to parser json files.
For example, If I want to define a datatype named Domain, which is possible to be a String or Null. In python I can define its type as Union[str, None], but how do I do in rust?
I have found blew codes in Google search:
enum Domain{
String(String),
Null,
}
But it is not OK when I applied it on my serde_json codes: I need to parser the json code which the key domain maybe a string or null, and above definition will triggle blew error:
AFAIK, serde_json has special handling of Rust’s built-in enum type to signal “some value of type T or null”, which is called Option<T>.
Try if using Option<String> instead of Domain works as expected.
If not, you’ll want to give us more detail, e.g. some code together with some concrete json input that results in the error message you got, so we can help you fix the problem.
Another related intersting problem is in some items some "key"s were lost. That is another problem driving me to think out a "Union type". For example, there are two similar data sturcture when I try to parse a json Value, like: