use crate::registry::Registry;
use crate::tremor_const_fn;
use simd_json::to_owned_value;
pub fn load(registry: &mut Registry) {
registry
.insert(tremor_const_fn! (json::decode(_context, _input: String) {
let mut s: String = _input.to_string();
println!("{}", &s);
let mut bytes = unsafe{s.as_bytes_mut()};
to_owned_value(&mut bytes).map_err(to_runtime_error).map(Value::from)
}))
.insert(tremor_const_fn! (json::encode(_context, _input) {
simd_json::to_string(_input).map(Value::from).map_err(to_runtime_error)
}))
.insert(tremor_const_fn! (json::encode_pretty(_context, _input) {
simd_json::to_string_pretty(_input).map(Value::from).map_err(to_runtime_error)
}));
}
#[cfg(test)]
mod test {
use crate::registry::fun;
use simd_json::BorrowedValue as Value;
macro_rules! assert_val {
($e:expr, $r:expr) => {
assert_eq!($e, Ok(Value::from($r)))
};
}
#[test]
fn decode() {
let f = fun("json", "decode");
let v = Value::from(r#"["this","is","a","cake"]"#);
assert_val!(f(&[&v]), Value::from(vec!["this", "is", "a", "cake"]));
}
#[test]
fn encode() {
let f = fun("json", "encode");
let v = Value::from(vec!["this", "is", "a", "cake"]);
assert_val!(f(&[&v]), Value::from(r#"["this","is","a","cake"]"#));
}
#[test]
fn encode_pretty() {
let f = fun("json", "encode_pretty");
let v = Value::from(vec!["this", "is", "a", "cake"]);
assert_val!(
f(&[&v]),
Value::from(
r#"[
"this",
"is",
"a",
"cake"
]"#
)
);
}
}