laxjson.h (2807B)
1 /* 2 * Copyright (c) 2013 Andrew Kelley 3 * 4 * This file is part of liblaxjson, which is MIT licensed. 5 * See http://opensource.org/licenses/MIT 6 */ 7 8 #pragma once 9 10 #ifdef __cplusplus 11 extern "C" 12 { 13 #endif /* __cplusplus */ 14 15 enum LaxJsonType { 16 LaxJsonTypeString, 17 LaxJsonTypeProperty, 18 LaxJsonTypeNumber, 19 LaxJsonTypeObject, 20 LaxJsonTypeArray, 21 LaxJsonTypeTrue, 22 LaxJsonTypeFalse, 23 LaxJsonTypeNull 24 }; 25 26 enum LaxJsonState { 27 LaxJsonStateValue, 28 LaxJsonStateObject, 29 LaxJsonStateArray, 30 LaxJsonStateString, 31 LaxJsonStateStringEscape, 32 LaxJsonStateUnicodeEscape, 33 LaxJsonStateBareProp, 34 LaxJsonStateCommentBegin, 35 LaxJsonStateCommentLine, 36 LaxJsonStateCommentMultiLine, 37 LaxJsonStateCommentMultiLineStar, 38 LaxJsonStateExpect, 39 LaxJsonStateEnd, 40 LaxJsonStateColon, 41 LaxJsonStateNumber, 42 LaxJsonStateNumberDecimal, 43 LaxJsonStateNumberExponent, 44 LaxJsonStateNumberExponentSign 45 }; 46 47 enum LaxJsonError { 48 LaxJsonErrorNone, 49 LaxJsonErrorUnexpectedChar, 50 LaxJsonErrorExpectedEof, 51 LaxJsonErrorExceededMaxStack, 52 LaxJsonErrorNoMem, 53 LaxJsonErrorExceededMaxValueSize, 54 LaxJsonErrorInvalidHexDigit, 55 LaxJsonErrorInvalidUnicodePoint, 56 LaxJsonErrorExpectedColon, 57 LaxJsonErrorUnexpectedEof, 58 LaxJsonErrorAborted 59 }; 60 61 /* All callbacks must be provided. Return nonzero to abort the ongoing feed operation. */ 62 struct LaxJsonContext { 63 void *userdata; 64 /* type can be property or string */ 65 int (*string)(struct LaxJsonContext *, enum LaxJsonType type, const char *value, int length); 66 /* type is always number */ 67 int (*number)(struct LaxJsonContext *, char* x); 68 /* type can be true, false, or null */ 69 int (*primitive)(struct LaxJsonContext *, enum LaxJsonType type); 70 /* type can be array or object */ 71 int (*begin)(struct LaxJsonContext *, enum LaxJsonType type); 72 /* type can be array or object */ 73 int (*end)(struct LaxJsonContext *, enum LaxJsonType type); 74 75 int line; 76 int column; 77 78 int max_state_stack_size; 79 int max_value_buffer_size; 80 81 /* private members */ 82 enum LaxJsonState state; 83 enum LaxJsonState *state_stack; 84 int state_stack_index; 85 int state_stack_size; 86 87 char *value_buffer; 88 int value_buffer_index; 89 int value_buffer_size; 90 91 unsigned int unicode_point; 92 unsigned int unicode_digit_index; 93 94 char *expected; 95 char delim; 96 enum LaxJsonType string_type; 97 }; 98 99 struct LaxJsonContext *lax_json_create(void); 100 void lax_json_destroy(struct LaxJsonContext *context); 101 102 enum LaxJsonError lax_json_feed(struct LaxJsonContext *context, int size, const char *data); 103 enum LaxJsonError lax_json_eof(struct LaxJsonContext *context); 104 105 const char *lax_json_str_err(enum LaxJsonError err); 106 107 #ifdef __cplusplus 108 } 109 #endif /* __cplusplus */