Add subentry to return json

This commit is contained in:
Dendy 2022-10-27 03:33:47 +02:00
parent dc527e93de
commit ee7b2eeb75
2 changed files with 11 additions and 4 deletions

View File

@ -37,6 +37,7 @@ pub fn main() !void {
; ;
var jobj = json.Obj.newFromString(jsonText); var jobj = json.Obj.newFromString(jsonText);
defer jobj.deinit();
try request.process(&jobj, &db); try request.process(&jobj, &db);
} }

View File

@ -11,17 +11,23 @@ pub fn process(jobj: *json.Obj, db: *sqlite.Db) !void {
defer arena.deinit(); defer arena.deinit();
const allocator = arena.allocator(); const allocator = arena.allocator();
var jret = json.Obj.newObject();
defer jret.deinit();
// Test the action to carry and pass the object // Test the action to carry and pass the object
if (jobj.objectGet("add") catch null) |*jadd| { 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? // TODO: Maybe return error when no items in the array?
// Freed by the caller
var jret = json.Obj.newArray(); var jret = json.Obj.newArray();
defer jret.deinit();
var iter = jobj.arrayGetIterator(); var iter = jobj.arrayGetIterator();
while(iter.next()) |*jtags| { 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()); jret.arrayAdd(&item.toJson());
} }
std.debug.print("{s}", .{ jret.toString() }); return jret;
} }