Hi, if you just want to transform some data to json, this is an example on how to do it.
const std = @import("std");
const mem = std.mem;
const heap = std.heap;
const process = std.process;
const log = std.log;
const Io = std.Io;
const json = std.json;
const fs = std.fs;
const example = @embedFile("../contacts.json");
const Contacts = struct {
name: json.ArrayHashMap(Info),
};
const Info = struct {
number: []const u8 = "",
address: []const u8 = "",
};
fn openReadToEndAlloc(allocator: mem.Allocator, io: Io, dir: Io.Dir, file_path: []const u8) ![]const u8 {
const file = try dir.openFile(io, file_path, .{ .mode = .read_only });
defer file.close(io);
const file_stat = try file.stat(io);
var file_buff: [heap.pageSize()]u8 = undefined;
var file_reader = file.reader(io, &file_buff);
const reader = &file_reader.interface;
const content = try reader.readAlloc(allocator, file_stat.size);
return content;
}
fn jsonFromFileContentLeaky(comptime T: type, allocator: mem.Allocator, content: []const u8, options: json.ParseOptions) !T {
return try json.parseFromSliceLeaky(T, allocator, content, options);
}
fn juicyMain(allocator: mem.Allocator, io: Io, argv: [][:0]u8) !void {
var arena: heap.ArenaAllocator = .init(allocator);
defer arena.deinit();
const cwd = Io.Dir.cwd();
const file_path = if (argv.len >= 2) argv[1] else return error.MissingFilePath;
const file_content = try openReadToEndAlloc(allocator, io, cwd, file_path);
defer allocator.free(file_content);
const file_json = try jsonFromFileContentLeaky(Contacts, arena.allocator(), file_content, .{});
var stdout_buffer: [128]u8 = undefined;
var stdout_writer = fs.File.stdout().writer(&stdout_buffer);
const stdout: *Io.Writer = &stdout_writer.interface;
var it = file_json.name.map.iterator();
while (it.next()) |entry| {
try stdout.print("name : {s} | number : {s} | address : {s}\n", .{ entry.key_ptr.*, entry.value_ptr.number, entry.value_ptr.address });
}
try stdout.flush();
}
pub fn main() !void {
var gpa: heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
var threaded: Io.Threaded = .init(gpa.allocator());
defer threaded.deinit();
const argv: [][:0]u8 = process.argsAlloc(gpa.allocator()) catch |err| {
return log.err("Fatal : {}", .{err});
};
defer process.argsFree(gpa.allocator(), argv);
juicyMain(gpa.allocator(), threaded.io(), argv) catch |err| {
return log.err("Fatal : {}", .{err});
};
return;
}
json example
{
"name": {
"Alice": {
"number": "123456789",
"address": "42 Rue de Zig, Paris"
},
"Bob": {
"number": "+33 6 12 34 56 78",
"address": "7 Avenue du Kernel, Lyon"
},
"Charlie": {
"number": "555-0101",
"address": "Embedded Systems Lab, Grenoble"
}
}
}
output :
bar ) zbr -- ./contacts.json
name : Alice | number : 123456789 | address : 42 Rue de Zig, Paris
name : Bob | number : +33 6 12 34 56 78 | address : 7 Avenue du Kernel, Lyon
name : Charlie | number : 555-0101 | address : Embedded Systems Lab, Grenoble
If you want to parse json at comptime, I don’t know if it’s possible at the moment (at least the memory can’t be reference at runtime i believe, so you can use it do to other comptime stuff, but not use that memory i think), but don’t quote me on that, if you need the file content at comptime you can use this const example = @embedFile("../contacts.json");