Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:chrestomathy: add ziglang.org json parsing example
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 5bc84a252625ba837eccf9b0198bbc20d96105da17964d6c5f0456beadd690c5
User & Date: admin 2021-10-16 13:57:26
Context
2021-10-30
18:58
s/info/alert/ so that non-debug runs have output check-in: aabb260b80 user: admin tags: trunk
2021-10-16
13:57
chrestomathy: add ziglang.org json parsing example check-in: 5bc84a2526 user: admin tags: trunk
2021-10-03
08:14
add BOM.none check-in: 8d26af00d9 user: admin tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added chrestomathy/zigjson/jsonex.d.



































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#! /usr/bin/env dub
/+ dub.sdl:
    dependency "painlessjson" version="~>1.4.0"
+/
import std.json : parseJSON;
import painlessjson : fromJSON;

enum payload = q"json
{
    "vals": {
        "testing": 1,
        "production": 42
    },
    "uptime": 9999
}
json";

struct Config {
    struct Vals {
        ubyte testing, production;
    }
    Vals vals;
    ulong uptime;
}

const config = fromJSON!Config(payload.parseJSON);

void main() {
    import std.stdio : writefln;

    static assert(config.vals.production <= 50, "only up to 50 supported");
    writefln!"up=%d"(config.uptime);
}

Added chrestomathy/zigjson/ziglangorg.zig.

























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const std = @import("std");
const json = std.json;
const payload =
    \\{
    \\    "vals": {
    \\        "testing": 1,
    \\        "production": 42
    \\    },
    \\    "uptime": 9999
    \\}
;
const Config = struct {
    vals: struct { testing: u8, production: u8 },
    uptime: u64,
};
const config = x: {
    var stream = json.TokenStream.init(payload);
    const res = json.parse(Config, &stream, .{});
    // Assert no error can occur since we are
    // parsing this JSON at comptime!
    break :x res catch unreachable;
};
pub fn main() !void {
    if (config.vals.production > 50) {
        @compileError("only up to 50 supported");
    }
    std.log.info("up={d}", .{config.uptime});
}