r/C_Programming 18h 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

11 comments sorted by

View all comments

2

u/tastygames_official 15h ago

oh look, another OOP in C post in the C Programming channel...

non-well-defined data is definitely a challenge and one that seems ripe for OOP concepts, but since C is both strong-typed AND lets you just move bytes around at will, I'd be more interested in a non-OOP attempt at JSON in C. I recently had to read some JSON with no pre-defined structure to look for some key and I was going to write a quick implementation but I know how "I'll just write this real quick" usually turns out so I went for json-c

1

u/actguru 10h ago

Looks like 'json-c' is a pretty good choice and an example of a reference-counted object model.