Make conversion from Rust <-> SurrealDB lossless (#1600)

This commit is contained in:
Rushmore Mushambi 2023-01-14 21:29:39 +02:00 committed by GitHub
parent af88fcef38
commit 7f927d77aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,7 +18,6 @@ use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
use serde_json::json; use serde_json::json;
use serde_json::Value as JsonValue; use serde_json::Value as JsonValue;
use std::collections::BTreeMap;
pub use endpoint::*; pub use endpoint::*;
pub use query::*; pub use query::*;
@ -174,24 +173,9 @@ where
} }
pub(crate) fn from_json(json: JsonValue) -> sql::Value { pub(crate) fn from_json(json: JsonValue) -> sql::Value {
match json { match sql::json(&json.to_string()) {
JsonValue::Null => sql::Value::None, Ok(value) => value,
JsonValue::Bool(boolean) => boolean.into(), // It shouldn't get to this as `JsonValue` will always produce valid JSON
JsonValue::Number(number) => match (number.as_u64(), number.as_i64(), number.as_f64()) { Err(_) => unreachable!(),
(Some(number), _, _) => number.into(),
(_, Some(number), _) => number.into(),
(_, _, Some(number)) => number.into(),
_ => unreachable!(),
},
JsonValue::String(string) => match sql::thing(&string) {
Ok(thing) => thing.into(),
Err(_) => string.into(),
},
JsonValue::Array(array) => array.into_iter().map(from_json).collect::<Vec<_>>().into(),
JsonValue::Object(object) => object
.into_iter()
.map(|(key, value)| (key, from_json(value)))
.collect::<BTreeMap<_, _>>()
.into(),
} }
} }