Implement parsing and object_get

This commit is contained in:
Dendy 2022-10-21 23:38:22 +02:00
parent 0189c82ccf
commit f418aa5097
2 changed files with 31 additions and 20 deletions

View File

@ -3,16 +3,13 @@ const c = @cImport({
});
const std = @import("std");
// NOTE: I try to be faithful to the original namings but also try to
// adapt some things to a more zig-ish style.
pub const Obj = struct {
obj: *c.json_object,
// TODO: Port type attribute to a zig enum and assign it upon creation
///////////////////////////////////
// ** JSON object types creation **
// ** General functions **
/////////////////////////////////
pub fn newObject() Obj {
@ -24,18 +21,21 @@ pub const Obj = struct {
pub fn newString(s: [*c]const u8) Obj {
return Obj{ .obj = c.json_object_new_string(s).? };
}
pub const newInt = newInt32;
pub fn newInt32(i: i32) Obj {
return Obj{ .obj = c.json_object_new_int(i).? };
}
pub fn newInt64(i: i64) Obj {
return Obj{ .obj = c.json_object_new_int64(i).? };
}
pub fn newFromString(s: [*c]const u8) Obj {
return Obj{ .obj = c.json_tokener_parse(s).? };
}
pub fn deinit(self: *Obj) void {
_ = c.json_object_put(self.obj);
}
// Not *const Obj because the json_object retains ownership of the string
pub fn toString(self: *Obj) []const u8 {
// TODO: Allow passing of flags as an enum like the SDL2 binding
return std.mem.sliceTo(c.json_object_to_json_string(self.obj), 0);
@ -47,9 +47,11 @@ pub const Obj = struct {
// TODO: Create an error for type checking.
pub fn objectGet(self: *Obj, key: [*c]const u8, value: **Obj) void {
pub fn objectGet(self: *const Obj, key: [*c]const u8) Obj {
// TODO: Check type and null return as errors
_ = c.json_object_object_get_ex(self.obj, key, value);
var obj: ?*c.json_object = undefined;
_ = c.json_object_object_get_ex(self.obj, key, &obj);
return Obj{ .obj = c.json_object_get(obj).? };
}
pub fn objectAdd(self: *Obj, key: [*c]const u8, value: ?*Obj) void {

View File

@ -4,27 +4,36 @@ const sqlite = @import("sqlite");
const Db = @import("Db.zig");
const Item = @import("Item.zig");
const Tag = @import("Tag.zig");
const json = @import("json.zig");
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const alloc = arena.allocator();
var db = try Db.init();
var tags = [2]Item.Tag{
.{ .name = "title", .value = "clean room" },
.{ .name = "task", .value = null },
};
//var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
//defer arena.deinit();
//const alloc = arena.allocator();
var item = Item{ .id = null, .tags = &tags };
const jsonText: [:0]const u8 =
\\{
\\ "tags": {
\\ "title": "clean room",
\\ "task": null
\\ }
\\}
;
try item.persist(&db);
var jsonItem = json.Obj.newFromString(jsonText);
defer jsonItem.deinit();
var jobj = item.toJson();
_ = db;
//var item = Item.fromJson(jsonItem);
//try item.persist(&db);
//var jobj = item.toJson();
var jobj = jsonItem.objectGet("tags");
defer jobj.deinit();
_ = alloc;
std.debug.print("{s}\n", .{jobj.toString()});
}