open-source-search-engine/Json.h

83 lines
1.4 KiB
C
Raw Normal View History

2013-10-12 02:14:26 +04:00
#ifndef JSON_H
#define JSON_H
#define JT_NULL 2
#define JT_NUMBER 3
#define JT_STRING 4
#define JT_ARRAY 5
#define JT_OBJECT 6
//#define JT_IsReference 256
#include "gb-include.h"
#include "Unicode.h"
#include "SafeBuf.h"
#define MAXJSONPARENTS 64
class JsonItem {
public:
// scan the linked list
class JsonItem *m_next,*m_prev;
class JsonItem *m_parent;//child;
// the JT_* values above
int m_type;
// . the NAME of the item
// . points into the ORIGINAL json string
char *m_name;
long m_nameLen;
// for JT_NUMBER
long m_valueLong;
// for JT_NUMBER
double m_valueDouble;
// for JT_String
long m_valueLen;
2013-10-17 02:41:12 +04:00
// for JT_String
long getValueLen() { return m_valueLen; };
2013-10-12 02:14:26 +04:00
// for JT_String
char *getValue () {
// if value is another json object, then return NULL
// must be string
if ( m_type != JT_STRING ) return NULL;
// otherwie return the string which is stored decoded
// after this object in the same buffer
return (char *)this + sizeof(JsonItem);
};
// like acme.product.offerPrice if "acme:{product:{offerprice:1.23}}"
bool getCompoundName ( SafeBuf &nameBuf ) ;
bool isInArray ( );
2013-10-12 02:14:26 +04:00
};
class Json {
public:
void test();
2013-10-12 03:35:12 +04:00
JsonItem *parseJsonStringIntoJsonItems ( char *json );
2013-10-12 02:14:26 +04:00
JsonItem *getFirstItem ( ) ;
JsonItem *getItem ( char *name );
2013-10-12 02:14:26 +04:00
JsonItem *addNewItem ();
2013-10-18 05:59:00 +04:00
Json() { m_stackPtr = 0; m_prev = NULL; };
2013-10-12 02:14:26 +04:00
SafeBuf m_sb;
JsonItem *m_stack[MAXJSONPARENTS];
long m_stackPtr;
2013-10-12 03:35:12 +04:00
class JsonItem *m_prev;
2013-10-12 02:14:26 +04:00
};
#endif