1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef __JSON_H__
#define __JSON_H__
#include <stdbool.h>

typedef struct {
    enum  { ELEMENT_INT, ELEMENT_STRING, ELEMENT_LIST } type;
        // size: 4 byte
        // aligned access:
        //      hardware requires reads/writes of min 4 bytes
    union {
        int    as_int;         // size: 4 byte
        char*  as_string;      // size: 8 byte
        struct _Node* as_list; // size: 8 byte
    }; // size: 8 bytes - largest size from the union
} Element; // size: 16 bytes - 12 total bytes,
           //but our architecture rounded up to 16


typedef struct _Node {
    struct _Node* next; // size 8
    Element element;    // size 16
} Node; // size 24


bool parse_int(int* a_value, char const** a_pos);
bool parse_string(char** a_s, char const** a_pos);
bool parse_element(Element* a_element, char const** a_pos);
bool parse_list(Node** a_head, char const** a_pos);
void print_element(Element element);
void free_element(Element element);

#endif

#define JSON_H_VERSION_2
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2022 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.