| 1 |
#ifndef _PROTO_JSON |
| 2 |
#define _PROTO_JSON |
| 3 |
|
| 4 |
// This provides a means for loading JSON input (from a file or text stream, etc) into a data structure |
| 5 |
// The plan is to use this for configuration files, etc for Protolib protocol implementations. |
| 6 |
|
| 7 |
// NOTES: |
| 8 |
// 1) The parsing of ASCII text JSON files is pretty much complete. Work needs to be done to support UTF |
| 9 |
// codings that can occur, etc. |
| 10 |
// |
| 11 |
// 2) The ProtoJson::Parser here loads JSON input text into a "document" data structure. Object key,value pairs |
| 12 |
// _are_ stored in dictionaries and Array elements are in indexed array structures. A ProtoJson::Document |
| 13 |
// is defined will be created with methods for iteration and queries. The ProtoJson::Document::Print() method |
| 14 |
// uses iteration over the document tree and can serve as an example. Eventually, helper methods for |
| 15 |
// programmatically constructing the Document/Tree will be supported, too. |
| 16 |
// |
| 17 |
// 3) The initial goal is to support configuration files for Protolib protocol implementations using the JSON |
| 18 |
// format since it is well documented, fairly structured, and more human readable/editable than some other formats. |
| 19 |
// |
| 20 |
// 4) There are some additional convenience methods to be added for setting/getting object (by key) or |
| 21 |
// array (by index) String, Number, etc. values. Some of these have been implemented. |
| 22 |
|
| 23 |
#include "protoTree.h" |
| 24 |
#include "protoQueue.h" |
| 25 |
#include "protoDefs.h" |
| 26 |
|
| 27 |
#ifdef TRUE |
| 28 |
#undef TRUE |
| 29 |
#undef FALSE |
| 30 |
#endif // TRUE |
| 31 |
|
| 32 |
namespace ProtoJson |
| 33 |
{ |
| 34 |
class Item : public ProtoQueue::Item |
| 35 |
{ |
| 36 |
public: |
| 37 |
virtual ~Item(); |
| 38 |
|
| 39 |
enum Type |
| 40 |
{ |
| 41 |
INVALID = 0, // for error handling |
| 42 |
ENTRY, // Object key,value entries |
| 43 |
STRING, |
| 44 |
NUMBER, |
| 45 |
OBJECT, |
| 46 |
ARRAY, |
| 47 |
TRUE, |
| 48 |
FALSE, |
| 49 |
NONE // we don't use 'NULL' because that collides |
| 50 |
}; |
| 51 |
|
| 52 |
static const char* GetTypeString(Type type); |
| 53 |
|
| 54 |
Type GetType() const |
| 55 |
{return type;} |
| 56 |
|
| 57 |
unsigned int GetLevel() const |
| 58 |
{return level;} |
| 59 |
|
| 60 |
bool IsValue() const |
| 61 |
{return (type > ENTRY);} |
| 62 |
|
| 63 |
void SetParent(Item* theParent) // TBD - make this only accessible to 'friend' classes |
| 64 |
{parent = theParent;} |
| 65 |
|
| 66 |
const Item* GetParent() const // TBD - don't need this, can use stack instead?? |
| 67 |
{return parent;} |
| 68 |
Item* AccessParent() // TBD - don't need this, can use stack instead?? |
| 69 |
{return parent;} |
| 70 |
|
| 71 |
protected: |
| 72 |
Item(Type theType, Item* theParent = NULL); |
| 73 |
void SetType(Type theType) |
| 74 |
{type = theType;} |
| 75 |
|
| 76 |
private: |
| 77 |
Type type; |
| 78 |
Item* parent; // TBD - deprecate the "parent" member if we don't really need it |
| 79 |
unsigned int level; // Item's depth in document tree (a convenience for printing, etc) |
| 80 |
// (If we implemented a container for our Document::Iterator class |
| 81 |
// instead of using the default ProtoQueue mechanism, the container |
| 82 |
// could be used to cache the item's level during iteration) |
| 83 |
}; // end class ProtoJson::Item |
| 84 |
|
| 85 |
typedef Item Value; // for clarity and convenience |
| 86 |
|
| 87 |
class ItemList : public ProtoSimpleQueueTemplate<ProtoJson::Item> |
| 88 |
{ |
| 89 |
public: |
| 90 |
void AddItem(Item& item) |
| 91 |
{Append(item);} |
| 92 |
}; // end class ProtoJson::ItemList |
| 93 |
|
| 94 |
// The String 'text' is "unescaped" representation |
| 95 |
// Input parsing and output (e.g., printed to a file) |
| 96 |
// is decoded/encoded per JSON escape sequence requirements. |
| 97 |
class String : public Value |
| 98 |
{ |
| 99 |
public: |
| 100 |
String(Item* theParent = NULL); |
| 101 |
virtual ~String(); |
| 102 |
|
| 103 |
bool SetText(const char* theText); |
| 104 |
const char* GetText() const |
| 105 |
{return text;} |
| 106 |
|
| 107 |
size_t GetLength() const |
| 108 |
{return (NULL != text) ? strlen(text) : 0;} |
| 109 |
|
| 110 |
// for internal use only |
| 111 |
void SetTextPtr(char* textPtr); |
| 112 |
|
| 113 |
private: |
| 114 |
char* text; |
| 115 |
|
| 116 |
}; // end class ProtoJson::String |
| 117 |
|
| 118 |
// may be an int or double |
| 119 |
class Number : public Value |
| 120 |
{ |
| 121 |
public: |
| 122 |
Number(Item* theParent = NULL); |
| 123 |
Number(int value, Item* theParent = NULL); |
| 124 |
Number(double value, Item* theParent = NULL); |
| 125 |
|
| 126 |
virtual ~Number(); |
| 127 |
|
| 128 |
bool SetValue(const char* text); |
| 129 |
void SetValue(double value) |
| 130 |
{ |
| 131 |
floating = value; |
| 132 |
is_float = true; |
| 133 |
} |
| 134 |
void SetValue(int value) |
| 135 |
{ |
| 136 |
integer = value; |
| 137 |
is_float = false; |
| 138 |
} |
| 139 |
bool IsFloat() const |
| 140 |
{return is_float;} |
| 141 |
double GetDouble() const |
| 142 |
{return is_float ? floating : (double)integer;} |
| 143 |
int GetInteger() const |
| 144 |
{return is_float ? (int)floating : integer;} |
| 145 |
private: |
| 146 |
bool is_float; |
| 147 |
union |
| 148 |
{ |
| 149 |
double floating; |
| 150 |
int integer; |
| 151 |
}; |
| 152 |
}; // end class ProtoJson::Number |
| 153 |
|
| 154 |
class Array : public Value |
| 155 |
{ |
| 156 |
public: |
| 157 |
Array(Item* theParent = NULL); |
| 158 |
virtual ~Array(); |
| 159 |
|
| 160 |
void Destroy(); // clear all values |
| 161 |
|
| 162 |
unsigned int GetLength() const |
| 163 |
{return array_len;} |
| 164 |
|
| 165 |
bool AppendString(const char* text); |
| 166 |
const char* GetString(unsigned int index); |
| 167 |
|
| 168 |
bool AppendValue(Value& value); |
| 169 |
const Value* GetValue(unsigned int index) const; |
| 170 |
Value* AccessValue(unsigned int index); |
| 171 |
|
| 172 |
// "index" MUST be in existing array range for now |
| 173 |
void SetValue(unsigned int index, Value& value); |
| 174 |
void ClearValue(unsigned int index); // remove/delete Value at index |
| 175 |
|
| 176 |
private: |
| 177 |
Item** array_buf; |
| 178 |
unsigned int array_len; |
| 179 |
}; // end class ProtoJson::Array |
| 180 |
|
| 181 |
// This is used for JSON Object key,value pairs |
| 182 |
class Entry : public ProtoJson::Item, public ProtoSortedTree::Item |
| 183 |
{ |
| 184 |
public: |
| 185 |
Entry(ProtoJson::Item* theParent = NULL); |
| 186 |
virtual ~Entry(); |
| 187 |
|
| 188 |
bool SetKey(const char* text); |
| 189 |
|
| 190 |
const char* GetKey() const |
| 191 |
{return key;} |
| 192 |
unsigned int GetKeysize() const |
| 193 |
{return keysize;} |
| 194 |
|
| 195 |
void SetValue(Value* theValue); // deletes old value, if any |
| 196 |
|
| 197 |
const Value* GetValue() const |
| 198 |
{return value;} |
| 199 |
Value* AccessValue() const |
| 200 |
{return value;} |
| 201 |
|
| 202 |
private: |
| 203 |
char* key; |
| 204 |
unsigned int keysize; |
| 205 |
Value* value; |
| 206 |
}; // end class ProtoJson::Entry |
| 207 |
|
| 208 |
class Object : public Item, protected ProtoSortedTreeTemplate<Entry> |
| 209 |
{ |
| 210 |
public: |
| 211 |
Object(ProtoJson::Item* theParent = NULL); |
| 212 |
virtual ~Object(); |
| 213 |
void Destroy(); |
| 214 |
|
| 215 |
bool HasKey(const char* key) const |
| 216 |
{return (NULL != FindEntry(key));} |
| 217 |
|
| 218 |
bool InsertString(const char* key, const char* text); |
| 219 |
bool InsertBoolean(const char* key, bool state); |
| 220 |
|
| 221 |
// Returns first String text matching "key" |
| 222 |
const char* GetString(const char* key); |
| 223 |
// Returns first Boolean value matching "key" |
| 224 |
// Returns "false" if "key" is not included (i.e. "false" default |
| 225 |
bool GetBoolean(const char* key); |
| 226 |
// Returns first Array matching "key" |
| 227 |
Array* GetArray(const char* key); |
| 228 |
|
| 229 |
bool InsertEntry(const char* key, Value& value); |
| 230 |
bool InsertEntry(Entry& entry); |
| 231 |
|
| 232 |
Entry* FindEntry(const char* key) const |
| 233 |
{return Find(key, (unsigned int)(strlen(key)+1)*8);} |
| 234 |
|
| 235 |
void RemoveEntry(Entry& entry) |
| 236 |
{ |
| 237 |
Remove(entry); |
| 238 |
entry.SetParent(NULL); |
| 239 |
} |
| 240 |
|
| 241 |
class Iterator : protected ProtoSortedTreeTemplate<Entry>::Iterator |
| 242 |
{ |
| 243 |
public: |
| 244 |
// If "key" is non-NULL, return matching entries only |
| 245 |
Iterator(Object& object); |
| 246 |
~Iterator(); |
| 247 |
bool Reset(bool reverse = false, const char* key = NULL); |
| 248 |
Entry* GetNextEntry(); |
| 249 |
Entry* GetPrevEntry(); |
| 250 |
|
| 251 |
private: |
| 252 |
char* match_key; |
| 253 |
|
| 254 |
}; // end class ProtoJson::Object::Iterator |
| 255 |
|
| 256 |
}; // end class ProtoJson::Object |
| 257 |
|
| 258 |
class Boolean : public Value |
| 259 |
{ |
| 260 |
public: |
| 261 |
Boolean(bool value, Item* theParent = NULL) |
| 262 |
: Item(value ? TRUE : FALSE, theParent) {} |
| 263 |
virtual ~Boolean() {} |
| 264 |
|
| 265 |
bool GetValue() const |
| 266 |
{return (TRUE == GetType() ? true : false);} |
| 267 |
void SetValue(bool state) |
| 268 |
{SetType(state ? TRUE: FALSE);} |
| 269 |
}; |
| 270 |
|
| 271 |
class NullValue : public Value |
| 272 |
{ |
| 273 |
public: |
| 274 |
NullValue(Item* theParent = NULL) : Item(NONE, theParent) {} |
| 275 |
virtual ~NullValue() {} |
| 276 |
}; // end class ProtoJson::NullValue |
| 277 |
|
| 278 |
class Document |
| 279 |
{ |
| 280 |
public: |
| 281 |
Document(); |
| 282 |
~Document(); |
| 283 |
void Destroy() |
| 284 |
{ |
| 285 |
item_list.Destroy(); |
| 286 |
item_count = 0; |
| 287 |
} |
| 288 |
|
| 289 |
bool AddItem(ProtoJson::Item& item); |
| 290 |
void RemoveItem(ProtoJson::Item& item); |
| 291 |
unsigned int GetItemCount() const |
| 292 |
{return item_count;} |
| 293 |
|
| 294 |
void Print(FILE* filePtr); // TBD - add a PrintToBuffer() method (or ConvertToText()) |
| 295 |
static void PrintValue(FILE* filePtr, const Value& value); |
| 296 |
static void PrintString(FILE* filePtr, const char* text); // encodes escapable text |
| 297 |
class Iterator |
| 298 |
{ |
| 299 |
public: |
| 300 |
Iterator(Document& document, bool depthFirst = true); |
| 301 |
~Iterator(); |
| 302 |
|
| 303 |
ProtoJson::Item* GetNextItem(); |
| 304 |
|
| 305 |
private: |
| 306 |
ItemList::Iterator list_iterator; |
| 307 |
bool depth_first; |
| 308 |
ItemList pending_list; |
| 309 |
}; |
| 310 |
friend class Iterator; |
| 311 |
protected: |
| 312 |
ItemList item_list; |
| 313 |
unsigned int item_count; |
| 314 |
|
| 315 |
}; // end class ProtoJson::Document |
| 316 |
|
| 317 |
class Parser |
| 318 |
{ |
| 319 |
public: |
| 320 |
Parser(); |
| 321 |
~Parser(); |
| 322 |
|
| 323 |
void Destroy(); |
| 324 |
|
| 325 |
bool LoadDocument(const char *path, Document* document = NULL); |
| 326 |
|
| 327 |
Document* AccessDocument() |
| 328 |
{return current_document;} |
| 329 |
|
| 330 |
Document* DetachDocument(); |
| 331 |
|
| 332 |
enum Status |
| 333 |
{ |
| 334 |
PARSE_ERROR, |
| 335 |
PARSE_MORE, |
| 336 |
PARSE_DONE |
| 337 |
}; |
| 338 |
|
| 339 |
Status ProcessInput(const char* inputBuffer, unsigned int inputLength); |
| 340 |
|
| 341 |
static bool IsValidEscapeCode(char c); |
| 342 |
static char GetEscapeCode(char c); |
| 343 |
static char Unescape(char c); |
| 344 |
|
| 345 |
private: |
| 346 |
// Delimiters for parsing |
| 347 |
static const char OBJECT_START; |
| 348 |
static const char OBJECT_END; |
| 349 |
static const char ARRAY_START; |
| 350 |
static const char ARRAY_END; |
| 351 |
static const char QUOTE; |
| 352 |
static const char COLON; |
| 353 |
static const char COMMA; |
| 354 |
static const char ESCAPE; |
| 355 |
static const char TRUE_START; |
| 356 |
static const char FALSE_START; |
| 357 |
static const char NULL_START; |
| 358 |
|
| 359 |
// Methods used in internal parsing |
| 360 |
static Item::Type GetType(char c); // infers item type from first non-whitespace char of Value |
| 361 |
bool AddValueToParent(Item* theParent, Item& value); |
| 362 |
bool AddToString(String& string, const char* text, unsigned int length); |
| 363 |
Status ProcessStringInput(const char* input, unsigned int length); |
| 364 |
bool AddToTemp(const char* text, unsigned int length); |
| 365 |
Status ProcessNumberInput(const char* input, unsigned int length); |
| 366 |
bool FixedItemIsValid(Item::Type type); |
| 367 |
Status ProcessFixedInput(const char* input, unsigned int length); |
| 368 |
Status ProcessArrayInput(const char* input, unsigned int length); |
| 369 |
Status ProcessEntryInput(const char* input, unsigned int length); |
| 370 |
Status ProcessObjectInput(const char* input, unsigned int length); |
| 371 |
Status ProcessValueInput(const char* input, unsigned int length); |
| 372 |
void PushStack(Item& item) |
| 373 |
{parser_stack.Prepend(item);} |
| 374 |
Item* PopStack() |
| 375 |
{return parser_stack.RemoveHead();} |
| 376 |
Item* PeekStack() |
| 377 |
{return parser_stack.GetHead();} |
| 378 |
|
| 379 |
Document* current_document; |
| 380 |
ItemList parser_stack; |
| 381 |
Item* current_item; |
| 382 |
unsigned int input_offset; |
| 383 |
bool input_escape_pending; |
| 384 |
bool is_escaped; // for caching escape parse state |
| 385 |
bool seek_colon; // for caching Object "key : value" parse state |
| 386 |
char* temp_buffer; |
| 387 |
unsigned int temp_buffer_max; |
| 388 |
unsigned int temp_buffer_len; |
| 389 |
|
| 390 |
}; // end class ProtoJson::Parser |
| 391 |
|
| 392 |
} // end namespace ProtoJson |
| 393 |
|
| 394 |
#endif // _PROTO_JSON |