Skip to content

Commit

Permalink
fix: round-trip properly serde_json::Value
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekleog committed Feb 20, 2024
1 parent e65f027 commit df6f35f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
16 changes: 2 additions & 14 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, Number, Object, Uint8Array};
use serde::de::value::{MapDeserializer, SeqDeserializer};
use serde::de::{self, IntoDeserializer};
use std::convert::TryFrom;
Expand Down Expand Up @@ -314,19 +314,7 @@ impl<'de> de::Deserializer<'de> for Deserializer {
// We need to handle this here because serde uses `deserialize_any`
// for internally tagged enums
visitor.visit_byte_buf(bytes)
} else if self.value.is_object() &&
// The only reason we want to support objects here is because serde uses
// `deserialize_any` for internally tagged enums
// (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
// `deserialize_*` methods instead of us trying to guess the right target type.
//
// 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)
{
} else if self.value.is_object() {
self.deserialize_map(visitor)
} else {
self.invalid_type(visitor)
Expand Down
12 changes: 12 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,18 @@ fn enums() {
}
}

#[wasm_bindgen_test]
fn serde_json_value() {
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 df6f35f

Please sign in to comment.