]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/objson/object.h
adding early custom json code
[Evergreen.git] / OpenSRF / src / objson / object.h
1 /* ---------------------------------------------------------------------------------------
2         Generic object framework for C.  An object can be either a string, boolean, null, 
3         number, array or hash (think Perl hash, dictionary, etc.).   
4  * --------------------------------------------------------------------------------------- */
5
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include <assert.h>
12 #include "utils.h"
13
14 /* does we need this? */
15 #define MAX_OBJECT_NODES 1000000
16
17 #ifndef OBJECT_H
18 #define OBJECT_H
19
20 /* top leve generic object structuure */
21 struct object_struct {
22
23         /* how many sub-objects do we contain.  Note that this includes null
24          * array elements in sparse arrays */
25         unsigned long size;
26
27         /* optional class hint */
28         char* classname;
29
30         /* these determine how we define a given object */
31         int is_array;
32         int is_hash;
33         int is_string;
34         int is_null;
35         int is_bool;
36         int is_number;
37         int is_double;
38
39         /* attached accessor/mutator methods for the OO inclined*/
40         unsigned long                           (*push)                         (struct object_struct* src, struct object_struct*);
41         unsigned long                           (*add_index)            (struct object_struct* src, unsigned long index, struct object_struct*);
42         unsigned long                           (*add_key)                      (struct object_struct* src, char* key, struct object_struct*);
43         struct object_struct*   (*get_index)            (struct object_struct*, unsigned long index);
44         struct object_struct*   (*get_key)                      (struct object_struct*, char* key);
45         void                                                    (*set_string)           (struct object_struct*, char*);
46         void                                                    (*set_number)           (struct object_struct*, long number);
47         void                                                    (*set_double)           (struct object_struct*, double number);
48         void                                                    (*set_class)            (struct object_struct*, char* classname);
49         unsigned long                           (*remove_index) (struct object_struct*, unsigned long index);
50         unsigned long                           (*remove_key)           (struct object_struct*, char* key);
51         char*                                                   (*get_string)           (struct object_struct*);
52         char*                                                   (*to_json)                      (struct object_struct*);
53         void                                                    (*set_comment)          (struct object_struct*, char* com);
54
55         /* our list of sub-objects */
56         struct object_node_struct* data;
57
58         /* if we're a string, here's our data */
59         char* string_data;
60
61         /* if we're a boolean value, here's our value */
62         int bool_value;
63
64         /* if we're a number, here's our value */
65         long num_value;
66
67         /* if we're a double, here's our value */
68         double double_value;
69
70         /* client may provide a comment string which will be 
71          * added serialized object when applicable
72          */
73         char* comment;
74 };
75 typedef struct object_struct object;
76
77
78 /* this contains a single element of the object along with the elements 
79  * index (if this object is an array) and key (if this object is a hash)
80  */
81 struct object_node_struct {
82         unsigned long index; /* our array position */
83         char* key; /* our hash key */
84         object* item; /* our object */
85         struct object_node_struct* next; /* pointer to the next object node */
86 };
87 typedef struct object_node_struct object_node;
88
89 /* utility object for iterating over hash objects */
90 struct object_iterator_struct {
91         object* obj; /* the topic object */
92         object_node* current; /* the current node within the object */
93         object_node* (*next) (struct object_iterator_struct*);
94         int (*has_next) (struct object_iterator_struct*);
95 };
96 typedef struct object_iterator_struct object_iterator;
97
98 /* allocates a new iterator */
99 object_iterator* new_iterator(object* obj);
100
101 /* de-allocates an iterator */
102 void free_iterator(object_iterator*);
103
104 /* returns the object_node currently pointed to by the iterator
105  * and increments the pointer to the next node
106  */
107 object_node* object_iterator_next(object_iterator*);
108
109 /* returns true if there is another node after the node 
110  * currently pointed to
111  */
112 int object_iterator_has_next(object_iterator*);
113
114
115 /* allocates a new object. classname is optional */
116 object* new_object(char* string);
117
118 /* utility method for initing an object */
119 object* _init_object();
120
121 /* returns a pointer to the object at the given index */
122 object* object_get_index( object* obj, unsigned long index );
123
124
125 /* returns a pointer to the object with the given key */
126 object* object_get_key( object* obj, char* key );
127
128 /* de-allocates a object */
129 void free_object(object*);
130
131 /* allocates a new object node */
132 object_node* new_object_node(object* obj);
133
134 /* de-allocates a object node */
135 void free_object_node(object_node*);
136
137 /* pushes the given object onto the end of the list, 
138  * returns the size on success, -1 on error 
139  */
140 unsigned long object_push(object*, object* obj);
141
142 /* removes the object at the given index (if one exists) and inserts 
143  * the new one.  returns the size on success, -1 on error 
144  */
145 unsigned long object_add_index(object*, unsigned long index, object* obj);
146
147 /* inserts the new object, overwriting any previous object with the given key 
148  * returns the size on success, -1 on error 
149  * if 'obj' is NULL, a new object is inserted at key 'key' with 'is_null' 
150  * set to true
151  */
152 unsigned long object_add_key(object*, char* key, object* obj);
153
154 /* removes the object at the given index and, if more items exist,
155  * re-indexes (shifts down by 1) the rest of the objects in the array
156  */
157 unsigned long object_remove_index(object*, unsigned long index);
158
159 /* de-allocates the object at index 'index' and sets the field to null.
160  * this will *not* change the object size */
161 //unsigned long object_clear_index(object*, unsigned long index);
162
163 /* removes the object with key 'key' if it exists */
164 unsigned long object_remove_key(object*, char* key);
165
166 /* returns a pointer to the string data held by this object */
167 char* object_get_string(object*);
168
169 /* sets the string data */
170 void object_set_string(object*, char* string);
171
172 /* sets the number value for the object */
173 void object_set_number(object*, long num);
174
175 /* sets the double value for this object */
176 void object_set_double(object*, double num);
177
178 /* sets the class hint for this object */
179 void object_set_class(object*, char* classname);
180
181 /* converts an object to a json string.  client is responsible for freeing the return string */
182 char* object_to_json(object*);
183
184 /* utility function. clears all of the is_* flags */
185 void object_clear_type(object*);
186
187 /* set this object's comment string */
188 void object_set_comment(object*, char*);
189
190 /* starting at index 'index', shifts all indices down by one and 
191  * decrements the objects size by 1 
192  */
193 void object_shift_index(object*, unsigned long index);
194
195
196
197 #endif