Implement from_json and to_json
This commit is contained in:
parent
9b693ba41b
commit
99dd189328
35
src/built_in_functions/data_formats.rs
Normal file
35
src/built_in_functions/data_formats.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
use crate::{BuiltInFunction, Error, Map, Result, Value};
|
||||||
|
|
||||||
|
pub struct FromJson;
|
||||||
|
|
||||||
|
impl BuiltInFunction for FromJson {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"from_json"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, arguments: &[Value], _context: &Map) -> Result<Value> {
|
||||||
|
Error::expect_built_in_function_argument_amount(self, 1, arguments.len())?;
|
||||||
|
|
||||||
|
let json_string = arguments.first().unwrap().as_string()?;
|
||||||
|
let value = serde_json::from_str(&json_string)?;
|
||||||
|
|
||||||
|
Ok(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ToJson;
|
||||||
|
|
||||||
|
impl BuiltInFunction for ToJson {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"to_json"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, arguments: &[Value], _context: &Map) -> Result<Value> {
|
||||||
|
Error::expect_built_in_function_argument_amount(self, 1, arguments.len())?;
|
||||||
|
|
||||||
|
let value = arguments.first().unwrap();
|
||||||
|
let json_string = serde_json::to_string(&value)?;
|
||||||
|
|
||||||
|
Ok(Value::String(json_string))
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,16 @@
|
|||||||
use crate::{Map, Result, Value};
|
use crate::{Map, Result, Value};
|
||||||
|
|
||||||
mod assert;
|
mod assert;
|
||||||
|
mod data_formats;
|
||||||
mod fs;
|
mod fs;
|
||||||
mod output;
|
mod output;
|
||||||
mod r#type;
|
mod r#type;
|
||||||
|
|
||||||
pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 7] = [
|
pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 9] = [
|
||||||
&assert::Assert,
|
&assert::Assert,
|
||||||
&assert::AssertEqual,
|
&assert::AssertEqual,
|
||||||
|
&data_formats::FromJson,
|
||||||
|
&data_formats::ToJson,
|
||||||
&fs::Read,
|
&fs::Read,
|
||||||
&fs::Write,
|
&fs::Write,
|
||||||
&fs::Append,
|
&fs::Append,
|
||||||
|
Loading…
Reference in New Issue
Block a user