mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-10 12:09:31 +00:00
serde_keyvalue: use nom-based methods for deserialize_any
These methods are better to predict the type of what follows than just parsing the next character. For instance, '1foo' was interpreted as an integer whereas it should be a string. BUG=b:218223240 BUG=b:241300017 TEST=cargo test -p serde_keyvalue TEST=cargo test Change-Id: I9a0a3cb73e06783784d47450de7d291adf41aa53 Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3889330 Auto-Submit: Alexandre Courbot <acourbot@chromium.org> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
parent
7b78909091
commit
92918a5b01
1 changed files with 15 additions and 12 deletions
|
@ -436,19 +436,22 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut KeyValueDeserializer<'de> {
|
|||
where
|
||||
V: serde::de::Visitor<'de>,
|
||||
{
|
||||
// If we have no value following, then we are dealing with a boolean flag.
|
||||
match self.peek_char() {
|
||||
Some('0'..='9') => self.deserialize_u64(visitor),
|
||||
Some('-') => self.deserialize_i64(visitor),
|
||||
Some('"') => self.deserialize_string(visitor),
|
||||
// Only possible option here is boolean flag.
|
||||
Some(',') | None => self.deserialize_bool(visitor),
|
||||
_ => {
|
||||
// We probably have an unquoted string, but possibly a boolean as well.
|
||||
match any_identifier(self.input) {
|
||||
Ok((_, "true")) | Ok((_, "false")) => self.deserialize_bool(visitor),
|
||||
_ => self.deserialize_str(visitor),
|
||||
}
|
||||
}
|
||||
Some(',') | None => return self.deserialize_bool(visitor),
|
||||
_ => (),
|
||||
}
|
||||
|
||||
// This is ambiguous as technically any argument could be an unquoted string. However we
|
||||
// don't have any type information here, so try to guess it on a best-effort basis...
|
||||
if any_number::<i64>(self.input).is_ok() {
|
||||
self.deserialize_i64(visitor)
|
||||
} else if any_number::<u64>(self.input).is_ok() {
|
||||
self.deserialize_u64(visitor)
|
||||
} else if any_bool(self.input).is_ok() {
|
||||
self.deserialize_bool(visitor)
|
||||
} else {
|
||||
self.deserialize_str(visitor)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue