Skip Navigation
Zig Programming Language @lemm.ee cryptocode @lemm.ee

[New library] json-schema-gen: Generate zig code from arbitrary json which can parse it

github.com GitHub - travisstaloch/json-schema-gen: Generate zig code from arbitrary json which can parse it

Generate zig code from arbitrary json which can parse it - travisstaloch/json-schema-gen

GitHub - travisstaloch/json-schema-gen: Generate zig code from arbitrary json which can parse it

From the README:

Generate zig code from arbitrary json data which can parse it.

Depends on python3 - tested with Python 3.10.12

Motivation

When you need to parse arbitrary json in zig, you usually pass std.json.Value to one of the parse() methods. This is convenient but is generally slower and allocates more memory than passing a concrete type.

Also, std.json.Value can be a little akward at times. Here is how it looks to access data from the github api

with std.json.Value:

const url = parsed.value.object.get("items").?
    .array.items[0].object.get("commits_url").?.string;

with generated schema:

const url = parsed.value.items[0].commits_url;

This library was developed after struggling with code generated by https://aerth.github.io/json-to-zig/. Hopefully std.json gets diagnostics soon cause it can be difficult to tell which fields were causing parse errors.

0
0 comments