Compare commits

..

2 Commits

Author SHA1 Message Date
b31820d771 Move indent_level into JSONFormat 2024-11-04 15:12:58 -07:00
b060ca9715 Remove old file 2024-11-04 12:56:39 -07:00
3 changed files with 64 additions and 131 deletions

View File

@ -25,30 +25,40 @@ pub const JSONType = enum {
};
pub const JSONFormat = struct {
indent_level: usize = 4,
indent_char: u8 = ' ',
indent_size: usize = 4,
indent_level: u8 = 0,
one_line: bool = false,
fn indent(self: *const JSONFormat) JSONFormat {
return JSONFormat{
.indent_char = self.indent_char,
.indent_size = self.indent_size,
.indent_level = self.indent_level + 1,
.one_line = self.one_line,
};
}
};
fn printIndent(writer: std.fs.File.Writer, format: JSONFormat, level: usize) !void {
fn printIndent(writer: std.fs.File.Writer, format: JSONFormat) !void {
if (!format.one_line) {
try writer.writeByte('\n');
}
for (0..level) |_| {
try writer.writeByteNTimes(format.indent_char, format.indent_level);
for (0..format.indent_level) |_| {
try writer.writeByteNTimes(format.indent_char, format.indent_size);
}
}
fn indentString(allocator: std.mem.Allocator, format: JSONFormat, level: usize) ![]const u8 {
fn indentString(allocator: std.mem.Allocator, format: JSONFormat) ![]const u8 {
var str = std.ArrayList(u8).init(allocator);
if (!format.one_line) {
try str.append('\n');
}
for (0..level) |_| {
try str.appendNTimes(format.indent_char, format.indent_level);
for (0..format.indent_level) |_| {
try str.appendNTimes(format.indent_char, format.indent_size);
}
return try str.toOwnedSlice();
@ -72,7 +82,7 @@ pub const JSONObject = struct {
return self.children.get(name);
}
pub fn write(self: *JSONObject, writer: std.fs.File.Writer, format: JSONFormat, indent_level: usize) std.fs.File.WriteError!void {
pub fn write(self: *JSONObject, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void {
try writer.writeByte('{');
var iter = self.children.keyIterator();
@ -82,18 +92,18 @@ pub const JSONObject = struct {
try writer.writeAll(", ");
}
try printIndent(writer, format, indent_level + 1);
try printIndent(writer, format.indent());
try writer.print("\"{s}\": ", .{key.*});
try self.children.get(key.*).?.print(writer, format, indent_level + 1);
try self.children.get(key.*).?.print(writer, format.indent());
first = false;
}
try printIndent(writer, format, indent_level);
try printIndent(writer, format);
try writer.writeByte('}');
}
pub fn toString(self: *JSONObject, allocator: std.mem.Allocator, format: JSONFormat, indent_level: usize) std.mem.Allocator.Error![]const u8 {
pub fn toString(self: *JSONObject, allocator: std.mem.Allocator, format: JSONFormat) std.mem.Allocator.Error![]const u8 {
var str = std.ArrayList(u8).init(allocator);
try str.append('{');
@ -104,18 +114,18 @@ pub const JSONObject = struct {
try str.appendSlice(", ");
}
try str.appendSlice(try indentString(allocator, format, indent_level + 1));
try str.appendSlice(try indentString(allocator, format.indent()));
try str.append('"');
try str.appendSlice(key.*);
try str.appendSlice("\": ");
const valstr = try self.children.get(key.*).?.toString(allocator, format, indent_level + 1);
const valstr = try self.children.get(key.*).?.toString(allocator, format.indent());
defer allocator.free(valstr);
try str.appendSlice(valstr);
first = false;
}
try str.appendSlice(try indentString(allocator, format, indent_level));
try str.appendSlice(try indentString(allocator, format));
try str.append('}');
return try str.toOwnedSlice();
@ -152,8 +162,8 @@ pub const JSONValue = struct {
deinit: *const fn (self: *JSONValue) void,
print: *const fn (self: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat, indent_level: usize) std.fs.File.WriteError!void,
toString: *const fn (self: *const JSONValue, allocator: std.mem.Allocator, format: JSONFormat, indent_level: usize) std.mem.Allocator.Error![]const u8,
print: *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,
};
fn incorrectValue(comptime T: type) *const fn (*const JSONValue) JSONError!T {
@ -188,12 +198,12 @@ pub const JSONValue = struct {
return self.vtable.getObject(self);
}
pub fn print(self: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat, indent_level: usize) std.fs.File.WriteError!void {
return self.vtable.print(self, writer, format, indent_level);
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 toString(self: *const JSONValue, allocator: std.mem.Allocator, format: JSONFormat, indent_level: usize) std.mem.Allocator.Error![]const u8 {
return self.vtable.toString(self, allocator, format, indent_level);
pub fn toString(self: *const JSONValue, allocator: std.mem.Allocator, format: JSONFormat) std.mem.Allocator.Error![]const u8 {
return self.vtable.toString(self, allocator, format);
}
pub fn deinit(self: *JSONValue) void {
@ -209,11 +219,11 @@ pub const JSONValue = struct {
return self;
}
fn printNull(_: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat, _: usize) std.fs.File.WriteError!void {
fn printNull(_: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void {
return writer.writeAll("null");
}
fn toStringNull(_: *const JSONValue, allocator: std.mem.Allocator, _: JSONFormat, _: usize) ![]const u8 {
fn toStringNull(_: *const JSONValue, allocator: std.mem.Allocator, _: JSONFormat) ![]const u8 {
return allocator.dupe(u8, "null");
}
@ -243,7 +253,7 @@ pub const JSONArrayValue = struct {
return self.array;
}
fn print(value: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat, indent_level: usize) std.fs.File.WriteError!void {
fn print(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("[ ");
@ -254,14 +264,14 @@ pub const JSONArrayValue = struct {
try writer.writeAll(", ");
}
try val.print(writer, format, indent_level);
try val.print(writer, format);
first = false;
}
try writer.writeAll(" ]");
}
fn toString(value: *const JSONValue, allocator: std.mem.Allocator, format: JSONFormat, indent_level: usize) std.mem.Allocator.Error![]const u8 {
fn toString(value: *const JSONValue, allocator: std.mem.Allocator, format: JSONFormat) std.mem.Allocator.Error![]const u8 {
const self: *const JSONArrayValue = @fieldParentPtr("value", value);
var str = std.ArrayList(u8).init(allocator);
@ -273,7 +283,7 @@ pub const JSONArrayValue = struct {
try str.appendSlice(", ");
}
const valstr = try val.toString(allocator, format, indent_level);
const valstr = try val.toString(allocator, format);
defer allocator.free(valstr);
try str.appendSlice(valstr);
first = false;
@ -315,14 +325,14 @@ pub const JSONObjectValue = struct {
return self.object;
}
fn print(value: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat, indent_level: usize) std.fs.File.WriteError!void {
fn print(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, indent_level);
try self.object.write(writer, format);
}
fn toString(value: *const JSONValue, allocator: std.mem.Allocator, format: JSONFormat, indent_level: usize) std.mem.Allocator.Error![]const u8 {
fn toString(value: *const JSONValue, allocator: std.mem.Allocator, format: JSONFormat) std.mem.Allocator.Error![]const u8 {
const self: *const JSONObjectValue = @fieldParentPtr("value", value);
return self.object.toString(allocator, format, indent_level);
return self.object.toString(allocator, format);
}
fn deinit(value: *JSONValue) void {
@ -353,12 +363,12 @@ pub const JSONIntValue = struct {
return self.int;
}
fn print(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat, _: usize) std.fs.File.WriteError!void {
fn print(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});
}
fn toString(value: *const JSONValue, allocator: std.mem.Allocator, _: JSONFormat, _: usize) std.mem.Allocator.Error![]const u8 {
fn toString(value: *const JSONValue, allocator: std.mem.Allocator, _: JSONFormat) std.mem.Allocator.Error![]const u8 {
const self: *const JSONIntValue = @fieldParentPtr("value", value);
return std.fmt.allocPrint(allocator, "{d}", .{self.int});
}
@ -390,12 +400,12 @@ pub const JSONFloatValue = struct {
return self.float;
}
fn print(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat, _: usize) std.fs.File.WriteError!void {
fn print(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});
}
fn toString(value: *const JSONValue, allocator: std.mem.Allocator, _: JSONFormat, _: usize) std.mem.Allocator.Error![]const u8 {
fn toString(value: *const JSONValue, allocator: std.mem.Allocator, _: JSONFormat) std.mem.Allocator.Error![]const u8 {
const self: *const JSONFloatValue = @fieldParentPtr("value", value);
return std.fmt.allocPrint(allocator, "{d}", .{self.float});
}
@ -427,7 +437,7 @@ pub const JSONBoolValue = struct {
return self.boolean;
}
fn print(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat, _: usize) std.fs.File.WriteError!void {
fn print(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");
@ -436,7 +446,7 @@ pub const JSONBoolValue = struct {
}
}
fn toString(value: *const JSONValue, allocator: std.mem.Allocator, _: JSONFormat, _: usize) std.mem.Allocator.Error![]const u8 {
fn toString(value: *const JSONValue, allocator: std.mem.Allocator, _: JSONFormat) std.mem.Allocator.Error![]const u8 {
const self: *const JSONBoolValue = @fieldParentPtr("value", value);
if (self.boolean) {
return try allocator.dupe(u8, "true");
@ -472,12 +482,12 @@ pub const JSONStringValue = struct {
return self.string;
}
fn print(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat, _: usize) std.fs.File.WriteError!void {
fn print(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});
}
fn toString(value: *const JSONValue, allocator: std.mem.Allocator, _: JSONFormat, _: usize) std.mem.Allocator.Error![]const u8 {
fn toString(value: *const JSONValue, allocator: std.mem.Allocator, _: JSONFormat) std.mem.Allocator.Error![]const u8 {
const self: *const JSONStringValue = @fieldParentPtr("value", value);
return std.fmt.allocPrint(allocator, "\"{s}\"", .{self.string});
}

View File

@ -40,106 +40,38 @@ pub fn main() void {
};
//defer root.deinit();
printObject(root, 0);
std.debug.print("\n", .{});
const stdout = std.io.getStdOut();
const writer = stdout.writer();
root.write(writer, .{}, 0) catch {};
root.write(writer, .{}) catch {};
std.debug.print("\n", .{});
const json_str = root.toString(allocator, .{}, 0) catch return;
const json_str = root.toString(allocator, .{}) catch return;
std.debug.print("{s}\n", .{json_str});
var result = json.JSONtoNewStruct(root, TestStruct) catch |err| {
std.debug.print("Failed to convert JSON to struct: {any}\n", .{err});
return;
};
std.debug.print("hello: {s}\n", .{result.hello});
std.debug.print("subitem: {d}\n", .{result.item2.subitem});
std.debug.print("otheritem: {d}\n", .{result.item2.otheritem});
std.debug.print("subsubitem: {any}\n", .{result.item2.subobject.subsubitem});
std.debug.print("array: {any}\n", .{result.array});
std.debug.print("online: {}\n", .{result.online});
std.debug.print("active: {}\n", .{result.active});
std.debug.print("null: {?}\n", .{result.null});
result.hello = "potato";
result.item2.subitem = -25;
result.item2.otheritem = 224.34567;
result.null = 25;
result.online = false;
result.active = true;
result.array[3] = 987654321;
result.item2.subobject.subsubitem[3] = "Hello :P";
const back2JSON = json.structToJSON(allocator, result) catch |err| {
std.debug.print("Failed to convert struct back to JSON: {any}\n", .{err});
return;
};
back2JSON.write(writer, .{}, 0) catch return;
}
fn printIndent(indent: usize) void {
for (0..indent) |_| {
std.debug.print(" ", .{});
}
}
fn printObject(obj: *json.JSONObject, indent: usize) void {
std.debug.print("{{\n", .{});
var first = true;
var iter = obj.children.keyIterator();
while (iter.next()) |key| {
if (!first) {
std.debug.print(",\n", .{});
}
printIndent(indent + 1);
std.debug.print("\"{s}\": ", .{key.*});
printValue(obj.get(key.*).?, indent + 1);
first = false;
}
back2JSON.write(writer, .{}) catch return;
std.debug.print("\n", .{});
printIndent(indent);
std.debug.print("}}", .{});
}
fn printArray(arr: []*json.JSONValue, indent: usize) void {
std.debug.print("[ ", .{});
var first = true;
for (arr) |value| {
if (!first) {
std.debug.print(", ", .{});
}
printValue(value, indent + 1);
first = false;
}
std.debug.print(" ]", .{});
}
fn printString(string: []const u8) void {
std.debug.print("\"{s}\"", .{string});
}
fn printBool(b: bool) void {
std.debug.print("{s}", .{if (b) "true" else "false"});
}
fn printInt(int: isize) void {
std.debug.print("{d}", .{int});
}
fn printFloat(float: f64) void {
std.debug.print("{d}", .{float});
}
fn printValue(value: *json.JSONValue, indent: usize) void {
switch (value.type) {
.Object => printObject(value.getObject() catch unreachable, indent),
.Array => printArray(value.getArray() catch unreachable, indent),
.String => printString(value.getString() catch unreachable),
.Bool => printBool(value.getBool() catch unreachable),
.Int => printInt(value.getInt() catch unreachable),
.Float => printFloat(value.getFloat() catch unreachable),
.Null => std.debug.print("null", .{}),
}
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;
}

View File

@ -1,9 +0,0 @@
const json = @import("json.zig");
pub const JSONError = json.JSONError;
pub const JSONType = json.JSONType;
pub const JSONObject = json.JSONObject;
pub const JSONValue = json.JSONValue;
pub const JSONStringValue = json.JSONStringValue;
pub const parseFile = json.parseFile;
pub const parseString = json.parseString;