From ee7b2eeb753985b99848fab324f03679956d17e2 Mon Sep 17 00:00:00 2001 From: Dendy Date: Thu, 27 Oct 2022 03:33:47 +0200 Subject: [PATCH] Add subentry to return json --- src/main.zig | 1 + src/request.zig | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main.zig b/src/main.zig index 9ef2114..f235783 100644 --- a/src/main.zig +++ b/src/main.zig @@ -37,6 +37,7 @@ pub fn main() !void { ; var jobj = json.Obj.newFromString(jsonText); + defer jobj.deinit(); try request.process(&jobj, &db); } diff --git a/src/request.zig b/src/request.zig index 8c6ef22..8765298 100644 --- a/src/request.zig +++ b/src/request.zig @@ -11,17 +11,23 @@ pub fn process(jobj: *json.Obj, db: *sqlite.Db) !void { defer arena.deinit(); const allocator = arena.allocator(); + var jret = json.Obj.newObject(); + defer jret.deinit(); + // Test the action to carry and pass the object if (jobj.objectGet("add") catch null) |*jadd| { - try add(jadd, db, allocator); + var ret = try add(jadd, db, allocator); + jret.objectAdd("added", &ret); } + + std.debug.print("{s}", .{ jret.toString() }); } -pub fn add(jobj: *json.Obj, db: *sqlite.Db, allocator: std.mem.Allocator) !void { +pub fn add(jobj: *json.Obj, db: *sqlite.Db, allocator: std.mem.Allocator) !json.Obj { // TODO: Maybe return error when no items in the array? + // Freed by the caller var jret = json.Obj.newArray(); - defer jret.deinit(); var iter = jobj.arrayGetIterator(); while(iter.next()) |*jtags| { @@ -38,5 +44,5 @@ pub fn add(jobj: *json.Obj, db: *sqlite.Db, allocator: std.mem.Allocator) !void jret.arrayAdd(&item.toJson()); } - std.debug.print("{s}", .{ jret.toString() }); + return jret; }