Skip to content

Commit

Permalink
Merge pull request #8 from erickt/fix-deserialization
Browse files Browse the repository at this point in the history
Fix deserialization of unit variants
  • Loading branch information
erickt committed Apr 22, 2024
2 parents 265839a + ff30dcd commit 788a2cf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde_json5"
version = "0.1.0"
version = "0.2.0"
authors = ["Gary Bressler <geb@google.com>"]
description = "A Serde (de)serializer for JSON5."
license = "Apache-2.0"
Expand Down
6 changes: 5 additions & 1 deletion third_party/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,11 @@ impl<'de> de::VariantAccess<'de> for Variant<'de> {
type Error = Error;

fn unit_variant(self) -> Result<()> {
Ok(())
if let Some(pair) = self.pair {
serde::Deserialize::deserialize(&mut Deserializer::from_pair(pair))
} else {
Ok(())
}
}

fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
Expand Down
9 changes: 4 additions & 5 deletions third_party/tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
use matches::assert_matches;
use serde_json5::{Error, Location};

#[allow(unused)]
pub fn deserializes_to<'a, T>(s: &'a str, v: T)
where
T: ::std::fmt::Debug + ::std::cmp::PartialEq + serde::de::Deserialize<'a>,
{
assert_matches!(serde_json5::from_str::<T>(s), Ok(value) if value == v);
assert_eq!(serde_json5::from_str::<T>(s), Ok(v));
}

#[allow(unused)]
pub fn deserializes_to_nan_f32(s: &str) {
assert_matches!(serde_json5::from_str::<f32>(s), Ok(value) if value.is_nan());
assert!(serde_json5::from_str::<f32>(s).unwrap().is_nan());
}

#[allow(unused)]
pub fn deserializes_to_nan_f64(s: &str) {
assert_matches!(serde_json5::from_str::<f64>(s), Ok(value) if value.is_nan());
assert!(serde_json5::from_str::<f64>(s).unwrap().is_nan());
}

#[allow(unused)]
pub fn deserializes_with_error<'a, T>(s: &'a str, error_expected: Error)
where
T: ::std::fmt::Debug + ::std::cmp::PartialEq + serde::de::Deserialize<'a>,
{
assert_matches!(serde_json5::from_str::<T>(s), Err(e) if e == error_expected);
assert_eq!(serde_json5::from_str::<T>(s), Err(error_expected));
}

#[allow(unused)]
Expand Down
16 changes: 14 additions & 2 deletions third_party/tests/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ fn deserializes_enum() {
}

deserializes_to("'A'", E::A);
deserializes_to("{ A: null }", E::A);
deserializes_to("{ B: 2 }", E::B(2));
deserializes_to("{ C: [3, 5] }", E::C(3, 5));
deserializes_to("{ D: { a: 7, b: 11 } }", E::D { a: 7, b: 11 });
Expand All @@ -712,6 +713,7 @@ fn deserializes_enum_error() {
enum E {
A {},
B(),
C,
}

#[derive(Deserialize, PartialEq, Debug)]
Expand All @@ -721,9 +723,19 @@ fn deserializes_enum_error() {

deserializes_with_error::<S>("{ e: 'A' }", make_error("expected an object", 1, 6));
deserializes_with_error::<S>("{ e: 'B' }", make_error("expected an array", 1, 6));
deserializes_with_error::<S>("{ e: { 'A': 5 } }", make_error("expected an object", 1, 6));
deserializes_with_error::<S>("{ e: { 'B': 5 } }", make_error("expected an array", 1, 6));
deserializes_with_error::<S>(
"{ e: { 'C': 5 } }",
make_error("invalid type: integer `5`, expected unit", 1, 13),
);
deserializes_with_error::<S>(
"{ e: { 'C': {} } }",
make_error("invalid type: map, expected unit", 1, 13),
);
deserializes_with_error::<E>(
"\n 'C'",
make_error("unknown variant `C`, expected `A` or `B`", 2, 2),
"\n 'D'",
make_error("unknown variant `D`, expected one of `A`, `B`, `C`", 2, 2),
);
}

Expand Down

0 comments on commit 788a2cf

Please sign in to comment.