Find Related products on Amazon

Shop on Amazon

Abusing C to implement JSON parsing with struct methods

Published on: 2025-07-08 12:53:20

Idea Build a JSON parser in c Instead of using by itself functions: attach functions to a struct and use these as methods make it C issue family free (segfaults, leaks, stack overflows, etc…) provide an ergonomic API Usage C 1 #include "json.h" 2 #include 3 4 int main ( void ) { 5 struct json json = json_new ( JSON ({ 6 "object" : {}, 7 "array" : [[]], 8 "atoms" : [ "string" , 0.1 , true , false , null ] 9 })); 10 struct json_value json_value = json. parse ( & json); 11 json_print_value ( & json_value); 12 puts ( "" ); 13 json_free_value ( & json_value); 14 return EXIT_SUCCESS; 15 } Tip - Compiling C projects the easy way Don’t take this as a guide for using make, in my projects I just use it as a command runner. Compiler flags These flags can be specific to gcc , I use gcc (GCC) 14.2.1 20250207 , so take this with a grain of salt. I use these flags in almost every c project I ever started. SH 1 gcc -std = c23 \ 2 -O2 \ 3 -Wall \ 4 -Wextra \ 5 -Werror \ 6 -fdiagnostics- ... Read full article.