|
|
|
|
@ -270,29 +270,21 @@ int JSONParse::GetObject(std::string varname, JSONParse *dest) {
|
|
|
|
|
|
|
|
|
|
#define MAXRECURSIVE 255
|
|
|
|
|
/// @brief Reads the source and returns the string of element requested.
|
|
|
|
|
/// @param src source json array
|
|
|
|
|
/// @param src source json array [ ...., .....]
|
|
|
|
|
/// @param idx index to read
|
|
|
|
|
/// @param dest destiantion of the string
|
|
|
|
|
/// @return 0 not found else 1
|
|
|
|
|
int JSONParse::GetIdx(std::string src, int idx, std::string *dest) {
|
|
|
|
|
char recursive[MAXRECURSIVE];
|
|
|
|
|
int i = 0, rcnt = 0, cnt = 0;
|
|
|
|
|
// debug ("src:%s\n\n", src.c_str());
|
|
|
|
|
(*dest) = "";
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < MAXRECURSIVE; i++) recursive[i] = 0;
|
|
|
|
|
for (i = 0; i < (int) src.length() && rcnt < MAXRECURSIVE && cnt <= idx; i++) {
|
|
|
|
|
|
|
|
|
|
/* if (src[i] == '[' || src[i] == ']' || src[i] == '"' || src[i] == ',' || src[i] == '}' || src[i] == '{') {
|
|
|
|
|
int _i;
|
|
|
|
|
debug ("src[i]:%c i:%d rcnt:%d cnt:%d idx:%d recursive:", src[i], i, rcnt, cnt, idx);
|
|
|
|
|
for (int _i = 0; _i < rcnt; _i++) printf ("%c", recursive[_i]);
|
|
|
|
|
printf ("\n");
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
if (src[i] == '[') {
|
|
|
|
|
recursive[rcnt++] = src[i];
|
|
|
|
|
continue;
|
|
|
|
|
if (rcnt == 1) continue;
|
|
|
|
|
}
|
|
|
|
|
else if (src[i] == '{' ) recursive[rcnt++] = src[i];
|
|
|
|
|
else if (src[i] == '}' && recursive[rcnt-1] == '{') rcnt--;
|
|
|
|
|
@ -303,9 +295,11 @@ int JSONParse::GetIdx(std::string src, int idx, std::string *dest) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else if (src[i] == ']' && rcnt > 0) {
|
|
|
|
|
continue;
|
|
|
|
|
rcnt--;
|
|
|
|
|
recursive[rcnt] = 0;
|
|
|
|
|
if (rcnt == 0) continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
recursive[rcnt] = 0;
|
|
|
|
|
if (rcnt > 0 && cnt == idx) {
|
|
|
|
|
(*dest) += src[i];
|
|
|
|
|
if (src[i] == '\\') (*dest) += src[i];
|
|
|
|
|
@ -324,14 +318,19 @@ int JSONParse::GetIdx(std::string src, int idx, std::string *dest) {
|
|
|
|
|
}
|
|
|
|
|
#undef MAXRECURSIVE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int JSONParse::GetValueIdx(std::string varname, int idx, std::string *dest) {
|
|
|
|
|
std::list<JSONElement>::iterator iter;
|
|
|
|
|
|
|
|
|
|
std::string d;
|
|
|
|
|
int ret = 0;
|
|
|
|
|
if (dest == NULL) return 0;
|
|
|
|
|
|
|
|
|
|
for (iter = names.begin(); iter != names.end(); iter++) {
|
|
|
|
|
if (varname.compare(iter->name) == 0) {
|
|
|
|
|
return GetIdx(iter->value, idx, dest);
|
|
|
|
|
ret = GetIdx(iter->value, idx, &d);
|
|
|
|
|
if (d[0]== '"') *dest = d.substr(1, d.length()-2);
|
|
|
|
|
else *dest = d;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -557,7 +556,7 @@ void JSONElement::SetArray (std::string n, std::list<JSONElement> *l) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void JSONElement::SetAddArray (std::string n, std::string jpstring) {
|
|
|
|
|
void JSONElement::SetAddArrayEscaped (std::string n, std::string jpstring) {
|
|
|
|
|
std::size_t ab; // start of array [
|
|
|
|
|
std::size_t ae; // end of array ]
|
|
|
|
|
|
|
|
|
|
@ -573,9 +572,35 @@ void JSONElement::SetAddArray (std::string n, std::string jpstring) {
|
|
|
|
|
|
|
|
|
|
value = value.substr(0, ae);
|
|
|
|
|
|
|
|
|
|
if (value.length() > 2) value += ",";
|
|
|
|
|
if (value.length() > 1) value += ",";
|
|
|
|
|
value += jpstring + "]";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void JSONElement::SetAddArray (std::string n, std::string v) {
|
|
|
|
|
std::string s;
|
|
|
|
|
s = "\"" + StringAddEscape(&v) + "\"";
|
|
|
|
|
SetAddArrayEscaped(n, s);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void JSONElement::SetAddArray (std::string n, double v) {
|
|
|
|
|
std::string s = std::to_string(v);
|
|
|
|
|
SetAddArrayEscaped(n, s);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void JSONElement::SetAddArray (std::string n, int v) {
|
|
|
|
|
std::string s = std::to_string(v);
|
|
|
|
|
SetAddArrayEscaped(n, s);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void JSONElement::SetAddArray (std::string n, int64_t v) {
|
|
|
|
|
std::string s = std::to_string(v);
|
|
|
|
|
SetAddArrayEscaped(n, s);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void JSONElement::SetObject (std::string n, std::string s) {
|
|
|
|
|
|