// std/json — JSON utility patterns
//
// Import with: import "std/json"
pub import "std/json/stream"
/**
* Pretty-print a value as indented JSON (2-space indent).
* pretty.
*
* @effects: []
* @allocation: heap
* @errors: []
* @api_stability: stable
* @example: pretty(value)
*/
pub fn pretty(value) {
return json_stringify_pretty(value)
}
/**
* Safely parse JSON, returning nil on failure instead of throwing.
* safe_parse.
*
* @effects: []
* @allocation: heap
* @errors: []
* @api_stability: stable
* @example: safe_parse(text)
*/
pub fn safe_parse(text) {
let result = try {
json_parse(text)
}
if is_ok(result) {
return unwrap(result)
}
return nil
}
/**
* Shallow-merge two dicts. Keys in `b` override matching keys in `a`.
* Row-polymorphic: each argument carries its own row of fields, and the result
* is the right-biased merge of both rows (`b`'s fields override `a`'s, every
* field preserved with its real type). Merging two records with *different*
* value types is therefore sound and precise — `merge({a: 1}, {b: "x"})`
* returns `{a: int, b: string}` rather than collapsing to `dict`.
*
* @effects: []
* @allocation: heap
* @errors: []
* @api_stability: stable
* @example: merge(a, b)
*/
pub fn merge<R1, R2>(a: {...R1}, b: {...R2}) -> {...R1, ...R2} {
return __dict_merge(a ?? {}, b ?? {})
}
/**
* Pick the listed keys from `data`, dropping entries whose stored value is
* `nil`. Generic over the value type — when called with a `dict<string, V>`
* the projection keeps the same V.
*
* @effects: []
* @allocation: heap
* @errors: []
* @api_stability: stable
* @example: pick(data, keys)
*/
pub fn pick<V>(data: dict<string, V>, keys: list<string>) -> dict<string, V> {
return __dict_pick(data ?? {}, keys ?? [])
}
/**
* Drop the listed keys from `data`. Generic over the value type — the
* resulting dict keeps the same value contract as the input.
*
* @effects: []
* @allocation: heap
* @errors: []
* @api_stability: stable
* @example: omit(data, keys)
*/
pub fn omit<V>(data: dict<string, V>, keys: list<string>) -> dict<string, V> {
return __dict_omit(data ?? {}, keys ?? [])
}