Skip to content

Commit e15548e

Browse files
authored
Merge pull request #71 from daurnimator/add-zig-jsonValue
Add zig implementation using std.json.Value
2 parents 233815f + 720e564 commit e15548e

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

zig/jsonValue.zig

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const std = @import("std");
2+
3+
const input =
4+
\\[
5+
\\ {
6+
\\ "title": "Getting started",
7+
\\ "reset_lesson_position": false,
8+
\\ "lessons": [
9+
\\ {"name": "Welcome"},
10+
\\ {"name": "Installation"}
11+
\\ ]
12+
\\ },
13+
\\
14+
\\ {
15+
\\ "title": "Basic operator",
16+
\\ "reset_lesson_position": false,
17+
\\ "lessons": [
18+
\\ {"name": "Addition / Subtraction"},
19+
\\ {"name": "Multiplication / Division"}
20+
\\ ]
21+
\\ },
22+
\\
23+
\\ {
24+
\\ "title": "Advanced topics",
25+
\\ "reset_lesson_position": true,
26+
\\ "lessons": [
27+
\\ {"name": "Mutability"},
28+
\\ {"name": "Immutability"}
29+
\\ ]
30+
\\ }
31+
\\]
32+
;
33+
34+
pub fn main() !void {
35+
const stdout = std.io.getStdOut().writer();
36+
37+
var p = std.json.Parser.init(std.heap.page_allocator, false);
38+
defer p.deinit();
39+
var tree = try p.parse(input);
40+
defer tree.deinit();
41+
42+
var section_counter: u32 = 1;
43+
var lesson_counter: u32 = 1;
44+
for (tree.root.Array.items) |*section| {
45+
if (section.Object.get("reset_lesson_position").?.Bool) {
46+
lesson_counter = 1;
47+
}
48+
49+
try section.Object.put("position", .{ .Integer = section_counter });
50+
section_counter += 1;
51+
52+
for (section.Object.get("lessons").?.Array.items) |*lesson| {
53+
try lesson.Object.put("position", .{ .Integer = lesson_counter });
54+
lesson_counter += 1;
55+
}
56+
}
57+
58+
try tree.root.jsonStringify(.{ .whitespace = .{} }, stdout);
59+
}

0 commit comments

Comments
 (0)