1#ifndef PDJSON_H
2#define PDJSON_H
3
4#ifdef __cplusplus
5extern "C" {
6#endif // __cplusplus
7
8#include <stdio.h>
9#include <stdbool.h>
10
11enum json_type {
12 JSON_ERROR = 1, JSON_DONE,
13 JSON_OBJECT, JSON_OBJECT_END, JSON_ARRAY, JSON_ARRAY_END,
14 JSON_STRING, JSON_NUMBER, JSON_TRUE, JSON_FALSE, JSON_NULL
15};
16
17struct json_allocator {
18 void *(*malloc)(size_t);
19 void *(*realloc)(void *, size_t);
20 void (*free)(void *);
21};
22
23typedef int (*json_user_io) (void *user);
24
25typedef struct json_stream json_stream;
26typedef struct json_allocator json_allocator;
27
28extern const char *json_typename[];
29
30void json_open_buffer(json_stream *json, const void *buffer, size_t size);
31void json_open_string(json_stream *json, const char *string);
32void json_open_stream(json_stream *json, FILE *stream);
33void json_open_user(json_stream *json, json_user_io get, json_user_io peek, void *user);
34void json_close(json_stream *json);
35
36void json_set_allocator(json_stream *json, json_allocator *a);
37void json_set_streaming(json_stream *json, bool strict);
38
39enum json_type json_next(json_stream *json);
40enum json_type json_peek(json_stream *json);
41void json_reset(json_stream *json);
42const char *json_get_string(json_stream *json, size_t *length);
43double json_get_number(json_stream *json);
44
45size_t json_get_lineno(json_stream *json);
46size_t json_get_position(json_stream *json);
47size_t json_get_depth(json_stream *json);
48const char *json_get_error(json_stream *json);
49
50/* internal */
51
52struct json_source {
53 int (*get) (struct json_source *);
54 int (*peek) (struct json_source *);
55 size_t position;
56 union {
57 struct {
58 FILE *stream;
59 } stream;
60 struct {
61 const char *buffer;
62 size_t length;
63 } buffer;
64 struct {
65 void *ptr;
66 json_user_io get;
67 json_user_io peek;
68 } user;
69 } source;
70};
71
72struct json_stream {
73 size_t lineno;
74
75 struct json_stack *stack;
76 size_t stack_top;
77 size_t stack_size;
78 enum json_type next;
79 unsigned flags;
80
81 struct {
82 char *string;
83 size_t string_fill;
84 size_t string_size;
85 } data;
86
87 size_t ntokens;
88
89 struct json_source source;
90 struct json_allocator alloc;
91 char errmsg[128];
92};
93
94#ifdef __cplusplus
95} // extern "C"
96#endif // __cplusplus
97
98#endif
99