use crate::registry::Registry;
use crate::tremor_const_fn;
use tremor_value::parse_to_value;
pub fn load(registry: &mut Registry) {
registry
.insert(tremor_const_fn! (json|decode(_context, _input: String) {
let s: String = _input.to_string();
let mut bytes = s.into_bytes();
parse_to_value(bytes.as_mut_slice()).map_err(to_runtime_error).map(Value::into_static)
}))
.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 crate::Value;
#[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"
]"#
)
);
}
}