Files

102 lines
1.9 KiB
C
Raw Permalink Normal View History

2016-03-08 22:14:30 +01:00
#ifndef GB_JSON_H
#define GB_JSON_H
2013-10-11 16:14:26 -06:00
#define JT_NULL 2
#define JT_NUMBER 3
#define JT_STRING 4
#define JT_ARRAY 5
#define JT_OBJECT 6
#include "gb-include.h"
#include "Unicode.h"
#include "SafeBuf.h"
#define MAXJSONPARENTS 64
bool endsInCurly ( char *s , int32_t slen );
2013-10-11 16:14:26 -06:00
class JsonItem {
public:
// scan the linked list
class JsonItem *m_next,*m_prev;
class JsonItem *m_parent;//child;
2013-10-11 16:14:26 -06:00
// the JT_* values above
int m_type;
// . the NAME of the item
// . points into the ORIGINAL json string
char *m_name;
2014-11-10 14:45:11 -08:00
int32_t m_nameLen;
2013-10-11 16:14:26 -06:00
// for JT_NUMBER
2014-11-10 14:45:11 -08:00
int32_t m_valueLong;
int64_t m_value64;
2013-10-11 16:14:26 -06:00
// for JT_NUMBER
double m_valueDouble;
// for JT_String
2014-11-10 14:45:11 -08:00
int32_t m_valueLen;
2013-10-11 16:14:26 -06:00
const char *m_valueArray;
2013-10-16 15:41:12 -07:00
// for JT_String
int32_t getValueLen() { return m_valueLen; }
2013-10-16 15:41:12 -07:00
// for arrays (JT_ARRAY), hack the char ptr into m_valueLong
const char *getArrayStart() { return m_valueArray;}
int32_t getArrayLen () { return m_valueLen; }
2013-10-11 16:14:26 -06: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);
2016-05-19 18:37:26 +02:00
}
2013-10-11 16:14:26 -06:00
// convert numbers and bools to strings for this one
2014-11-10 14:45:11 -08:00
char *getValueAsString ( int32_t *valueLen ) ;
// like acme.product.offerPrice if "acme:{product:{offerprice:1.23}}"
bool getCompoundName ( SafeBuf &nameBuf ) ;
bool isInArray ( );
2013-10-11 16:14:26 -06:00
};
class Json {
public:
2016-09-23 16:37:15 +02:00
JsonItem *parseJsonStringIntoJsonItems ( const char *json );
2013-10-11 16:14:26 -06:00
JsonItem *getFirstItem ( ) ;
JsonItem *getItem ( char *name );
2013-10-11 16:14:26 -06:00
JsonItem *addNewItem ();
2016-09-26 19:32:57 +02:00
Json() {
m_stackPtr = 0;
m_prev = NULL;
memset(m_stack, 0, sizeof(m_stack));
}
2013-10-11 16:14:26 -06:00
static bool prependKey(SafeBuf& jsonString, char* newKey);
2013-10-11 16:14:26 -06:00
SafeBuf m_sb;
JsonItem *m_stack[MAXJSONPARENTS];
2014-11-10 14:45:11 -08:00
int32_t m_stackPtr;
2013-10-11 17:35:12 -06:00
class JsonItem *m_prev;
2016-09-26 19:32:57 +02:00
void reset() {
m_sb.purge();
}
2013-10-11 16:14:26 -06:00
};
2016-03-08 22:14:30 +01:00
#endif // GB_JSON_H