Austin Schuh | f417eaf | 2019-09-16 21:58:36 -0700 | [diff] [blame] | 1 | // JSON Tokenizer. Copyright (c) 2012, Rasmus Andersson. All rights reserved. |
| 2 | // Use of this source code is governed by a MIT-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | #ifndef JSONT_INCLUDED |
| 5 | #define JSONT_INCLUDED |
| 6 | |
| 7 | #include <stdint.h> // uint8_t, int64_t |
| 8 | #include <stdlib.h> // size_t |
| 9 | #include <string.h> // strlen |
| 10 | #include <stdbool.h> // bool |
| 11 | |
| 12 | #ifndef _JSONT_IN_SOURCE |
| 13 | typedef struct jsont_ctx jsont_ctx_t; |
| 14 | typedef uint8_t jsont_tok_t; |
| 15 | #endif |
| 16 | |
| 17 | #ifndef JSONT_ERRINFO_CUSTOM |
| 18 | #define jsont_err_t const char* |
| 19 | #endif |
| 20 | |
| 21 | // Token types |
| 22 | enum { |
| 23 | JSONT_END = 0, // Input ended |
| 24 | JSONT_ERR, // Error |
| 25 | |
| 26 | JSONT_OBJECT_START, // { |
| 27 | JSONT_OBJECT_END, // } |
| 28 | |
| 29 | JSONT_ARRAY_START, // [ |
| 30 | JSONT_ARRAY_END, // ] |
| 31 | |
| 32 | JSONT_TRUE, // true |
| 33 | JSONT_FALSE, // false |
| 34 | JSONT_NULL, // null |
| 35 | |
| 36 | _JSONT_VALUES_START, |
| 37 | JSONT_NUMBER_INT, // number value without a fraction part |
| 38 | JSONT_NUMBER_FLOAT, // number value with a fraction part |
| 39 | JSONT_STRING, // string value |
| 40 | JSONT_FIELD_NAME, // field name |
| 41 | _JSONT_VALUES_END, |
| 42 | |
| 43 | _JSONT_COMMA, |
| 44 | }; |
| 45 | |
| 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
| 50 | // Create a new JSON tokenizer context. `user_data` can be anything and is |
| 51 | // accessible through `jsont_user_data`. |
| 52 | jsont_ctx_t* jsont_create(void* user_data); |
| 53 | |
| 54 | // Destroy a JSON tokenizer context. This will free any internal data, except |
| 55 | // from the input buffer. |
| 56 | void jsont_destroy(jsont_ctx_t* ctx); |
| 57 | |
| 58 | // Reset the tokenizer to parse the data pointed to by `bytes`. The tokenizer |
| 59 | // does NOT take ownership of `bytes`. This function can be used to recycle a |
| 60 | // tokenizer context, minimizing memory reallocation. |
| 61 | void jsont_reset(jsont_ctx_t* ctx, const uint8_t* bytes, size_t length); |
| 62 | |
| 63 | // Read and return the next token. See `jsont_tok_t` enum for a list of |
| 64 | // possible return values and their meaning. |
| 65 | jsont_tok_t jsont_next(jsont_ctx_t* ctx); |
| 66 | |
| 67 | // Returns the current token (last token read by `jsont_next`). |
| 68 | jsont_tok_t jsont_current(const jsont_ctx_t* ctx); |
| 69 | |
| 70 | // Returns a slice of the input which represents the current value, or nothing |
| 71 | // (returns 0) if the current token has no value (e.g. start of an object). |
| 72 | size_t jsont_data_value(jsont_ctx_t* ctx, const uint8_t** bytes); |
| 73 | |
| 74 | // Returns true if the current data value is equal to `bytes` of `length` |
| 75 | bool jsont_data_equals(jsont_ctx_t* ctx, const uint8_t* bytes, size_t length); |
| 76 | |
| 77 | // Returns true if the current data value is equal to c string `str` |
| 78 | static inline bool jsont_str_equals(jsont_ctx_t* ctx, const char* str) { |
| 79 | return jsont_data_equals(ctx, (const uint8_t*)str, strlen(str)); |
| 80 | } |
| 81 | |
| 82 | // Retrieve a newly allocated c-string. Similar to `jsont_data_value` but |
| 83 | // returns a newly allocated copy of the current value as a C string |
| 84 | // (terminated by a null byte). The calling code is responsible for calling |
| 85 | // `free()` on the returned value. |
| 86 | char* jsont_strcpy_value(jsont_ctx_t* ctx); |
| 87 | |
| 88 | // Returns the current integer value.If the number is too large or too small, |
| 89 | // this function sets errno and returns INT64_MAX or INT64_MIN. |
| 90 | int64_t jsont_int_value(jsont_ctx_t* ctx); |
| 91 | |
| 92 | // Returns the current floating-point number value. Sets errno and returns a |
| 93 | // value that isnan(N)==true on error. |
| 94 | double jsont_float_value(jsont_ctx_t* ctx); |
| 95 | |
| 96 | // Get the last byte read. Suitable for debugging JSONT_ERR. |
| 97 | uint8_t jsont_current_byte(jsont_ctx_t* ctx); |
| 98 | |
| 99 | // Get the current offset of the last byte read. |
| 100 | size_t jsont_current_offset(jsont_ctx_t* ctx); |
| 101 | |
| 102 | // Get information on the last error (by default a printable text message). |
| 103 | // Returns NULL if no error has occured since a call to `jsont_reset`. |
| 104 | jsont_err_t jsont_error_info(jsont_ctx_t* ctx); |
| 105 | |
| 106 | // Returns the value passed to `jsont_create`. |
| 107 | void* jsont_user_data(const jsont_ctx_t* ctx); |
| 108 | |
| 109 | // ----------------- C++ ----------------- |
| 110 | #ifdef __cplusplus |
| 111 | } // extern "C" |
| 112 | #endif |
| 113 | |
| 114 | #endif // JSONT_INCLUDED |