Sign in

    #totto2727/x/json

    Flattens Json values into path-keyed leaves and reconstructs them with bounded, typed validation. See the module README for consumer setup and package imports.

    #Usage

    Flatten a nested document for path-keyed storage, then reconstruct the original structure.

    ///|
    test {
    let source : Json = { "users": [{ "name": "Ada" }] }
    let flattened = flatten(source, JsonPointer)
    assert_eq(flattened, { "/users/0/name": "Ada" })
    assert_eq(from_flatten_json(flattened, JsonPointer), source)
    }

    #Package behavior

    • Supports RFC 6901 JSON Pointer and JavaScript-style paths.
    • Expands arrays by default or preserves them as Json leaves.
    • Rejects conflicting paths, invalid paths, sparse arrays, and oversized paths with FlattenJsonError.

    #API

    This README was generated from the share-artifact skill and README template.

    FlattenJsonError

    pub(all) suberror FlattenJsonError {
    ObjectValue(String)
    AmbiguousPointerToken(String)
    InvalidPath(String)
    PathLengthExceeded(Int)
    PathDepthExceeded(Int)
    PathConflict(String)
    ArrayIndexGap(String)
    } derive(Eq,
    Debug
    )

    A failure while decoding or reconstructing flattened JSON paths.

    ArrayMode

    pub(all) enum ArrayMode {
    Expand
    Preserve
    } derive(Eq,
    Debug
    )

    Controls whether arrays are expanded into indexed paths.

    PathFormat

    pub(all) enum PathFormat {
    JsonPointer
    JavaScript
    } derive(Eq,
    Debug
    )

    The string representation used for flattened JSON paths.

    flatten

    fn flatten(json : Json, format : PathFormat, array_mode? : ArrayMode) -> Map[String, Json] raise FlattenJsonError

    Flattens JSON containers into path-keyed JSON leaf values.

    The caller must select RFC 6901 JSON Pointer or JavaScript-style paths. Objects are traversed recursively. Arrays are expanded by default; pass array_mode=Preserve to retain each array as a single Json leaf. Primitive values and empty arrays remain Json leaves. A root empty object produces an empty map, while nested empty objects remain Json leaves. JSON Pointer also rejects object keys matching RFC 6901 array-index syntax because they cannot be distinguished from array indices during reconstruction; JavaScript paths preserve them as bracket-quoted keys.

    from_flatten_json

    fn from_flatten_json(flattened : Map[String, Json], format : PathFormat) -> Json raise FlattenJsonError

    Reconstructs JSON from path-keyed leaf values.

    format must match the format passed to flatten. Non-empty object values are rejected because they must be represented by their flattened children; empty objects are valid leaves. Paths longer than 16,384 UTF-16 code units or deeper than 256 segments are rejected to keep reconstruction within a bounded typed-error boundary.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io