| 1 |
/* |
| 2 |
* This is free and unencumbered software released into the public domain. |
| 3 |
* |
| 4 |
* Anyone is free to copy, modify, publish, use, compile, sell, or |
| 5 |
* distribute this software, either in source code form or as a compiled |
| 6 |
* binary, for any purpose, commercial or non-commercial, and by any |
| 7 |
* means. |
| 8 |
* |
| 9 |
* In jurisdictions that recognize copyright laws, the author or authors |
| 10 |
* of this software dedicate any and all copyright interest in the |
| 11 |
* software to the public domain. We make this dedication for the benefit |
| 12 |
* of the public at large and to the detriment of our heirs and |
| 13 |
* successors. We intend this dedication to be an overt act of |
| 14 |
* relinquishment in perpetuity of all present and future rights to this |
| 15 |
* software under copyright law. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 |
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 21 |
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 22 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
* |
| 25 |
* For more information, please refer to <http://unlicense.org/> |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifndef PDJSON_H |
| 29 |
#define PDJSON_H |
| 30 |
|
| 31 |
#include <stdio.h> |
| 32 |
#include "util.h" |
| 33 |
|
| 34 |
#define PDJSON_STACK_MAX 20 |
| 35 |
|
| 36 |
enum json_type { |
| 37 |
JSON_ERROR = 1, JSON_DONE, |
| 38 |
JSON_OBJECT, JSON_OBJECT_END, JSON_ARRAY, JSON_ARRAY_END, |
| 39 |
JSON_STRING, JSON_NUMBER, JSON_TRUE, JSON_FALSE, JSON_NULL |
| 40 |
}; |
| 41 |
|
| 42 |
extern const char *json_typename[]; |
| 43 |
|
| 44 |
struct json_allocator { |
| 45 |
void *(*malloc)(size_t); |
| 46 |
void *(*realloc)(void *, size_t); |
| 47 |
void (*free)(void *); |
| 48 |
}; |
| 49 |
|
| 50 |
typedef short (*json_user_io)(void *user); |
| 51 |
|
| 52 |
typedef struct json_stream json_stream; |
| 53 |
typedef struct json_allocator json_allocator; |
| 54 |
|
| 55 |
void json_open_buffer(json_stream *json, const void *buffer, size_t size); |
| 56 |
void json_open_string(json_stream *json, const char *string); |
| 57 |
void json_open_stream(json_stream *json, FILE *stream); |
| 58 |
void json_open_user(json_stream *json, json_user_io get, json_user_io peek, |
| 59 |
void *user); |
| 60 |
void json_close(json_stream *json); |
| 61 |
|
| 62 |
void json_set_allocator(json_stream *json, json_allocator *a); |
| 63 |
void json_set_streaming(json_stream *json, bool mode); |
| 64 |
|
| 65 |
enum json_type json_next(json_stream *json); |
| 66 |
enum json_type json_peek(json_stream *json); |
| 67 |
void json_reset(json_stream *json); |
| 68 |
const char *json_get_string(json_stream *json, size_t *length); |
| 69 |
double json_get_number(json_stream *json); |
| 70 |
|
| 71 |
enum json_type json_skip(json_stream *json); |
| 72 |
enum json_type json_skip_until(json_stream *json, enum json_type type); |
| 73 |
|
| 74 |
size_t json_get_lineno(json_stream *json); |
| 75 |
size_t json_get_position(json_stream *json); |
| 76 |
size_t json_get_depth(json_stream *json); |
| 77 |
enum json_type json_get_context(json_stream *json, size_t *count); |
| 78 |
const char *json_get_error(json_stream *json); |
| 79 |
|
| 80 |
short json_source_get(json_stream *json); |
| 81 |
short json_source_peek(json_stream *json); |
| 82 |
bool json_isspace(short c); |
| 83 |
|
| 84 |
/* internal */ |
| 85 |
|
| 86 |
struct json_source { |
| 87 |
short (*get)(struct json_source *); |
| 88 |
short (*peek)(struct json_source *); |
| 89 |
size_t position; |
| 90 |
union { |
| 91 |
struct { |
| 92 |
FILE *stream; |
| 93 |
} stream; |
| 94 |
struct { |
| 95 |
const char *buffer; |
| 96 |
size_t length; |
| 97 |
} buffer; |
| 98 |
struct { |
| 99 |
void *ptr; |
| 100 |
json_user_io get; |
| 101 |
json_user_io peek; |
| 102 |
} user; |
| 103 |
} source; |
| 104 |
}; |
| 105 |
|
| 106 |
struct json_stream { |
| 107 |
size_t lineno; |
| 108 |
|
| 109 |
struct json_stack *stack; |
| 110 |
size_t stack_top; |
| 111 |
size_t stack_size; |
| 112 |
enum json_type next; |
| 113 |
unsigned flags; |
| 114 |
|
| 115 |
struct { |
| 116 |
char *string; |
| 117 |
size_t string_fill; |
| 118 |
size_t string_size; |
| 119 |
} data; |
| 120 |
|
| 121 |
size_t ntokens; |
| 122 |
|
| 123 |
struct json_source source; |
| 124 |
struct json_allocator alloc; |
| 125 |
char errmsg[128]; |
| 126 |
}; |
| 127 |
|
| 128 |
#endif |