|
|
@ -4,7 +4,7 @@ pub const c = @cImport({ |
|
|
|
const std = @import("std"); |
|
|
|
|
|
|
|
const Type = enum(c.json_type) { |
|
|
|
@"null" = c.json_type_null, |
|
|
|
null = c.json_type_null, |
|
|
|
boolean = c.json_type_boolean, |
|
|
|
double = c.json_type_double, |
|
|
|
int = c.json_type_int, |
|
|
@ -36,8 +36,14 @@ pub const Obj = struct { |
|
|
|
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 newFromString(s: [*c]const u8) !Obj { |
|
|
|
const FromStringError = error{ |
|
|
|
MalformedString, |
|
|
|
}; |
|
|
|
|
|
|
|
return Obj{ |
|
|
|
.obj = c.json_tokener_parse(s) orelse return FromStringError.MalformedString, |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
pub fn deinit(self: *Obj) void { |
|
|
@ -48,7 +54,7 @@ pub const Obj = struct { |
|
|
|
// ** General functions ** |
|
|
|
///////////////////////////////// |
|
|
|
|
|
|
|
const TypeError = error { |
|
|
|
const TypeError = error{ |
|
|
|
TypeMismatch, // Tried to use a function on the wrong JSON format |
|
|
|
}; |
|
|
|
|
|
|
@ -58,7 +64,7 @@ pub const Obj = struct { |
|
|
|
} |
|
|
|
|
|
|
|
// Not *const Obj because the json_object retains ownership of the string |
|
|
|
pub fn toString(self: *Obj) []const u8 { |
|
|
|
pub fn toString(self: *const 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); |
|
|
|
} |
|
|
@ -68,7 +74,7 @@ pub const Obj = struct { |
|
|
|
///////////////////////////////// |
|
|
|
|
|
|
|
// Not *const Obj because the json_object retains ownership of the string |
|
|
|
pub fn getString(self: *Obj) []const u8 { |
|
|
|
pub fn getString(self: *const Obj) []const u8 { |
|
|
|
// TODO: Check type to not allow other types |
|
|
|
return std.mem.sliceTo(c.json_object_get_string(self.obj), 0); |
|
|
|
} |
|
|
@ -94,17 +100,17 @@ pub const Obj = struct { |
|
|
|
// Everything went alright |
|
|
|
if (obj) |ret| { |
|
|
|
return Obj{ .obj = c.json_object_get(ret).? }; |
|
|
|
} else if (obj == null and succ != 0){ |
|
|
|
} else if (obj == null and succ != 0) { |
|
|
|
// The actual value is just null |
|
|
|
return Obj{ .obj = null }; |
|
|
|
} else if (self.getType() != Type.object){ |
|
|
|
} else if (self.getType() != Type.object) { |
|
|
|
return GetError.TypeMismatch; |
|
|
|
} else { |
|
|
|
return GetError.KeyNotFound; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
pub fn objectAdd(self: *Obj, key: [*c]const u8, value: ?*Obj) void { |
|
|
|
pub fn objectAdd(self: *Obj, key: [*c]const u8, value: ?*const Obj) void { |
|
|
|
// TODO: Check type and error return |
|
|
|
|
|
|
|
// We need the json-c object or null, not the zig object |
|
|
@ -113,6 +119,10 @@ pub const Obj = struct { |
|
|
|
_ = c.json_object_object_add(self.obj, key, o); |
|
|
|
} |
|
|
|
|
|
|
|
pub fn objectAddString(self: *Obj, key: [*c]const u8, value: [*c]const u8) void { |
|
|
|
_ = c.json_object_object_add(self.obj, key, newString(value).obj); |
|
|
|
} |
|
|
|
|
|
|
|
pub fn objectLen(self: *const Obj) c_int { |
|
|
|
return c.json_object_object_length(self.obj); |
|
|
|
} |
|
|
@ -161,7 +171,7 @@ pub const Obj = struct { |
|
|
|
// ** Array functions ** |
|
|
|
///////////////////////////////// |
|
|
|
|
|
|
|
pub fn arrayAdd(self: *Obj, value: ?*Obj) void { |
|
|
|
pub fn arrayAdd(self: *Obj, value: ?*const Obj) void { |
|
|
|
// TODO: Check type and error return |
|
|
|
|
|
|
|
// We need the json-c object or null, not the zig object |
|
|
@ -173,7 +183,7 @@ pub const Obj = struct { |
|
|
|
return c.json_object_array_length(self.obj); |
|
|
|
} |
|
|
|
|
|
|
|
pub fn arrayGet(self: *Obj, idx: usize) ?Obj { |
|
|
|
pub fn arrayGet(self: *const Obj, idx: usize) ?Obj { |
|
|
|
// Sould the .? be there? |
|
|
|
if (c.json_object_array_get_idx(self.obj, idx)) |obj| { |
|
|
|
return Obj{ .obj = c.json_object_get(obj).? }; |
|
|
@ -182,12 +192,12 @@ pub const Obj = struct { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
pub fn arrayGetIterator (self: *Obj) ArrayIterator { |
|
|
|
pub fn arrayGetIterator(self: *const Obj) ArrayIterator { |
|
|
|
return ArrayIterator{ .obj = self, .idx = 0 }; |
|
|
|
} |
|
|
|
|
|
|
|
pub const ArrayIterator = struct { |
|
|
|
obj: *Obj, |
|
|
|
obj: *const Obj, |
|
|
|
idx: usize = 0, |
|
|
|
|
|
|
|
pub fn next(self: *ArrayIterator) ?Obj { |
|
|
|