json_mlake ยท API

json.deserialize

Traits

Deserializable

Trait for types that can be deserialized from JSON.

Implement this trait to enable automatic deserialization with deserialize().

Example: struct Person(Deserializable): var name: String var age: Int

    @staticmethod
    def from_json(json: Value) raises -> Self:
        return Self(
            name=get_string(json, "name"),
            age=get_int(json, "age")
        )

var person = deserialize[Person]('{"name":"Alice","age":30}')

from_json

def from_json(json: Value) -> Self

Deserialize from a JSON Value.

Functions

get_string

def get_string(value: Value, key: String) -> String

Extract a string field from a JSON object Value.

Example: var json = loads('{"name": "Alice"}') var name = get_string(json, "name") # Returns "Alice".

Args
value: The JSON object Value.
key: The field name.

get_int

def get_int(value: Value, key: String) -> Int

Extract an int field from a JSON object Value.

Example: var json = loads('{"age": 30}') var age = get_int(json, "age") # Returns 30.

Args
value: The JSON object Value.
key: The field name.

get_bool

def get_bool(value: Value, key: String) -> Bool

Extract a bool field from a JSON object Value.

Example: var json = loads('{"active": true}') var active = get_bool(json, "active") # Returns True.

Args
value: The JSON object Value.
key: The field name.

get_float

def get_float(value: Value, key: String) -> Float64

Extract a float field from a JSON object Value.

Example: var json = loads('{"price": 19.99}') var price = get_float(json, "price") # Returns 19.99.

Args
value: The JSON object Value.
key: The field name.

deserialize

def deserialize[T: Deserializable, target: StringSpan[ImmStaticOrigin] = StringSpan("cpu")](json_str: String) -> T

Deserialize a JSON string into a typed object.

The type must implement the Deserializable trait with a from_json() static method.

Example: var person = deserializePerson print(person.name) # Prints "Alice".

Parameters
T: Type that implements Deserializable.
target: "cpu" (default) or "gpu" for parsing backend.
Args
json_str: JSON string to deserialize.