]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/osrf_legacy_json.h
added connect/disconnect support, with onconnect callback
[OpenSRF.git] / include / opensrf / osrf_legacy_json.h
1 /*
2 Copyright (C) 2005  Georgia Public Library Service 
3 Bill Erickson <highfalutin@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
17
18
19 /* ---------------------------------------------------------------------------------------
20         JSON parser.
21  * --------------------------------------------------------------------------------------- */
22 #ifndef LEGACY_JSON_H
23 #define LEGACY_JSON_H
24
25 #include <opensrf/osrf_json.h>
26 #include <ctype.h>
27
28
29
30 /* Parses the given JSON string and returns the built object. 
31  *      returns NULL (and prints parser error to stderr) on error.  
32  */
33
34 jsonObject* json_parse_string(char* string);
35
36 jsonObject* legacy_jsonParseString(const char* string);
37 jsonObject* legacy_jsonParseStringFmt( const char* string, ... );
38
39 jsonObject* json_parse_file( const char* filename );
40
41 jsonObject* legacy_jsonParseFile( const char* string );
42
43
44
45 /* does the actual parsing work.  returns 0 on success.  -1 on error and
46  * -2 if there was no object to build (string was all comments) 
47  */
48 int _json_parse_string(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
49
50 /* returns 0 on success and turns obj into a string object */
51 int json_parse_json_string(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
52
53 /* returns 0 on success and turns obj into a number or double object */
54 int json_parse_json_number(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
55
56 /* returns 0 on success and turns obj into an 'object' object */
57 int json_parse_json_object(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
58
59 /* returns 0 on success and turns object into an array object */
60 int json_parse_json_array(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
61
62 /* churns through whitespace and increments index as it goes.
63  * eat_all == true means we should eat newlines, tabs
64  */
65 void json_eat_ws(char* string, unsigned long* index, int eat_all, int current_strlen);
66
67 int json_parse_json_bool(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
68
69 /* removes comments from a json string.  if the comment contains a class hint
70  * and class_hint isn't NULL, an allocated char* with the class name will be
71  * shoved into *class_hint.  returns 0 on success, -1 on parse error.
72  * 'index' is assumed to be at the second character (*) of the comment
73  */
74 int json_eat_comment(char* string, unsigned long* index, char** class_hint, int parse_class, int current_strlen);
75
76 /* prints a useful error message to stderr. always returns -1 */
77 int json_handle_error(char* string, unsigned long* index, char* err_msg);
78
79 int json_parse_json_null(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
80
81
82 char* legacy_jsonObjectToJSON( const jsonObject* obj );
83
84
85
86 /* LEGACY ITERATOR CODE ---------------------------------------------------  
87    ------------------------------------------------------------------------ */
88
89 struct _jsonObjectNodeStruct {
90         unsigned long index; /* our array position */
91         char* key; /* our hash key */
92         jsonObject* item; /* our object */
93 };
94 typedef struct _jsonObjectNodeStruct jsonObjectNode;
95
96
97
98 /* utility object for iterating over hash objects */
99 struct _jsonObjectIteratorStruct {
100     jsonIterator* iterator;
101         const jsonObject* obj; /* the topic object */
102         jsonObjectNode* current; /* the current node within the object */
103     int done;
104 };
105 typedef struct _jsonObjectIteratorStruct jsonObjectIterator;
106
107
108 /** Allocates a new iterator 
109         @param obj The object over which to iterate.
110 */
111 jsonObjectIterator* jsonNewObjectIterator(const jsonObject* obj);
112
113 /** 
114         De-allocates an iterator 
115         @param iter The iterator object to free
116 */
117 void jsonObjectIteratorFree(jsonObjectIterator* iter);
118
119 /** 
120         Returns the object_node currently pointed to by the iterator
121         and increments the pointer to the next node
122         @param iter The iterator in question.
123  */
124 jsonObjectNode* jsonObjectIteratorNext(jsonObjectIterator* iter);
125
126 /** 
127         @param iter The iterator.
128         @return True if there is another node after the current node.
129  */
130 int jsonObjectIteratorHasNext(const jsonObjectIterator* iter);
131
132
133 #endif
134
135