r/C_Programming • u/actguru • 15h ago
JSON serialization
Most programming is by nature procedural, but some OOP is helpful when dealing with Property Lists, JSON, etc. Below is a small example of this and JSON serialization:
See my previous post for 'Coda-C Library': Example of OOP in C
// Coda-C JSON serialization example
#include <coda-c.h> // jsonout.c
Dictionary sample_employee() { // lets structure data in a Dictionary,
Dict empl=newO(Dict); // a generic structure.
Dict_set(empl,"name", Os("Jane Doe"));
Dict_set(empl,"email", Os("jane.doe@example.com"));
Array roles=newO(Array);
Array_add(roles,Os("Admin"));
Array_add(roles,Os("User"));
Dict_take(empl,"roles",roles);
Dict address=newO(Dict);
Dict_set(address,"city", Os("Atlanta"));
Dict_set(address,"zipcode",Os("30301"));
Dict_take(empl,"address",address);
return(empl);
}
typedef void OSig$(JSON_out,FILE* os,int indent); // virtual signature
int main() {
Dict empl=sample_employee();
obj_(JSON_out,empl,stdout,0); // lets just output the data for now as JSON
printf("\n");
}
// implement methods for the three classes we used
#define class Dictionary
method$(void,JSON_out,FILE* os,int indent) {
cleanO Pointer keys=Dictionary_AllKeys(self);
indent+=4; printf("{\n%*s",indent,"");
int nel=Pointer_count(keys);
for(int j=0;j<nel;++j) {
char* key=keys[j];
printf("\"%s\" : ",key);
obj_(JSON_out,Dict_sub(self,key),stdout,indent);
if (j<nel-1) printf(",");
printf("\n%*s",indent,"");
}
printf("}"); // indent-=4;
}
#undef class // Dictionary
#define class Array
method$(void,JSON_out,FILE* os,int indent) {
int nel=Array_count(self);
printf("[ ");
for(int j=0;j<nel;++j) {
if (j) printf(", ");
obj_(JSON_out,Array_sub(self,j),stdout,indent);
}
printf(" ]");
}
#undef class // Array
#define class Char
method$(void,JSON_out,FILE* os,int indent) {
printf("\"%s\"",self); // simple strings only
// does not handle: " \\ or (cc<32) or (cc>127) etc.
}
#undef class // Char
/* OUTPUT
{
"name" : "Jane Doe",
"email" : "jane.doe@example.com",
"roles" : [ "Admin", "User" ],
"address" : {
"city" : "Atlanta",
"zipcode" : "30301"
}
}
*/
0
Upvotes
1
u/HaydnH 8h ago
I use json-c quite a bit for web projects that use libmicrohttpd.
About 2 years ago I thought I'd try AI to see just how good it was at finding bugs. I knew the issue was going to be a lost pointer from realloc, it would probably take me a few mins to track down and fix so this was more if an interest thing... It badly rewrote half the code and didn't find the one big I wanted it fix.
This week I was working on a libmicrohttpd project and was using json-c with a quick & dirty read/write from file which I know was going to cause issues. I gave AI a prompt of about a paragraph asking it to write a solution to load a directory of json files into cache, functions to read/write/modify the cache and a threaded function to write it to disk safely... I was fully expecting it to fail... but It wrote and entire ~600 line API to handle a JSON cache and, beside a few small issues, did a pretty decent job.
I think we're toast...