-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjRead.h
More file actions
134 lines (120 loc) · 4.84 KB
/
jRead.h
File metadata and controls
134 lines (120 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// jRead.h
//
// see jRead.c for more information
//
// uncomment this if you really want to use double quotes in query strings instead of '
//#define JREAD_DOUBLE_QUOTE_IN_QUERY
#ifndef JREAD_SINGLE_PRECISION
#define JREAD_FLOAT double
#else
#define JREAD_FLOAT float
#endif
//
// return dataTypes:
#define JREAD_ERROR 0 // general error, eof etc.
#define JREAD_OBJECT 1 // "{"
#define JREAD_ARRAY 2 // "["
#define JREAD_STRING 3 // "string"
#define JREAD_NUMBER 4 // number (may be -ve) int or float
#define JREAD_BOOL 5 // true or false
#define JREAD_NULL 6 // null
#define JREAD_KEY 7 // object "key"
// internal values:
#define JREAD_COLON 8 // ":"
#define JREAD_EOL 9 // end of input string (ptr at '\0')
#define JREAD_COMMA 10 // ","
#define JREAD_EOBJECT 11 // "}"
#define JREAD_EARRAY 12 // "]"
#define JREAD_QPARAM 13 // "*" query string parameter
//------------------------------------------------------
// jReadElement
// - structure to return JSON elements
// - error=0 for valid returns
//
// *NOTES*
// the returned pValue pointer points into the passed JSON
// string returns are not '\0' terminated.
// bytelen specifies the length of the returned data pointed to by pValue
//
struct jReadElement{
int dataType; // one of JREAD_...
int elements; // number of elements (e.g. elements in array or object)
int bytelen; // byte length of element (e.g. length of string, array text "[ ... ]" etc.)
void * pValue; // pointer to value string in JSON text
int error; // error value if dataType == JREAD_ERROR
};
//------------------------------------------------------
// The JSON reader function
//
// - reads a '\0'-terminated JSON text string from pJson
// - traverses the JSON according to the pQuery string
// - returns the result value in pResult
//
// returns: pointer into pJson after the queried value
//
// e.g.
// With JSON like: "{ ..., "key":"value", ... }"
//
// jRead( pJson, "{'key'", &result );
// returns with:
// result.dataType= JREAD_STRING, result.pValue->'value', result.bytelen=5
//
char * jRead( char *pJson, char *pQuery, struct jReadElement *pResult );
// version of jRead which allows one or more queryParam integers to be substituted
// for array or object indexes marked by a '*' in the query
//
// e.g. jReadParam( pJson, "[*", &resultElement, &arrayIndex );
//
// *!* CAUTION *!*
// You can supply an array of integers which are indexed for each '*' in pQuery
// however, horrid things will happen if you don't supply enough parameters
//
char * jReadParam( char *pJson, char *pQuery, struct jReadElement *pResult, int *queryParams );
// Array Stepping function
// - assumes pJsonArray is JSON source of an array "[ ... ]"
// - returns next element of the array in pResult
// - returns pointer to end of element, to be passed to next call of jReadArrayStep()
// - if end of array is encountered, pResult->error = 13 "End of array found"
//
// e.g.
// With JSON like: "{ ... "arrayInObject":[ elem1,elem2,... ], ... }"
//
// pJson= jRead( pJson, "{'arrayInObject'", &theArray );
// if( theArray.dataType == JREAD_ARRAY )
// {
// char *pArray= (char *)theArray.pValue;
// jReadElement arrayElement;
// int index;
// for( index=0; index < theArray.elements; index++ )
// {
// pArray= jReadArrayStep( pArray, &arrayElement );
// ...
//
// Note: this significantly speeds up traversing arrays.
//
char *jReadArrayStep( char *pJsonArray, struct jReadElement *pResult );
#define EXPORT_OPTIONAL_FUNCTIONS
#ifdef EXPORT_OPTIONAL_FUNCTIONS
//------------------------------------------------------
// Optional Helper Functions
//
long jRead_long( char *pJson, char *pQuery, int *queryParams );
int jRead_int( char *pJson, char *pQuery, int *queryParams );
JREAD_FLOAT jRead_number( char *pJson, char *pQuery, int *queryParams );
int jRead_string( char *pJson, char *pQuery, char *pDest, int destlen, int *queryParams );
//------------------------------------------------------
// Optional String output Functions
//
char *jReadTypeToString( int dataType ); // string describes dataType
char * jReadErrorToString( int error ); // string descibes error code
//------------------------------------------------------
// Other jRead utilities which may be useful...
//
char * jRead_atoi( char *p, unsigned int *result ); // string to unsigned int
char * jRead_atol( char *p, long *result ); // string to signed long
char * jRead_atof( char *p, JREAD_FLOAT *result); // string to float (does not do exponents)
int jReadStrcmp( struct jReadElement *j1, struct jReadElement *j2 ); // compare STRING elements
// copy element to '\0'-terminated buffer
char * jRead_strcpy( char *destBuffer, int destLength, struct jReadElement *pElement );
#endif
// end of jRead.h