From 4b2493afe0b4e528d2eb77aa51c72dfec3b5b039 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Fri, 6 Mar 2026 16:42:50 -0800 Subject: [PATCH] use std.json for the json formatting --- src/providers/openfigi.zig | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/providers/openfigi.zig b/src/providers/openfigi.zig index 75f1398..6f88600 100644 --- a/src/providers/openfigi.zig +++ b/src/providers/openfigi.zig @@ -58,18 +58,18 @@ pub fn lookupCusips( ) ![]FigiResult { if (cusips.len == 0) return try allocator.alloc(FigiResult, 0); - // Build JSON request body: [{"idType":"ID_CUSIP","idValue":"..."},...] - var body_buf: std.ArrayList(u8) = .empty; - defer body_buf.deinit(allocator); + const Job = struct { idType: []const u8, idValue: []const u8 }; - try body_buf.append(allocator, '['); + // Build jobs array + var jobs = try allocator.alloc(Job, cusips.len); + defer allocator.free(jobs); for (cusips, 0..) |cusip, i| { - if (i > 0) try body_buf.append(allocator, ','); - try body_buf.appendSlice(allocator, "{\"idType\":\"ID_CUSIP\",\"idValue\":\""); - try body_buf.appendSlice(allocator, cusip); - try body_buf.appendSlice(allocator, "\"}"); + jobs[i] = .{ .idType = "ID_CUSIP", .idValue = cusip }; } - try body_buf.append(allocator, ']'); + + // Serialize to JSON + const body = try std.fmt.allocPrint(allocator, "{f}", .{std.json.fmt(jobs, .{})}); + defer allocator.free(body); // Build headers var headers_buf: [2]std.http.Header = undefined; @@ -84,7 +84,7 @@ pub fn lookupCusips( var client = http.Client.init(allocator); defer client.deinit(); - var response = try client.post(api_url, body_buf.items, headers_buf[0..n_headers]); + var response = try client.post(api_url, body, headers_buf[0..n_headers]); defer response.deinit(); return parseResponse(allocator, response.body, cusips.len);