Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: round-trip properly serde_json::Value #64

Merged
merged 3 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -320,12 +320,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
Loading