]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/osrf_json_utils.h
Enhance the performance of the recursive descent JSON parser,
[OpenSRF.git] / include / opensrf / osrf_json_utils.h
1 /*
2 Copyright (C) 2006  Georgia Public Library Service 
3 Bill Erickson <billserickson@gmail.com>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 */
15
16 #ifndef OSRF_JSON_UTILS_H
17 #define OSRF_JSON_UTILS_H
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 /* ----------------------------------------------------------------------- */
24 /* Clients need not include this file.  These are internal utilities only       */
25 /* ----------------------------------------------------------------------- */
26
27 #define JSON_EAT_WS(ctx)        \
28         while( ctx->index < ctx->chunksize ) {  \
29                 if(!isspace(ctx->chunk[ctx->index])) break; \
30                 ctx->index++;   \
31         } \
32         if( ctx->index >= ctx->chunksize ) return 0; \
33         c = ctx->chunk[ctx->index];
34
35 #define JSON_CACHE_DATA(ctx, buf, size) \
36         while( (buf->n_used < size) && (ctx->index < ctx->chunksize) ) \
37                 buffer_add_char(buf, ctx->chunk[ctx->index++]); 
38
39 #define JSON_LOG_MARK __FILE__,__LINE__
40
41 #define JSON_NUMBER_CHARS "0123456789.+-eE"
42
43 /**
44  * These are the callbacks through which the top level parser 
45  * builds objects via the push parser
46  */
47 void _jsonHandleStartObject(void*);
48 void _jsonHandleObjectKey(void*, char* key);
49 void _jsonHandleEndObject(void*);
50 void _jsonHandleStartArray(void*);
51 void _jsonHandleEndArray(void*);
52 void _jsonHandleNull(void*);
53 void _jsonHandleString(void*, char* string);
54 void _jsonHandleBool(void*, int boolval);
55 void _jsonHandleNumber(void*, const char* numstr);
56 void _jsonHandleError(void*, char* str, ...);
57
58 struct jsonInternalParserStruct {
59         jsonParserContext* ctx;
60         jsonObject* obj;
61         jsonObject* current;
62         char* lastkey;
63         void (*handleError) (const char*);
64 };
65 typedef struct jsonInternalParserStruct jsonInternalParser;
66
67 jsonInternalParser* _jsonNewInternalParser();
68 void _jsonInternalParserFree(jsonInternalParser* p);
69
70 /**
71  * Calls the defined error handler with the given error message.
72  * @return -1
73  */
74 int _jsonParserError( jsonParserContext* ctx, char* err, ... );
75
76
77 /**
78  *
79  * @return 0 on continue, 1 if it goes past the end of the string, -1 on error
80  */
81 int _jsonParserHandleUnicode( jsonParserContext* ctx );
82
83
84 /**
85  * @param type 0 for null, 1 for true, 2 for false
86  * @return 0 on continue, 1 if it goes past the end of the string, -1 on error
87  */
88 int _jsonParserHandleMatch( jsonParserContext* ctx, int type );
89
90 /**
91  * @return 0 on continue, 1 on end of chunk, -1 on error 
92  */
93 int _jsonParserHandleString( jsonParserContext* ctx );
94
95 /**
96  * @return 0 on continue, 1 on end of chunk, -1 on error 
97  */
98 int _jsonParserHandleNumber( jsonParserContext* ctx );
99
100
101 void _jsonInsertParserItem( jsonInternalParser* p, jsonObject* newo );
102
103 #ifdef __cplusplus
104 }
105 #endif
106
107 #endif
108