Fix directing JSON data to JS visualizations. (#3984)

This commit is contained in:
Michał Wawrzyniec Urbańczyk 2022-12-15 11:29:09 +01:00 committed by GitHub
parent b9bf958f2c
commit a2d8c1d0ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -214,7 +214,7 @@ impl InstanceModel {
_ => return Err(DataError::BinaryNotSupported),
};
let data_json: &serde_json::Value = data_json.deref();
let data_js = match serde_wasm_bindgen::to_value(data_json) {
let data_js = match json_to_value(data_json) {
Ok(value) => value,
Err(_) => return Err(DataError::InvalidDataType),
};
@ -353,3 +353,17 @@ fn get_method(
) -> Result<web::Function> {
Ok(default())
}
/// Convert the given JSON value to a `JsValue`.
///
/// Note that we need to use special serializer, as `serde_wasm_bindgen` defaults to outputting
/// some special `Map` type that is not supported by the visualization API (rather than proper
/// objects).
#[cfg(target_arch = "wasm32")]
pub fn json_to_value(
json: &serde_json::Value,
) -> std::result::Result<JsValue, serde_wasm_bindgen::Error> {
use serde::Serialize;
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
json.serialize(&serializer)
}