-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson.h
163 lines (128 loc) · 4.83 KB
/
json.h
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* json - A simple JSON library using only the standard C library.
* Copyright (C) 2022 Hasan Zahra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef JSON_H
#define JSON_H
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
/* hashmap */
#define JSONLIB_MAX(a, b) ((a) > (b)) ? (a) : (b)
typedef struct HMItem HMItem;
struct HMItem {
char *key;
void *val;
void (*k_free_func)(void *);
void (*v_free_func)(void *);
};
#define hashmap_item_free_func(a) (void (*)(void *))a
typedef struct {
HMItem **items; /* buckets */
size_t can_store;
size_t stored;
} HashMap;
HMItem *new_item(char *key, void *val,
void (*k_free_func)(void *), void (*v_free_func)(void *));
void free_item(HMItem *item);
HashMap *new_hashmap(size_t capacity);
void free_hashmap(HashMap *map);
int check_hashmap_capacity(HashMap *map, size_t n);
int hashmap_set(HashMap *map, HMItem *item);
/*
* 64-bit FNV-1a hash:
* https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function
* https://github.com/benhoyt/ht/blob/master/ht.c
*/
#define FNV_OFFSET 14695981039346656037UL
#define FNV_PRIME 1099511628211UL
uint64_t hashmap_hash_func(char *key);
void *hashmap_index(HashMap *map, char *key);
void hashmap_remove(HashMap *map, char *key);
/* Begin JSON implementation */
typedef enum json_t {
json_bool,
json_string,
json_array,
json_dict,
json_number,
json_null
} json_t;
typedef union {
double number;
char *string;
bool boolean;
struct json *json_data;
struct json **json_data_array;
HashMap *json_data_dict;
} json_data;
struct json {
json_t type;
char *key;
int n_data_items; /* length of array or dict (if present) */
size_t data_list_capacity; /* capacity of array/dict */
json_data data;
};
struct json *new_json(void);
void free_json_item(struct json *j);
void print_json(struct json *j);
HMItem *json_parse_dict_tuple(char *str, int *idx);
struct json *json_parse_dict(char *str, int *idx);
struct json *json_parse_string(char *str, int *idx);
struct json *json_parse_array(char *str, int *idx);
struct json *json_parse_number(char *str, int *idx);
struct json *json_parse_bool(char *str, int *idx);
struct json *json_parse_null(char *str, int *idx);
struct json *json_parse_item(char *str, int *idx);
#define json_parse(str) json_parse_item(str, NULL)
struct json *json_get_array_item(struct json *arr, int idx);
struct json *json_get_dict_item(struct json *dict, char *key);
int json_get_size(struct json *arr);
int json_get_capacity(struct json *arr);
struct json *json_access(struct json *j, ...);
struct json *json_safe_access(struct json *j, char *fmt, ...);
char *json_read_file(char *path);
#define json_get_string(j) (j->data.string)
#define json_get_bool(j) (j->data.boolean)
#define json_get_number(j) (j->data.number)
#define json_get_data(j) (j->data.json_data)
#define json_get_array(j) (j->data.json_data_array)
#define json_get_dict(j) (j->data.json_data_dict)
#define json_get_array_string(arr, i) (json_get_string(\
json_get_array_item(arr, i)))
#define json_get_array_bool(arr, i) (json_get_bool(\
json_get_array_item(arr, i)))
#define json_get_array_number(arr, i) (json_get_number(\
json_get_array_item(arr, i)))
#define json_get_array_data(arr, i) (json_get_data(\
json_get_array_item(arr, i)))
#define json_get_array_array(arr, i) (json_get_array(\
json_get_array_item(arr, i)))
#define json_get_array_dict(arr, i) (json_get_dict(\
json_get_array_item(arr, i)))
#define json_get_dict_string(dict, str) (json_get_string(\
json_get_dict_item(dict, str)))
#define json_get_dict_bool(dict, str) (json_get_bool(\
json_get_dict_item(dict, str)))
#define json_get_dict_number(dict, str) (json_get_number(\
json_get_dict_item(dict, str)))
#define json_get_dict_data(dict, str) (json_get_data(\
json_get_dict_item(dict, str)))
#define json_get_dict_array(dict, str) (json_get_array(\
json_get_dict_item(dict, str)))
#define json_get_dict_dict(dict, str) (json_get_dict(\
json_get_dict_item(dict, str)))
#endif /* JSON_H */