aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/jv/parse.zig
blob: 3d90be574635b1b02d72e2348c00ad37d8815e4b (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const std = @import("std");
const Value = @import("./value.zig").Value;
const Array = @import("./value.zig").Array;
const Object = @import("./value.zig").Object;

pub const Parsed = struct {
    value: Value,
    arena: *std.heap.ArenaAllocator,
    allocator: std.mem.Allocator,

    pub fn deinit(self: *const Parsed) void {
        self.value.deinit(self.allocator);
        const arena = self.arena;
        const allocator = self.allocator;
        arena.deinit();
        allocator.destroy(arena);
    }
};

pub fn parse(allocator: std.mem.Allocator, input: []const u8) !Parsed {
    const internal = try std.json.parseFromSlice(std.json.Value, allocator, input, .{});
    const value = try convertValue(allocator, internal.value);
    return .{
        .value = value,
        .arena = internal.arena,
        .allocator = allocator,
    };
}

fn convertValue(allocator: std.mem.Allocator, v: std.json.Value) !Value {
    return switch (v) {
        .null => Value.null,
        .bool => |b| Value.initBool(b),
        .integer => |i| Value.initInteger(i),
        .float => |f| Value.initFloat(f),
        .string => |s| Value.initString(s),
        .array => |a| blk: {
            var arr = try Array.init(allocator);
            for (a.items) |item| {
                try arr.append(allocator, try convertValue(allocator, item));
            }
            break :blk Value.initArray(arr);
        },
        .object => |o| blk: {
            var obj = try Object.init(allocator);
            var it = o.iterator();
            while (it.next()) |entry| {
                try obj.set(allocator, entry.key_ptr.*, try convertValue(allocator, entry.value_ptr.*));
            }
            break :blk Value.initObject(obj);
        },
        .number_string => unreachable,
    };
}