diff --git a/cli/src/utils/io.zig b/cli/src/utils/io.zig index bc33d2b..4b7d698 100644 --- a/cli/src/utils/io.zig +++ b/cli/src/utils/io.zig @@ -205,3 +205,41 @@ fn writeJSONString(writer: anytype, s: []const u8) !void { fn hexDigit(v: u8) u8 { return if (v < 10) ('0' + v) else ('a' + (v - 10)); } + +// ============================================================================ +// JSON Object Accessor Utilities +// ============================================================================ + +/// Get a string value from a JSON object map +pub fn jsonGetString(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 jsonGetInt(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 jsonGetFloat(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 jsonGetBool(obj: std.json.ObjectMap, key: []const u8) ?bool { + const v = obj.get(key) orelse return null; + if (v != .bool) return null; + return v.bool; +} diff --git a/cli/src/utils/json.zig b/cli/src/utils/json.zig index 5b3699c..0ef719b 100644 --- a/cli/src/utils/json.zig +++ b/cli/src/utils/json.zig @@ -1,35 +1,8 @@ -const std = @import("std"); +/// Re-exports from io.zig for backward compatibility +/// Use @import("io.zig") directly for new code +const io = @import("io.zig"); -/// 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; -} +pub const getString = io.jsonGetString; +pub const getInt = io.jsonGetInt; +pub const getFloat = io.jsonGetFloat; +pub const getBool = io.jsonGetBool;