1use koto_runtime::prelude::*;
4use koto_serde::{DeserializableKValue, SerializableKValue};
5
6pub fn make_module() -> KMap {
7 let result = KMap::with_type("json");
8
9 result.add_fn("from_string", |ctx| match ctx.args() {
10 [KValue::Str(s)] => match serde_json::from_str::<DeserializableKValue>(s) {
11 Ok(result) => Ok(result.into()),
12 Err(e) => runtime_error!(
13 "json.from_string: Error while parsing input: {}",
14 e.to_string()
15 ),
16 },
17 unexpected => unexpected_args("|String|", unexpected),
18 });
19
20 result.add_fn("to_string", |ctx| match ctx.args() {
21 [value] => match serde_json::to_string_pretty(&SerializableKValue(value)) {
22 Ok(result) => Ok(result.into()),
23 Err(e) => runtime_error!("json.to_string: {e}"),
24 },
25 unexpected => unexpected_args("|Any|", unexpected),
26 });
27
28 result
29}