// just store one converted report at a time StringBuffer report2json(const ConstBuf& in) { char *keys, *vals; unsigned short ir[16], ik[32], iv[32], i; StringBuffer reportJson(100, 8); // copy cuz we edit it char *report = (char*)malloc(in.length()); memcpy(report, in, in.length()); // parse this and humanize js0n((unsigned char*)report, strlen(report), ir, 16); if (!*ir) { free(report); } // TODO: Proper JSON escaping in this function reportJson.appendSprintf("{\"type\":\"%s\"", keyGet(atoi(j0g_safe(0, report, ir)))); keys = report + ir[2]; js0n((unsigned char*)keys, ir[3], ik, 32); if (!*ik) { free(report); reportJson = (char*)NULL; return reportJson; } vals = report+ir[4]; js0n((unsigned char*)vals, ir[5], iv, 32); if (!*iv) { free(report); reportJson = (char*)NULL; return reportJson; } for (i=0; ik[i]; i+=2) { reportJson.appendSprintf(",\"%s\":", keyGet(atoi(j0g_safe(i, keys, ik)))); if (vals[iv[i]-1] == '"') { iv[i]--; iv[i+1]+=2; } *(vals+iv[i]+iv[i+1]) = 0; reportJson.appendSprintf("%s", vals + iv[i]); } reportJson += "}"; free(report); return reportJson; }
// loads json array of strings, outs is optional void keyLoad(const char *array, int *outs, unsigned long at) { unsigned short *index; unsigned int i; unsigned int oi=1; if (!array || !*array) { return; } index = (unsigned short*)malloc(strlen(array)); if (!index) { outs[0] = 0; return; } j0g((char*)array, index, strlen(array)); for (i=0; index[i]; i+=2) { int k = keyMap(j0g_safe(i, (char*)array, index), at); if (outs) { outs[oi++] = k; } } if (outs) { outs[0] = oi-1; } free(index); }
// returns ["0","1","2"] or {"0":"1","2":"3"} char *packet_get_istr(packet_t p, int i) { int j; if(!p) return NULL; for(j=0;p->js[j];j+=2) { if(i*2 != j) continue; return j0g_safe(j, packet_j0g(p), p->js); } return NULL; }
char *report2json(char *in) { char *keys, *vals, report[100]; unsigned short ir[16], ik[32], iv[32], i; // copy cuz we edit it memcpy(report, in, 100); // parse this and humanize js0n((unsigned char*)report, strlen(report), ir, 16); if (!*ir) { return NULL; } sprintf(reportJson, "{\"type\":\"%s\"", keyGet(atoi(j0g_safe(0, report, ir)))); keys = report + ir[2]; js0n((unsigned char*)keys, ir[3], ik, 32); if (!*ik) { return NULL; } vals = report+ir[4]; js0n((unsigned char*)vals, ir[5], iv, 32); if (!*iv) { return NULL; } for (i=0; ik[i]; i+=2) { sprintf(reportJson + strlen(reportJson), ",\"%s\":", keyGet(atoi(j0g_safe(i, keys, ik)))); if (vals[iv[i]-1] == '"') { iv[i]--; iv[i+1]+=2; } *(vals+iv[i]+iv[i+1]) = 0; sprintf(reportJson + strlen(reportJson), "%s", vals + iv[i]); } sprintf(reportJson + strlen(reportJson), "}"); return reportJson; }
// return the null-terminated string value matching the given key char *j0g_str(char *key, char *json, unsigned short *index) { int val = j0g_val(key, json, index); if(!val) return NULL; return j0g_safe(val, json, index); }