const std = @import("std");

/// Get a string value from a JSON object map
pub fn getString(obj: std.json.ObjectMap, key: []const u8) ?[]const u8 {
    const v = obj.get(key) orelse return null;
    if (v != .string) return null;
    return v.string;
}

/// Get an integer value from a JSON object map
pub fn getInt(obj: std.json.ObjectMap, key: []const u8) ?i64 {
    const v = obj.get(key) orelse return null;
    switch (v) {
        .integer => |i| return i,
        .float => |f| return @intFromFloat(f),
        else => return null,
    }
}

/// Get a float value from a JSON object map
pub fn getFloat(obj: std.json.ObjectMap, key: []const u8) ?f64 {
    const v = obj.get(key) orelse return null;
    switch (v) {
        .float => |f| return f,
        .integer => |i| return @floatFromInt(i),
        else => return null,
    }
}

/// Get a boolean value from a JSON object map
pub fn getBool(obj: std.json.ObjectMap, key: []const u8) ?bool {
    const v = obj.get(key) orelse return null;
    if (v != .bool) return null;
    return v.bool;
}
