MCPcopy Create free account
hub / github.com/aiscriptdev/aiscript / serde_to_str

Function serde_to_str

aiscript-vm/src/stdlib/serde.rs:50–84  ·  view source on GitHub ↗
(state: &mut State<'gc>, args: Vec<Value<'gc>>)

Source from the content-addressed store, hash-verified

48}
49
50fn serde_to_str<'gc>(state: &mut State<'gc>, args: Vec<Value<'gc>>) -> Result<Value<'gc>, VmError> {
51 let (positional, keyword) = extract_keyword_args(&args)?;
52
53 if positional.len() != 1 {
54 return Err(VmError::RuntimeError(
55 "to_str() requires value argument".into(),
56 ));
57 }
58
59 let pretty = if let Some(pretty_val) = keyword.get("pretty") {
60 match pretty_val {
61 Value::Boolean(b) => *b,
62 _ => {
63 return Err(VmError::RuntimeError(
64 "pretty argument must be a boolean".into(),
65 ));
66 }
67 }
68 } else {
69 false
70 };
71
72 // Convert AIScript Value to JSON value
73 let json_value = to_json_value(&positional[0])?;
74
75 // Convert to string with appropriate formatting
76 let result = if pretty {
77 serde_json::to_string_pretty(&json_value)
78 } else {
79 serde_json::to_string(&json_value)
80 }
81 .map_err(|e| VmError::RuntimeError(format!("Failed to serialize to JSON: {}", e)))?;
82
83 Ok(Value::IoString(Gc::new(state, result)))
84}
85
86fn serde_from_file<'gc>(
87 state: &mut State<'gc>,

Callers

nothing calls this directly

Calls 5

extract_keyword_argsFunction · 0.85
RuntimeErrorClass · 0.85
to_json_valueFunction · 0.85
lenMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected