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
36
37
38
39
40
41
42
43
44
45
#ifndef __JSON_H__
#define __JSON_H__
#include <stdbool.h>

typedef struct {
    enum  { ELEMENT_INT, ELEMENT_STRING, ELEMENT_LIST } type;  // 4 bytes (on our platform)
    union {   // 8 bytes == max of ↓
        int    as_int;          // 4 bytes
        char*  as_string;       // 8 bytes
        struct _Node* as_list;  // 8 bytes
    }; // ANONYMOUS union (C11).
} Element;

// Size of Element in bytes will be (at least):
// 4 bytes for .type
// 8 bytes for the union ==> max(4 bytes for .as_int, 8 bytes for .as_string or .as_list)
// -----------------------
// 12 bytes (total)
//
// Size of a union object is the size of the largest field it can hold.
//
// Size of Node object in bytes will be (at least):
//  8 bytes for .next 
// 12 bytes for .element
// -----------------------
// 20 bytes (total)


typedef struct _Node {
    struct _Node* next;   //   8 bytes
    Element element;      // ≥12 bytes (see above)
} Node;                   // ≥20 bytes TOTAL


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.