2 Copyright (C) 2005 Georgia Public Library Service
3 Bill Erickson <highfalutin@gmail.com>
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.
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.
19 /* ---------------------------------------------------------------------------------------
21 * --------------------------------------------------------------------------------------- */
25 #include <opensrf/osrf_json.h>
32 /* Parses the given JSON string and returns the built object.
33 * returns NULL (and prints parser error to stderr) on error.
36 jsonObject* json_parse_string(char* string);
38 jsonObject* legacy_jsonParseString(const char* string);
39 jsonObject* legacy_jsonParseStringFmt( const char* string, ... );
41 /* does the actual parsing work. returns 0 on success. -1 on error and
42 * -2 if there was no object to build (string was all comments)
44 int _json_parse_string(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
46 /* returns 0 on success and turns obj into a string object */
47 int json_parse_json_string(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
49 /* returns 0 on success and turns obj into a number or double object */
50 int json_parse_json_number(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
52 /* returns 0 on success and turns obj into an 'object' object */
53 int json_parse_json_object(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
55 /* returns 0 on success and turns object into an array object */
56 int json_parse_json_array(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
58 /* churns through whitespace and increments index as it goes.
59 * eat_all == true means we should eat newlines, tabs
61 void json_eat_ws(char* string, unsigned long* index, int eat_all, int current_strlen);
63 int json_parse_json_bool(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
65 /* removes comments from a json string. if the comment contains a class hint
66 * and class_hint isn't NULL, an allocated char* with the class name will be
67 * shoved into *class_hint. returns 0 on success, -1 on parse error.
68 * 'index' is assumed to be at the second character (*) of the comment
70 int json_eat_comment(char* string, unsigned long* index, char** class_hint, int parse_class, int current_strlen);
72 /* prints a useful error message to stderr. always returns -1 */
73 int json_handle_error(char* string, unsigned long* index, char* err_msg);
75 int json_parse_json_null(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
78 char* legacy_jsonObjectToJSON( const jsonObject* obj );
82 /* LEGACY ITERATOR CODE ---------------------------------------------------
83 ------------------------------------------------------------------------ */
85 struct _jsonObjectNodeStruct {
86 unsigned long index; /* our array position */
87 char* key; /* our hash key */
88 jsonObject* item; /* our object */
90 typedef struct _jsonObjectNodeStruct jsonObjectNode;
94 /* utility object for iterating over hash objects */
95 struct _jsonObjectIteratorStruct {
96 jsonIterator* iterator;
97 const jsonObject* obj; /* the topic object */
98 jsonObjectNode* current; /* the current node within the object */
101 typedef struct _jsonObjectIteratorStruct jsonObjectIterator;
104 /** Allocates a new iterator
105 @param obj The object over which to iterate.
107 jsonObjectIterator* jsonNewObjectIterator(const jsonObject* obj);
110 De-allocates an iterator
111 @param iter The iterator object to free
113 void jsonObjectIteratorFree(jsonObjectIterator* iter);
116 Returns the object_node currently pointed to by the iterator
117 and increments the pointer to the next node
118 @param iter The iterator in question.
120 jsonObjectNode* jsonObjectIteratorNext(jsonObjectIterator* iter);
123 @param iter The iterator.
124 @return True if there is another node after the current node.
126 int jsonObjectIteratorHasNext(const jsonObjectIterator* iter);