You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.9 KiB
78 lines
1.9 KiB
|
|
#ifndef _JSON_H_
|
|
#define _JSON_H_
|
|
|
|
#include <list>
|
|
#include <string>
|
|
#include <string.h>
|
|
|
|
enum {
|
|
JSON_T_NONE,
|
|
JSON_T_STRING,
|
|
JSON_T_NUMBER,
|
|
JSON_T_OBJECT,
|
|
JSON_T_ARRAY
|
|
};
|
|
|
|
class JSONElement {
|
|
public:
|
|
int type;
|
|
std::string name;
|
|
std::string value;
|
|
|
|
JSONElement() { Clear(); };
|
|
~JSONElement() {};
|
|
|
|
void Clear() { type = JSON_T_NONE; name = ""; value = ""; };
|
|
void Set (std::string n, double v);
|
|
void Set (std::string n, int v);
|
|
void Set (std::string n, int64_t v);
|
|
void Set (std::string n, std::string v);
|
|
void SetArray (std::string n, std::list<JSONElement> *l);
|
|
void SetAddArray (std::string n, std::string jpstring);
|
|
void SetObject (std::string n, std::string s);
|
|
static std::string StringAddEscape(std::string *s);
|
|
static std::string StringRemoveEscape(std::string *s);
|
|
std::string GetString();
|
|
};
|
|
|
|
class JSONParse {
|
|
private:
|
|
std::string jsondata;
|
|
std::list<JSONElement> names;
|
|
|
|
public:
|
|
JSONParse() { Set("{}"); };
|
|
JSONParse(std::string json) { Set(json); };
|
|
~JSONParse() {};
|
|
|
|
void Clear();
|
|
int Set(std::string json);
|
|
|
|
int GetValue(std::string varname, std::string *dest);
|
|
int GetValueInt(std::string varname, int *dest);
|
|
int GetValueInt64(std::string varname, int64_t *dest);
|
|
int GetValueString(std::string varname, std::string *dest);
|
|
int GetObject(std::string varname, JSONParse *dest);
|
|
|
|
int GetIdx(std::string src, int idx, std::string *dest);
|
|
int GetValueIdx(std::string varname, int idx, std::string *dest);
|
|
int GetObjectIdx(std::string varname, int idx, JSONParse *dest);
|
|
|
|
std::list<JSONElement> GetElements();
|
|
|
|
void AddObject (JSONElement element);
|
|
void AddObject (std::string name, int val);
|
|
void AddObject (std::string name, int64_t val);
|
|
void AddObject (std::string name, std::string val);
|
|
void AddObject (std::string name, double val);
|
|
void AddObject (std::string name, JSONParse jp);
|
|
|
|
int LoadFromFile (std::string filename);
|
|
int SaveToFile (std::string filename);
|
|
|
|
std::string ToString();
|
|
};
|
|
|
|
#endif // _JSON_H_
|