Implement parsing and object_get
This commit is contained in:
parent
0189c82ccf
commit
f418aa5097
16
src/json.zig
16
src/json.zig
|
@ -3,16 +3,13 @@ const c = @cImport({
|
||||||
});
|
});
|
||||||
const std = @import("std");
|
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 {
|
pub const Obj = struct {
|
||||||
obj: *c.json_object,
|
obj: *c.json_object,
|
||||||
|
|
||||||
// TODO: Port type attribute to a zig enum and assign it upon creation
|
// TODO: Port type attribute to a zig enum and assign it upon creation
|
||||||
|
|
||||||
///////////////////////////////////
|
///////////////////////////////////
|
||||||
// ** JSON object types creation **
|
// ** General functions **
|
||||||
/////////////////////////////////
|
/////////////////////////////////
|
||||||
|
|
||||||
pub fn newObject() Obj {
|
pub fn newObject() Obj {
|
||||||
|
@ -24,18 +21,21 @@ pub const Obj = struct {
|
||||||
pub fn newString(s: [*c]const u8) Obj {
|
pub fn newString(s: [*c]const u8) Obj {
|
||||||
return Obj{ .obj = c.json_object_new_string(s).? };
|
return Obj{ .obj = c.json_object_new_string(s).? };
|
||||||
}
|
}
|
||||||
pub const newInt = newInt32;
|
|
||||||
pub fn newInt32(i: i32) Obj {
|
pub fn newInt32(i: i32) Obj {
|
||||||
return Obj{ .obj = c.json_object_new_int(i).? };
|
return Obj{ .obj = c.json_object_new_int(i).? };
|
||||||
}
|
}
|
||||||
pub fn newInt64(i: i64) Obj {
|
pub fn newInt64(i: i64) Obj {
|
||||||
return Obj{ .obj = c.json_object_new_int64(i).? };
|
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 {
|
pub fn deinit(self: *Obj) void {
|
||||||
_ = c.json_object_put(self.obj);
|
_ = 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 {
|
pub fn toString(self: *Obj) []const u8 {
|
||||||
// TODO: Allow passing of flags as an enum like the SDL2 binding
|
// 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);
|
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.
|
// 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
|
// 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 {
|
pub fn objectAdd(self: *Obj, key: [*c]const u8, value: ?*Obj) void {
|
||||||
|
|
35
src/main.zig
35
src/main.zig
|
@ -4,27 +4,36 @@ const sqlite = @import("sqlite");
|
||||||
const Db = @import("Db.zig");
|
const Db = @import("Db.zig");
|
||||||
const Item = @import("Item.zig");
|
const Item = @import("Item.zig");
|
||||||
const Tag = @import("Tag.zig");
|
const Tag = @import("Tag.zig");
|
||||||
|
const json = @import("json.zig");
|
||||||
|
|
||||||
pub fn main() !void {
|
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 db = try Db.init();
|
||||||
|
|
||||||
var tags = [2]Item.Tag{
|
//var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||||
.{ .name = "title", .value = "clean room" },
|
//defer arena.deinit();
|
||||||
.{ .name = "task", .value = null },
|
//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();
|
defer jobj.deinit();
|
||||||
|
|
||||||
_ = alloc;
|
|
||||||
|
|
||||||
std.debug.print("{s}\n", .{jobj.toString()});
|
std.debug.print("{s}\n", .{jobj.toString()});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue