Skip to content

Commit

Permalink
♻️ Refactor JSON handling for efficiency and simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
segersniels committed Apr 5, 2024
1 parent f9327b8 commit b9ff2f2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
9 changes: 3 additions & 6 deletions apps/cli/src/emoji.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ fn fetchFromCache(allocator: std.mem.Allocator) !?Response {

const stat = try file.stat();
const content = try file.readToEndAlloc(allocator, stat.size);
const data = try std.json.parseFromSlice(Response, allocator, content, .{ .allocate = .alloc_always, .ignore_unknown_fields = true });

return data.value;
return try std.json.parseFromSliceLeaky(Response, allocator, content, .{ .allocate = .alloc_always, .ignore_unknown_fields = true });
}

fn writeToCache(allocator: std.mem.Allocator, data: Response) !void {
Expand All @@ -61,10 +60,8 @@ fn writeToCache(allocator: std.mem.Allocator, data: Response) !void {
const file = try std.fs.createFileAbsolute(path, .{ .truncate = true });
defer file.close();

var string = std.ArrayList(u8).init(allocator);
try std.json.stringify(data, .{}, string.writer());

try file.writeAll(string.items);
const string = try std.json.stringifyAlloc(allocator, data, .{});
try file.writeAll(string);
}

fn fetchFromRemote(allocator: std.mem.Allocator) !Response {
Expand Down
4 changes: 1 addition & 3 deletions apps/cli/src/utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ pub fn fetch(comptime T: type, url: []const u8, options: InitOptions) !T {
var body = std.ArrayList(u8).init(options.allocator);
_ = try client.fetch(.{ .location = .{ .url = url }, .method = options.method, .headers = headers, .response_storage = .{ .dynamic = &body }, .payload = options.body });

const data = try std.json.parseFromSlice(T, options.allocator, body.items, .{ .allocate = .alloc_always, .ignore_unknown_fields = true });

return data.value;
return try std.json.parseFromSliceLeaky(T, options.allocator, body.items, .{ .allocate = .alloc_always, .ignore_unknown_fields = true });
}

0 comments on commit b9ff2f2

Please sign in to comment.