JSON & Process Spawning
Wysna provides structured data serialization and process execution modules in its standard library: std/json and std/process.
JSON Handling (std/json)
Section titled “JSON Handling (std/json)”The std/json module parses and serializes standard JSON data structures using the algebraic type json.Value and helper query functions.
Parsing and Querying JSON
Section titled “Parsing and Querying JSON”import "std/json.wysna"
fn main() -> unit { let raw = "{\"name\":\"gateway\",\"enabled\":true}"
let decoded = json.parse(raw) else err { print("JSON parse error at offset:", err.offset, err.message) return }
# Query fields safely with json.get and typed extractors let name_value = json.get(decoded, "name") ?? json.null() print("Service name:", json.as_string(name_value) ?? "missing")
# Serializing back to JSON text let output = json.stringify(decoded) else err { print("Serialization error:", err.message) return } print("Serialized:", output)}Available std/json Functions
Section titled “Available std/json Functions”json.parse(source: str) -> json.Value ! json.JsonError: Parses raw JSON text.json.stringify(value: json.Value) -> str ! json.JsonError: Serializes a JSON tree into compact UTF-8 text.json.get(obj: json.Value, key: str) -> json.Value?: Retrieves a field from a JSON object.json.at(arr: json.Value, index: int) -> json.Value?: Retrieves an item by index from a JSON array.json.as_string(val: json.Value) -> str?: Extracts the underlying string value.json.as_bool(val: json.Value) -> bool?: Extracts the underlying boolean flag.json.null() -> json.Value: Returns a canonical JSON Null variant.
Process Spawning (std/process)
Section titled “Process Spawning (std/process)”The std/process module executes external system binaries directly without an intermediate command shell. Arguments are passed as raw UTF-8 string vectors, preventing shell injection vulnerabilities.
import "std/process.wysna"
fn main() -> unit { # Arguments: executable, argv array, timeout in milliseconds (max 10 minutes) let exit_code = process.run("git", ["status", "--short"], 5000) else err { print("Failed to spawn process:", err.operation, "code:", err.code) return }
print("Process exited with status:", exit_code)}Key security guarantees of process.run:
- No Shell Interpolation: The executable is invoked directly via OS process primitives (
execvp/CreateProcess). - Bounded Execution: Timeouts are strictly enforced (between 1ms and 600,000ms).
- Inherited Streams: Standard output and standard error are inherited from the parent process.