shape-runtime 0.2.0

Bytecode compiler, builtins, and runtime infrastructure for Shape
Documentation
/// @module std::core::json
/// JSON Parsing and Serialization
///
/// Parse JSON strings into typed `Json` enum values and serialize Shape
/// values back to JSON strings. Pull in the `Json` ADT (re-exported from
/// `std::core::json_value`) so the enum is in scope wherever this module
/// is imported.
///
/// # Example
///
/// ```shape
/// use std::core::json
///
/// let data = json.parse("{\"name\": \"Alice\", \"age\": 30}")
/// match data {
///     Ok(value) => match value {
///         Json::Object(_) => print("got an object")
///         _ => print("got some other JSON value")
///     }
///     Err(e) => print(f"Parse error: {e}")
/// }
/// ```

use std::core::json_value

/// Parse a JSON string into Shape values.
///
/// Returns a typed `Json` enum when the schema is registered,
/// otherwise returns untyped Shape values (HashMap, Array, etc.).
///
/// # Arguments
///
/// * `text` - JSON string to parse
///
/// # Returns
///
/// `Ok(value)` with the parsed value, or `Err(message)` on parse failure.
///
/// # Example
///
/// ```shape
/// let result = json.parse("[1, 2, 3]")
/// ```
pub builtin fn parse(text: string) -> Result<_, string>;

/// Parse a JSON string into a typed struct using a schema.
///
/// Internal function used by the compiler for typed JSON deserialization.
/// Deserializes JSON directly into a TypedObject using the registered schema.
///
/// # Arguments
///
/// * `text` - JSON string to parse
/// * `schema_id` - Schema ID of the target type
///
/// # Returns
///
/// `Ok(value)` with the typed struct, or `Err(message)` on failure.
builtin fn __parse_typed(text: string, schema_id: float) -> Result<_, string>;

/// Serialize a Shape value to a JSON string.
///
/// # Arguments
///
/// * `value` - Value to serialize
/// * `pretty` - Pretty-print with indentation (default: false)
///
/// # Returns
///
/// `Ok(json_string)` with the JSON output, or `Err(message)` on failure.
///
/// # Example
///
/// ```shape
/// let text = json.stringify({ name: "Alice", age: 30 })
/// let pretty = json.stringify({ name: "Alice" }, true)
/// ```
pub builtin fn stringify(value: _, pretty: bool) -> Result<string, string>;

/// Check if a string is valid JSON.
///
/// # Arguments
///
/// * `text` - String to validate as JSON
///
/// # Returns
///
/// `true` if the string is valid JSON, `false` otherwise.
///
/// # Example
///
/// ```shape
/// json.is_valid("{\"key\": \"value\"}")  // true
/// json.is_valid("{invalid}")             // false
/// ```
pub builtin fn is_valid(text: string) -> bool;