跳转到内容

std.json.hashmap

标准库Zig 0.17.0官方原文
源码std/json/hashmap.zig

第 1 行

std.json.hashmap.Allocator

别名

第 2 行

别名目标 std.mem.Allocator

std.json.hashmap.ArrayHashMap

类型函数

第 13 行

围绕 std.array_hash_map.String 的轻量包装器,实现 jsonParse、jsonParseFromValue 和 jsonStringify。 适用于 JSON 模式中的对象具有任意数据键, 而非编译期已知结构体字段名的情况。

pub fn ArrayHashMap(comptime T: type) type

参数

T: type

字段

map: std.array_hash_map.String(T) = .empty

std.json.hashmap.ArrayHashMap.deinit

函数

第 17 行

pub fn deinit(self: *@This(), allocator: Allocator) void

参数

self: *@This()
allocator: Allocator

std.json.hashmap.ArrayHashMap.jsonParse

函数

第 21 行

pub fn jsonParse(allocator: Allocator, source: anytype, options: ParseOptions) !@This()

参数

allocator: Allocator
options: ParseOptions

std.json.hashmap.ArrayHashMap.jsonParseFromValue

函数

第 52 行

pub fn jsonParseFromValue(allocator: Allocator, source: Value, options: ParseOptions) !@This()

参数

allocator: Allocator
source: Value
options: ParseOptions

std.json.hashmap.ArrayHashMap.jsonStringify

函数

第 65 行

pub fn jsonStringify(self: @This(), jws: anytype) !void

参数

self: @This()

std.json.hashmap.innerParse

别名

第 5 行

别名目标 std.json.static.innerParse

std.json.hashmap.innerParseFromValue

别名

第 6 行

别名目标 std.json.static.innerParseFromValue

std.json.hashmap.ParseOptions

别名

第 4 行

别名目标 std.json.static.ParseOptions

std.json.hashmap.std

别名

第 1 行

别名目标 std

std.json.hashmap.Value

别名

第 7 行

别名目标 std.json.dynamic.Value

源码

std/json/hashmap.zig

const std = @import("std");
const Allocator = std.mem.Allocator;

const ParseOptions = @import("static.zig").ParseOptions;
const innerParse = @import("static.zig").innerParse;
const innerParseFromValue = @import("static.zig").innerParseFromValue;
const Value = @import("dynamic.zig").Value;

/// 围绕 `std.array_hash_map.String` 的轻量包装器,实现
/// `jsonParse`、`jsonParseFromValue` 和 `jsonStringify`。
/// 适用于 JSON 模式中的对象具有任意数据键,
/// 而非编译期已知结构体字段名的情况。
pub fn ArrayHashMap(comptime T: type) type {
    return struct {
        map: std.array_hash_map.String(T) = .empty,

        pub fn deinit(self: *@This(), allocator: Allocator) void {
            self.map.deinit(allocator);
        }

        pub fn jsonParse(allocator: Allocator, source: anytype, options: ParseOptions) !@This() {
            var map: std.array_hash_map.String(T) = .empty;
            errdefer map.deinit(allocator);

            if (.object_begin != try source.next()) return error.UnexpectedToken;
            while (true) {
                const token = try source.nextAlloc(allocator, options.allocate.?);
                switch (token) {
                    inline .string, .allocated_string => |k| {
                        const gop = try map.getOrPut(allocator, k);
                        if (gop.found_existing) {
                            switch (options.duplicate_field_behavior) {
                                .use_first => {
                                    // 解析并忽略冗余值。
                                    // 不直接跳过此值,因为需要进行类型检查。
                                    _ = try innerParse(T, allocator, source, options);
                                    continue;
                                },
                                .@"error" => return error.DuplicateField,
                                .use_last => {},
                            }
                        }
                        gop.value_ptr.* = try innerParse(T, allocator, source, options);
                    },
                    .object_end => break,
                    else => unreachable,
                }
            }
            return .{ .map = map };
        }

        pub fn jsonParseFromValue(allocator: Allocator, source: Value, options: ParseOptions) !@This() {
            if (source != .object) return error.UnexpectedToken;

            var map: std.array_hash_map.String(T) = .empty;
            errdefer map.deinit(allocator);

            var it = source.object.iterator();
            while (it.next()) |kv| {
                try map.put(allocator, kv.key_ptr.*, try innerParseFromValue(T, allocator, kv.value_ptr.*, options));
            }
            return .{ .map = map };
        }

        pub fn jsonStringify(self: @This(), jws: anytype) !void {
            try jws.beginObject();
            var it = self.map.iterator();
            while (it.next()) |kv| {
                try jws.objectField(kv.key_ptr.*);
                try jws.write(kv.value_ptr.*);
            }
            try jws.endObject();
        }
    };
}

test {
    _ = @import("hashmap_test.zig");
}