Json module

from_query

procedure · 1 overloadSource

Decodes a T from pairs. A key that repeats gives an array for an array field, and is a "duplicate field" error for any other. Keys no field has are ignored unless T is marked deny_unknown_fields. Everything allocated (strings, arrays) comes from context.allocator and is released by free_json, as for from_json.

ExampleOpen in playground ↗
Search :: struct @json(default) { q: string; limit := 20; tags: [..] string; }

// The pairs of "?q=jai&tags=a&tags=b".
pairs := Query_Pair.[.{"q", "jai"}, .{"tags", "a"}, .{"tags", "b"}];
search, error := from_query(Search, pairs);

assert(error.kind == .NONE);
assert(search.q == "jai");
assert(search.limit == 20);
assert(search.tags.count == 2);
from_query :: (
    $T: Type,
    pairs: [] Query_Pair,
    options := Json_Options.{}
) -> (T, Json_Error)

Decodes a T from pairs. A key that repeats gives an array for an array field, and is a "duplicate field" error for any other. Keys no field has are ignored unless T is marked deny_unknown_fields. Everything allocated (strings, arrays) comes from context.allocator and is released by free_json, as for from_json.

Parameters

$T: Type
pairs: [] Query_Pair
options := Json_Options.{}

Returns

T
ExampleOpen in playground ↗
Search :: struct @json(default) { q: string; limit := 20; tags: [..] string; }

// The pairs of "?q=jai&tags=a&tags=b".
pairs := Query_Pair.[.{"q", "jai"}, .{"tags", "a"}, .{"tags", "b"}];
search, error := from_query(Search, pairs);

assert(error.kind == .NONE);
assert(search.q == "jai");
assert(search.limit == 20);
assert(search.tags.count == 2);

See also