diff --git a/src/json.zig b/src/json.zig index f36a607..8aee181 100644 --- a/src/json.zig +++ b/src/json.zig @@ -40,7 +40,7 @@ pub const JSONFormat = struct { } }; -fn printIndent(writer: std.fs.File.Writer, format: JSONFormat) !void { +fn writeIndent(writer: std.fs.File.Writer, format: JSONFormat) !void { if (!format.one_line) { try writer.writeByte('\n'); } @@ -92,14 +92,14 @@ pub const JSONObject = struct { try writer.writeAll(", "); } - try printIndent(writer, format.indent()); + try writeIndent(writer, format.indent()); try writer.print("\"{s}\": ", .{key.*}); - try self.children.get(key.*).?.print(writer, format.indent()); + try self.children.get(key.*).?.write(writer, format.indent()); first = false; } - try printIndent(writer, format); + try writeIndent(writer, format); try writer.writeByte('}'); } @@ -162,7 +162,7 @@ pub const JSONValue = struct { deinit: *const fn (self: *JSONValue) void, - print: *const fn (self: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void, + write: *const fn (self: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void, toString: *const fn (self: *const JSONValue, allocator: std.mem.Allocator, format: JSONFormat) std.mem.Allocator.Error![]const u8, }; @@ -198,8 +198,8 @@ pub const JSONValue = struct { return self.vtable.getObject(self); } - pub fn print(self: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void { - return self.vtable.print(self, writer, format); + pub fn write(self: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void { + return self.vtable.write(self, writer, format); } pub fn toString(self: *const JSONValue, allocator: std.mem.Allocator, format: JSONFormat) std.mem.Allocator.Error![]const u8 { @@ -213,13 +213,13 @@ pub const JSONValue = struct { pub fn createNull(allocator: std.mem.Allocator) JSONError!*JSONValue { var self = try allocator.create(JSONValue); self.type = .Null; - self.vtable = .{ .deinit = basicDeinit, .print = printNull, .toString = toStringNull }; + self.vtable = .{ .deinit = basicDeinit, .write = writeNull, .toString = toStringNull }; self.allocator = allocator; return self; } - fn printNull(_: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { + fn writeNull(_: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { return writer.writeAll("null"); } @@ -241,7 +241,7 @@ pub const JSONArrayValue = struct { self.array = array; self.value = .{ .type = .Array, - .vtable = .{ .getArray = getValue, .print = print, .toString = toString, .deinit = deinit }, + .vtable = .{ .getArray = getValue, .write = write, .toString = toString, .deinit = deinit }, .allocator = allocator, }; @@ -253,7 +253,7 @@ pub const JSONArrayValue = struct { return self.array; } - fn print(value: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void { + fn write(value: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void { const self: *const JSONArrayValue = @fieldParentPtr("value", value); try writer.writeAll("[ "); @@ -264,7 +264,7 @@ pub const JSONArrayValue = struct { try writer.writeAll(", "); } - try val.print(writer, format); + try val.write(writer, format); first = false; } @@ -313,7 +313,7 @@ pub const JSONObjectValue = struct { self.object = object; self.value = .{ .type = .Object, - .vtable = .{ .getObject = getValue, .print = print, .toString = toString, .deinit = deinit }, + .vtable = .{ .getObject = getValue, .write = write, .toString = toString, .deinit = deinit }, .allocator = allocator, }; @@ -325,7 +325,7 @@ pub const JSONObjectValue = struct { return self.object; } - fn print(value: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void { + fn write(value: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void { const self: *const JSONObjectValue = @fieldParentPtr("value", value); try self.object.write(writer, format); } @@ -351,7 +351,7 @@ pub const JSONIntValue = struct { self.int = value; self.value = .{ .type = .Int, - .vtable = .{ .getInt = getValue, .print = print, .toString = toString, .deinit = deinit }, + .vtable = .{ .getInt = getValue, .write = write, .toString = toString, .deinit = deinit }, .allocator = allocator, }; @@ -363,7 +363,7 @@ pub const JSONIntValue = struct { return self.int; } - fn print(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { + fn write(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { const self: *const JSONIntValue = @fieldParentPtr("value", value); try writer.print("{d}", .{self.int}); } @@ -388,7 +388,7 @@ pub const JSONFloatValue = struct { self.float = value; self.value = .{ .type = .Float, - .vtable = .{ .getFloat = getValue, .print = print, .toString = toString, .deinit = deinit }, + .vtable = .{ .getFloat = getValue, .write = write, .toString = toString, .deinit = deinit }, .allocator = allocator, }; @@ -400,7 +400,7 @@ pub const JSONFloatValue = struct { return self.float; } - fn print(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { + fn write(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { const self: *const JSONFloatValue = @fieldParentPtr("value", value); try writer.print("{d}", .{self.float}); } @@ -425,7 +425,7 @@ pub const JSONBoolValue = struct { self.boolean = value; self.value = .{ .type = .Bool, - .vtable = .{ .getBool = getValue, .print = print, .toString = toString, .deinit = deinit }, + .vtable = .{ .getBool = getValue, .write = write, .toString = toString, .deinit = deinit }, .allocator = allocator, }; @@ -437,7 +437,7 @@ pub const JSONBoolValue = struct { return self.boolean; } - fn print(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { + fn write(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { const self: *const JSONBoolValue = @fieldParentPtr("value", value); if (self.boolean) { try writer.writeAll("true"); @@ -470,7 +470,7 @@ pub const JSONStringValue = struct { self.string = string; self.value = .{ .type = .String, - .vtable = .{ .getString = getValue, .print = print, .toString = toString, .deinit = deinit }, + .vtable = .{ .getString = getValue, .write = write, .toString = toString, .deinit = deinit }, .allocator = allocator, }; @@ -482,7 +482,7 @@ pub const JSONStringValue = struct { return self.string; } - fn print(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { + fn write(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { const self: *const JSONStringValue = @fieldParentPtr("value", value); try writer.print("\"{s}\"", .{self.string}); } @@ -529,7 +529,7 @@ pub fn writeToFile(fileName: []const u8, json: *JSONObject, format: JSONFormat) defer file.close(); const writer = file.writer(); - return json.write(writer, format, 0); + return json.write(writer, format); } const JSONParser = struct { diff --git a/src/main.zig b/src/main.zig index 22ed4d3..14c57f1 100644 --- a/src/main.zig +++ b/src/main.zig @@ -68,10 +68,4 @@ pub fn main() void { }; back2JSON.write(writer, .{}) catch return; std.debug.print("\n", .{}); - - const rootJSON = json.structToJSON(allocator, json.JSONFormat{}) catch |err| { - std.debug.print("Failed to convert root to JSON: {any}\n", .{err}); - return; - }; - rootJSON.write(writer, .{}) catch return; }