use smartstring's deserializer to support non borrowed strings

The map visitor for Dynamic was expecting a &str for the key, but the
serde_json deserializer internally uses a Cow string, which can be
Borrowed or Owned. In the case of Owned, the serde_json key deserializer
is calling visit_string on the Visitor, which for &str will result in
the error:
Error("invalid type: string \"a\", expected a borrowed string", line: 0,
column: 0)

smartstring actually has its own Visitor implementation that handles
both cases, so we can use it instead of an explicit conversion.
This commit is contained in:
Geoffroy Couprie
2022-05-23 16:40:49 +02:00
parent 6b57331c60
commit f2b5566c0b
3 changed files with 12 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ use rhai::{
Dynamic, Engine, EvalAltResult, ImmutableString, INT,
};
use serde::{Deserialize, Serialize};
use serde_json::json;
#[cfg(not(feature = "no_index"))]
use rhai::Array;
@@ -814,3 +815,10 @@ fn test_serde_blob() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_serde_json_borrowed_string() {
let value = json!({ "a": "b" });
println!("value: {:?}", value);
let _: Dynamic = serde_json::from_value(value).unwrap();
}