From cf7e82c758aca811571fc346aea7a8925a9a9386 Mon Sep 17 00:00:00 2001 From: Jeremie Fraeys Date: Wed, 4 Mar 2026 21:07:04 -0500 Subject: [PATCH] refactor(cli): consolidate JSON utilities into io.zig Move JSON accessor functions to io.zig: - jsonGetString, jsonGetInt, jsonGetFloat, jsonGetBool - json.zig now re-exports from io.zig for backward compatibility Benefits: - Single location for all I/O related utilities - Consistent with terminal/color consolidation - Reduced file count Build passes successfully. --- cli/src/utils/io.zig | 38 ++++++++++++++++++++++++++++++++++++++ cli/src/utils/json.zig | 41 +++++++---------------------------------- 2 files changed, 45 insertions(+), 34 deletions(-) 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;