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.
This commit is contained in:
Jeremie Fraeys 2026-03-04 21:07:04 -05:00
parent 00ffeb93c8
commit cf7e82c758
No known key found for this signature in database
2 changed files with 45 additions and 34 deletions

View file

@ -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;
}

View file

@ -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;