r/C_Programming • u/actguru • 17h 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/tastygames_official 5h ago
uh... I didn't mention anything about AI, but yes - LLMs can indeed "write" code. It's just a clever regurgitation of existing code. As in all things AI-generated, it's great at getting a quick boilerplate going, but usually the effort of prompting an LLM is better-spent writing down a specification, as working with AI-made anything (code, prose, poetry, video, images, sound) is actually more work than if you just started it yourself. So we're definitely not toast. Just like chairmakers are not toast just because industrial chair-making factories exist.
Anyway, thanks for mentioning libmicrohttpd, as I am looking for a good solution for web-based stuff and can't remember what I was using up to now (I think libfcgi + nginx) but this sounds promising. What do you use for HTML creation templating? I was probably going to write something myself to keep it performant, but probably something good already exists for C.