Skip to content

Commit

Permalink
Fix map roundtrips in untagged enums (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekleog authored Feb 27, 2024
1 parent b1b73ac commit 6281985
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/de.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use js_sys::{Array, ArrayBuffer, JsString, Number, Object, Symbol, Uint8Array};
use js_sys::{Array, ArrayBuffer, JsString, Map, Number, Object, Symbol, Uint8Array};
use serde::de::value::{MapDeserializer, SeqDeserializer};
use serde::de::{self, IntoDeserializer};
use std::convert::TryFrom;
Expand Down Expand Up @@ -317,12 +317,15 @@ impl<'de> de::Deserializer<'de> for Deserializer {
// (see https://github.com/RReverser/serde-wasm-bindgen/pull/4#discussion_r352245020).
//
// We expect such enums to be represented via plain JS objects, so let's explicitly
// exclude Sets, Maps and any other iterables. These should be deserialized via concrete
// exclude Sets and other iterables. These should be deserialized via concrete
// `deserialize_*` methods instead of us trying to guess the right target type.
//
// We still do support Map, so that the format described here stays a self-describing
// format: we happen to serialize to Map, and it is not ambiguous.
//
// Hopefully we can rid of these hacks altogether once
// https://github.com/serde-rs/serde/issues/1183 is implemented / fixed on serde side.
!Symbol::iterator().js_in(&self.value)
(!Symbol::iterator().js_in(&self.value) || self.value.has_type::<Map>())
{
self.deserialize_map(visitor)
} else {
Expand Down
26 changes: 26 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use wasm_bindgen_test::wasm_bindgen_test;

const SERIALIZER: Serializer = Serializer::new();

const JSON_SERIALIZER: Serializer = Serializer::json_compatible();

const BIGINT_SERIALIZER: Serializer =
Serializer::new().serialize_large_number_types_as_bigints(true);

Expand Down Expand Up @@ -664,6 +666,30 @@ fn enums() {
}
}

#[wasm_bindgen_test]
fn serde_json_value_with_json() {
test_via_round_trip_with_config(
serde_json::from_str::<serde_json::Value>("[0, \"foo\"]").unwrap(),
&JSON_SERIALIZER,
);
test_via_round_trip_with_config(
serde_json::from_str::<serde_json::Value>(r#"{"foo": "bar"}"#).unwrap(),
&JSON_SERIALIZER,
);
}

#[wasm_bindgen_test]
fn serde_json_value_with_default() {
test_via_round_trip_with_config(
serde_json::from_str::<serde_json::Value>("[0, \"foo\"]").unwrap(),
&SERIALIZER,
);
test_via_round_trip_with_config(
serde_json::from_str::<serde_json::Value>(r#"{"foo": "bar"}"#).unwrap(),
&SERIALIZER,
);
}

#[wasm_bindgen_test]
fn preserved_value() {
#[derive(serde::Deserialize, serde::Serialize, PartialEq, Clone, Debug)]
Expand Down

0 comments on commit 6281985

Please sign in to comment.