dermetfan / zig-json-decode

Create a zig type to easily decode json from a struct

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

zig-json-decode

To use, simply clone and link in your zig project

Example

// ./build.zig =====
exe.addPackagePath("zig-json-decode", "zig-json-decode/src/main.zig");

// ./src/main.zig =====
const Decodable = @import("zig-json-decode").Decodable;
const Skeleton = struct {
  key: []const u8,
  
  // json key mapping 
  const json_key: []const u8 = "oddly_named_key";
};

const FleshedType = Decodable(Skeleton);
//...
const json = 
  \\{"oddly_named_key": "test"}
;
const foo = try FleshedType.fromJson(.{}, allocator, (try parser.parse(json)).root.Object);

Features

  • Map json keys to struct fields
  • Dump objects as JSON
  • Create custom decode functions specifically tailored to skeleton structs

TODO

  • Alternative key names

About

Create a zig type to easily decode json from a struct

License:MIT License


Languages

Language:Zig 100.0%