Unfair #rustlang #serde Quiz: What's the difference between these four types' deserialization behavior (ignoring the type name)?

#[derive(Deserialize)]
struct One {
field: Option<String>,
}

#[derive(Deserialize)]
struct Two {
#[serde(default)]
field: Option<String>,
}

#[derive(Deserialize)]
struct Three {
#[serde(deserialize_with = "Option::deserialize")]
field: Option<String>,
}

#[derive(Deserialize)]
struct Four {
#[serde(default, deserialize_with = "Option::deserialize")]
field: Option<String>,
}

@jplatte

Depends heavily on the Deserializer implementation, but

1) empty string deserializes as Some("")
2) empty string deserializes as None
3) empty string deserializes as Option::deserialize("")
4) empty string deserializes as None

In a format with explicit fields, 1 and 3 will fail if the field is not present, 2 and 4 will not.

@emc2 No, serde(default) on an Option has nothing to do with how empty strings are handled, but good guess. If you can show me a deserializer that treats empty strings differently between any of these, I'll be very surprised.

Also if by "a format with explicit fields" you mean something like JSON, you should try the absent field with serde_json, because it does not behave like you suggested ;)

@jplatte one will fail if the field isn't there - in json it needs to be null or a string value. two will work and act as if there was null if it isn't present.

on the second two I am not as sure, but I'd think that three does as one does, but four doesn't what two does, I think it will fails like three and one.

@gnunicorn Wrong on almost all accounts, but props for actually posting your best guess instead of trying it out :P
@jplatte when the field is missing the ones with serde(default) will have the value from the Default impl for the struct, unlike the ones missing it which will be None. the ones with deserialize_with will behave the same as without
@vavakado No, serde(default) uses the Default impl of the field type, not the struct's Default impl. And deserialize_with does make a difference, otherwise I wouldn't have included it x)