Skip to content

fastc-lang/fastc-core-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fastc-core-json

JSON encoder + integer-field decoder for fastC.

Part of the fastc-core launch set. Implementation ships in the fastC v1.0 prelude — use json::* works out of the box.

Encoder

Builder model over an owned Str buffer:

use json::new_builder;
use json::obj_start;
use json::key;
use json::int_value;
use json::str_value;
use json::obj_end;

fn render() -> Str {
    let b: JsonBuilder = new_builder();
    obj_start(addrm(b));
        key(addrm(b), cstr("id"));     int_value(addrm(b), 42);
        key(addrm(b), cstr("name"));   str_value(addrm(b), cstr("alice"));
    obj_end(addrm(b));
    return (deref(addr(b))).out;
}

Produces {"id":42,"name":"alice"}.

Decoder slice

find_int(text, key, fallback) — locate a top-level "key": <integer> and return the parsed i64. Returns fallback for absent keys, non-integer values, malformed JSON, or keys nested below the top level.

use json::find_int;
let resp: raw(u8) = cstr("{\"id\": 42, \"count\": 7}");
let id: i64 = find_int(resp, cstr("id"), cast(i64, -1));     // 42
let absent: i64 = find_int(resp, cstr("x"), cast(i64, -1));  // -1

v1 covers the 80% "pull an int out of an HTTP response" case. Full DOM + streaming decoder lands when there's user demand.

Status

v0.1.0 — preview. API is final.

License

MIT

About

JSON encoder + integer-field decoder for fastC. Part of the fastc-core launch set.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors