bool Json::Parse(const string& str) { JSONNODE* node = json_parse(str.c_str()); if (node == NULL) return false; JSONNODE_ITERATOR iter = json_begin(node); while (iter != json_end(node)) { json_char* nodeName = json_name(*iter); if(string(nodeName) == "" || json_type(*iter) == JSON_NULL) { json_free(nodeName); break; } if (json_type(*iter) == JSON_NODE) { if(!Parse(nodeName, *iter)) return false; } else if(json_type(*iter) == JSON_ARRAY) { JSONNODE_ITERATOR i = json_begin(*iter); while (i != json_end(*iter)) { if(json_type(*iter) == JSON_NUMBER) { mDataFloatArray[nodeName].push_back(json_as_float(*i)); } else { mDataStrArray[nodeName].push_back(json_as_string(*i)); } i++; } } else if(json_type(*iter) == JSON_NUMBER) { mDataFloat[nodeName] = json_as_float(*iter); } else if(json_type(*iter) == JSON_BOOL) { mDataBool[nodeName] = json_as_bool(*iter); } else if(json_type(*iter) == JSON_STRING) { mDataStr[nodeName] = json_as_string(*iter); } json_free(nodeName); iter++; } json_delete(node); return true; }
/** * Builds Native Libjson Node tree recusively * \param pANode A SettingsAPI's Node. * \return libjson's equivalent to SettingNode including children. * If parentAbstractNode is null, then empty native node is returned */ JSONNODE* buildNativeJSONTreeNode(SNI* pANode) { if (pANode == NULL) { return json_new(identifyNodeType(SNI::TYPE_OBJECT)); } JSONNODE* pNNode = json_new(identifyNodeType(pANode->getType())); json_set_name(pNNode, pANode->getKey().c_str()); switch (pANode->getType()) { case SNI::TYPE_VALUE: { json_set_a(pNNode, pANode->readString().c_str()); break; } case SNI::TYPE_OBJECT: case SNI::TYPE_ARRAY: { std::vector<SNI*> children = pANode->getChildren(); std::vector<SNI*>::iterator it = children.begin(); while (it != children.end()) { SNI* cANode = *it; JSONNODE* cNChild = buildNativeJSONTreeNode(cANode); json_insert(pNNode, json_end(pNNode), cNChild); ++it; } break; } default: // Node is undefined type. break; } return pNNode; }
void JSON_GET_TEXT_ARRAY(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT json; ARRAY_TEXT values; json.fromParamAtIndex(pParams, 1); values.setSize(1); JSONNODE *n = _fromHex(json); if(n){ if(json_type(n) == JSON_ARRAY){ JSONNODE_ITERATOR i = json_begin(n); while (i != json_end(n)){ if (*i){ json_char *s = json_as_string(*i); std::wstring w = std::wstring(s); C_TEXT t; _copyString(w, t); CUTF16String u; t.copyUTF16String(&u); values.appendUTF16String(&u); json_free(s); } ++i; } } } values.toParamAtIndex(pParams, 2); }
int json_send_warn(struct asfd *asfd, const char *msg) { if(json_start(asfd) || yajl_gen_str_pair_w("warning", msg) || json_end(asfd)) return -1; return 0; }
void SdkHandler::passwordFinished(char *bufferchar) { CCLOG("in function:[%s], buff:[%s]", __FUNCTION__, bufferchar); int state = 0; int userId = 0; int flag = 0; JSONNODE *n = json_parse(bufferchar); if (n == NULL){ return; } JSONNODE_ITERATOR i = json_begin(n); while (i != json_end(n)){ CCLOG("Start Parse Json in [%s]", __FUNCTION__); if (*i == NULL){ break; } // recursively call ourselves to dig deeper into the tree if (json_type(*i) == JSON_ARRAY || json_type(*i) == JSON_NODE){ break; } // get the node name and value as a string json_char *node_name = json_name(*i); // find out where to store the values if (strcmp(node_name, "STATE") == 0){ json_int_t node_value = json_as_int(*i); state = node_value; } else if (strcmp(node_name, "USER_ID") == 0){ json_int_t node_value = json_as_int(*i); userId = node_value; } else if (strcmp(node_name, "LOGIN_KEY") == 0){ json_char *node_value = json_as_string(*i); json_free(node_value); } else if (strcmp(node_name, "ERROR_TYPE") == 0){ json_int_t node_value = json_as_int(*i); flag = node_value; } // cleanup and increment the iterator json_free(node_name); ++i; } CCLOG("state:[%d], flag:[%d]", state, flag); SdkInfoData *sdkLoginData = new SdkInfoData(); sdkLoginData->state = state; sdkLoginData->errorFlag = flag; SGNotificationCenter::sharedNotificationCenter()->postNotification(PASSWORDLAG,sdkLoginData,false); }
int json_cntr_to_file(struct asfd *asfd, struct cntr *cntr) { int ret=-1; if(json_start(asfd) || do_counters(cntr)) goto end; ret=0; end: if(json_end(asfd)) return -1; return ret; }
json_object *parse_filter(const char *s) { json_pull *jp = json_begin_string(s); json_object *filter = json_read_tree(jp); if (filter == NULL) { fprintf(stderr, "Could not parse filter %s\n", s); fprintf(stderr, "%s\n", jp->error); exit(EXIT_FAILURE); } json_disconnect(filter); json_end(jp); return filter; }
void SdkHandler::destroyGuestFinished(char *bufferchar) { CCLOG("in function:[%s], buff:[%s]", __FUNCTION__, bufferchar); int state = 0; int userId = 0; int flag = 0; JSONNODE *n = json_parse(bufferchar); if (n == NULL){ return; } JSONNODE_ITERATOR i = json_begin(n); while (i != json_end(n)){ CCLOG("Start Parse Json in [%s]", __FUNCTION__); if (*i == NULL){ break; } // recursively call ourselves to dig deeper into the tree if (json_type(*i) == JSON_ARRAY || json_type(*i) == JSON_NODE){ break; } // get the node name and value as a string json_char *node_name = json_name(*i); // find out where to store the values if (strcmp(node_name, "STATE") == 0){ json_int_t node_value = json_as_int(*i); state = node_value; } else if (strcmp(node_name, "USER_ID") == 0){ json_int_t node_value = json_as_int(*i); userId = node_value; } else if (strcmp(node_name, "LOGIN_KEY") == 0){ json_char *node_value = json_as_string(*i); json_free(node_value); } else if (strcmp(node_name, "ERROR_TYPE") == 0){ json_int_t node_value = json_as_int(*i); flag = node_value; } // cleanup and increment the iterator json_free(node_name); ++i; } }
json_object *read_filter(const char *fname) { FILE *fp = fopen(fname, "r"); if (fp == NULL) { perror(fname); exit(EXIT_FAILURE); } json_pull *jp = json_begin_file(fp); json_object *filter = json_read_tree(jp); if (filter == NULL) { fprintf(stderr, "%s: %s\n", fname, jp->error); exit(EXIT_FAILURE); } json_disconnect(filter); json_end(jp); fclose(fp); return filter; }
int json_wlparam_proc_all(JSONNODE* node, wlp_descr_t* wlp, void* params) { int ret; void* param; while(wlp->type != WLP_NULL) { JSONNODE_ITERATOR i_param = json_find(node, wlp->name), i_end = json_end(node); param = ((char*) params) + wlp->off; if(i_param == i_end) { /* If parameter is optional, try to assign it to default value */ if(wlp->flags & WLPF_OPTIONAL) { ret = wlparam_set_default(wlp, param); if(ret == WLPARAM_NO_DEFAULT) { tsload_error_msg(TSE_INTERNAL_ERROR, "Missing default value for %s", wlp->name); return WLPARAM_JSON_NOT_FOUND; } wlp++; continue; } tsload_error_msg(TSE_INVALID_DATA, "Workload parameter %s not specified", wlp->name); return WLPARAM_JSON_NOT_FOUND; } ret = json_wlparam_proc(*i_param, wlp, param); if(ret == WLPARAM_JSON_WRONG_TYPE) { tsload_error_msg(TSE_INVALID_DATA, "Workload parameter %s has wrong type", wlp->name); return ret; } if(ret == WLPARAM_JSON_OUTSIDE_RANGE) { tsload_error_msg(TSE_INVALID_DATA, "Workload parameter %s outside defined range", wlp->name); return ret; } wlp++; } return WLPARAM_JSON_OK; }
bool admJsonToCouple::scan( void *xnode,string name) { JSONNODE *node=(JSONNODE *)xnode; if (!node){ ADM_error("Invalid JSON Node\n"); return false; } JSONNODE_ITERATOR i = json_begin(node); while (i != json_end(node)){ if (*i == NULL){ ADM_error("Invalid JSON Node\n"); return false; } json_char *node_name = json_name(*i); //printf("Node :%s\n",node_name); // recursively call ourselves to dig deeper into the tree if (json_type(*i) == JSON_ARRAY || json_type(*i) == JSON_NODE) { if(name=="") scan(*i,string(node_name)); else scan(*i,name+string(".")+string(node_name)); } else { keyVal k; json_char *node_value = json_as_string(*i); if(name=="") k.key=string(node_name); else k.key=string(name)+string(".")+string(node_name); k.value=string(node_value); readItems.push_back(k); json_free(node_value); } json_free(node_name); ++i; } return true; }
int json_send(struct asfd *asfd, struct cstat *clist, struct cstat *cstat, struct bu *bu, const char *logfile, const char *browse, int use_cache) { int ret=-1; struct cstat *c; if(json_start(asfd) || json_clients()) goto end; if(cstat && bu) { if(json_send_client_backup(asfd, cstat, bu, NULL, logfile, browse, use_cache)) goto end; } else if(cstat) { if(json_send_client_backup_list(asfd, cstat, use_cache)) goto end; } else for(c=clist; c; c=c->next) { if(!c->permitted) continue; if(json_send_client_backup(asfd, c, bu_find_current(c->bu), bu_find_working_or_finishing(c->bu), NULL, NULL, use_cache)) goto end; } ret=0; end: if(json_clients_end() || json_end(asfd)) return -1; return ret; }
void JSON_DELETE_ITEM_BY_NAME(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT json; C_TEXT name; C_LONGINT option; json.fromParamAtIndex(pParams, 1); name.fromParamAtIndex(pParams, 2); option.fromParamAtIndex(pParams, 3); JSONNODE *n = _fromHex(json); if(n){ std::wstring w; _copyString(name, w); JSONNODE_ITERATOR i; switch (option.getIntValue()) { case 1: i = json_find_nocase(n, w.c_str()); break; default: i = json_find(n, w.c_str()); break; } /* while (i != json_end(n)){ json_erase(n, i); ++i; } */ if (i != json_end(n)){ json_erase(n, i); } } }
void JSON_GET_BOOL_ARRAY(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT json; ARRAY_BOOLEAN values; json.fromParamAtIndex(pParams, 1); values.setSize(1); JSONNODE *n = _fromHex(json); if(n){ if(json_type(n) == JSON_ARRAY){ JSONNODE_ITERATOR i = json_begin(n); while (i != json_end(n)){ if (*i){ values.appendBooleanValue(json_as_bool(*i)); } ++i; } } } values.toParamAtIndex(pParams, 2); }
void JSON_DELETE_ITEM_BY_POSITION(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT json; C_LONGINT pos; json.fromParamAtIndex(pParams, 1); pos.fromParamAtIndex(pParams, 2); json.fromParamAtIndex(pParams, 1); JSONNODE *n = _fromHex(json); if(n){ JSONNODE_ITERATOR i = json_begin(n); int p = 1; while (i != json_end(n)){ if(p == pos.getIntValue()){ json_erase(n, i); break; } ++i; } } }
int read_json(int argc, char **argv, char *fname, const char *layername, int maxzoom, int minzoom, sqlite3 *outdb, struct pool *exclude, struct pool *include, int exclude_all, double droprate, int buffer, const char *tmpdir, double gamma, char *prevent) { int ret = EXIT_SUCCESS; char metaname[strlen(tmpdir) + strlen("/meta.XXXXXXXX") + 1]; char geomname[strlen(tmpdir) + strlen("/geom.XXXXXXXX") + 1]; char indexname[strlen(tmpdir) + strlen("/index.XXXXXXXX") + 1]; sprintf(metaname, "%s%s", tmpdir, "/meta.XXXXXXXX"); sprintf(geomname, "%s%s", tmpdir, "/geom.XXXXXXXX"); sprintf(indexname, "%s%s", tmpdir, "/index.XXXXXXXX"); int metafd = mkstemp(metaname); if (metafd < 0) { perror(metaname); exit(EXIT_FAILURE); } int geomfd = mkstemp(geomname); if (geomfd < 0) { perror(geomname); exit(EXIT_FAILURE); } int indexfd = mkstemp(indexname); if (indexfd < 0) { perror(indexname); exit(EXIT_FAILURE); } FILE *metafile = fopen(metaname, "wb"); if (metafile == NULL) { perror(metaname); exit(EXIT_FAILURE); } FILE *geomfile = fopen(geomname, "wb"); if (geomfile == NULL) { perror(geomname); exit(EXIT_FAILURE); } FILE *indexfile = fopen(indexname, "wb"); if (indexfile == NULL) { perror(indexname); exit(EXIT_FAILURE); } long long metapos = 0; long long geompos = 0; long long indexpos = 0; unlink(metaname); unlink(geomname); unlink(indexname); unsigned file_bbox[] = { UINT_MAX, UINT_MAX, 0, 0 }; unsigned midx = 0, midy = 0; long long seq = 0; int nlayers = argc; if (nlayers == 0) { nlayers = 1; } int n; for (n = 0; n < nlayers; n++) { json_pull *jp; const char *reading; FILE *fp; long long found_hashes = 0; long long found_features = 0; if (n >= argc) { reading = "standard input"; fp = stdin; } else { reading = argv[n]; fp = fopen(argv[n], "r"); if (fp == NULL) { perror(argv[n]); continue; } } jp = json_begin_file(fp); while (1) { json_object *j = json_read(jp); if (j == NULL) { if (jp->error != NULL) { fprintf(stderr, "%s:%d: %s\n", reading, jp->line, jp->error); } json_free(jp->root); break; } if (j->type == JSON_HASH) { found_hashes++; if (found_hashes == 50 && found_features == 0) { fprintf(stderr, "%s:%d: Not finding any GeoJSON features in input. Is your file just bare geometries?\n", reading, jp->line); break; } } json_object *type = json_hash_get(j, "type"); if (type == NULL || type->type != JSON_STRING || strcmp(type->string, "Feature") != 0) { continue; } found_features++; json_object *geometry = json_hash_get(j, "geometry"); if (geometry == NULL) { fprintf(stderr, "%s:%d: feature with no geometry\n", reading, jp->line); json_free(j); continue; } json_object *geometry_type = json_hash_get(geometry, "type"); if (geometry_type == NULL) { static int warned = 0; if (!warned) { fprintf(stderr, "%s:%d: null geometry (additional not reported)\n", reading, jp->line); warned = 1; } json_free(j); continue; } if (geometry_type->type != JSON_STRING) { fprintf(stderr, "%s:%d: geometry without type\n", reading, jp->line); json_free(j); continue; } json_object *properties = json_hash_get(j, "properties"); if (properties == NULL || (properties->type != JSON_HASH && properties->type != JSON_NULL)) { fprintf(stderr, "%s:%d: feature without properties hash\n", reading, jp->line); json_free(j); continue; } json_object *coordinates = json_hash_get(geometry, "coordinates"); if (coordinates == NULL || coordinates->type != JSON_ARRAY) { fprintf(stderr, "%s:%d: feature without coordinates array\n", reading, jp->line); json_free(j); continue; } int t; for (t = 0; t < GEOM_TYPES; t++) { if (strcmp(geometry_type->string, geometry_names[t]) == 0) { break; } } if (t >= GEOM_TYPES) { fprintf(stderr, "%s:%d: Can't handle geometry type %s\n", reading, jp->line, geometry_type->string); json_free(j); continue; } { unsigned bbox[] = { UINT_MAX, UINT_MAX, 0, 0 }; int nprop = 0; if (properties->type == JSON_HASH) { nprop = properties->length; } long long metastart = metapos; char *metakey[nprop]; char *metaval[nprop]; int metatype[nprop]; int m = 0; int i; for (i = 0; i < nprop; i++) { if (properties->keys[i]->type == JSON_STRING) { if (exclude_all) { if (!is_pooled(include, properties->keys[i]->string, VT_STRING)) { continue; } } else if (is_pooled(exclude, properties->keys[i]->string, VT_STRING)) { continue; } metakey[m] = properties->keys[i]->string; if (properties->values[i] != NULL && properties->values[i]->type == JSON_STRING) { metatype[m] = VT_STRING; metaval[m] = properties->values[i]->string; m++; } else if (properties->values[i] != NULL && properties->values[i]->type == JSON_NUMBER) { metatype[m] = VT_NUMBER; metaval[m] = properties->values[i]->string; m++; } else if (properties->values[i] != NULL && (properties->values[i]->type == JSON_TRUE || properties->values[i]->type == JSON_FALSE)) { metatype[m] = VT_BOOLEAN; metaval[m] = properties->values[i]->type == JSON_TRUE ? "true" : "false"; m++; } else if (properties->values[i] != NULL && (properties->values[i]->type == JSON_NULL)) { ; } else { fprintf(stderr, "%s:%d: Unsupported property type for %s\n", reading, jp->line, properties->keys[i]->string); json_free(j); continue; } } } serialize_int(metafile, m, &metapos, fname); for (i = 0; i < m; i++) { serialize_int(metafile, metatype[i], &metapos, fname); serialize_string(metafile, metakey[i], &metapos, fname); serialize_string(metafile, metaval[i], &metapos, fname); } long long geomstart = geompos; serialize_byte(geomfile, mb_geometry[t], &geompos, fname); serialize_byte(geomfile, n, &geompos, fname); serialize_long_long(geomfile, metastart, &geompos, fname); parse_geometry(t, coordinates, bbox, &geompos, geomfile, VT_MOVETO, fname, jp); serialize_byte(geomfile, VT_END, &geompos, fname); /* * Note that minzoom for lines is the dimension * of the geometry in world coordinates, but * for points is the lowest zoom level (in tiles, * not in pixels) at which it should be drawn. * * So a line that is too small for, say, z8 * will have minzoom of 18 (if tile detail is 10), * not 8. */ int minzoom = 0; if (mb_geometry[t] == VT_LINE) { for (minzoom = 0; minzoom < 31; minzoom++) { unsigned mask = 1 << (32 - (minzoom + 1)); if (((bbox[0] & mask) != (bbox[2] & mask)) || ((bbox[1] & mask) != (bbox[3] & mask))) { break; } } } else if (mb_geometry[t] == VT_POINT) { double r = ((double) rand()) / RAND_MAX; if (r == 0) { r = .00000001; } minzoom = maxzoom - floor(log(r) / - log(droprate)); } serialize_byte(geomfile, minzoom, &geompos, fname); struct index index; index.start = geomstart; index.end = geompos; index.index = encode(bbox[0] / 2 + bbox[2] / 2, bbox[1] / 2 + bbox[3] / 2); fwrite_check(&index, sizeof(struct index), 1, indexfile, fname); indexpos += sizeof(struct index); for (i = 0; i < 2; i++) { if (bbox[i] < file_bbox[i]) { file_bbox[i] = bbox[i]; } } for (i = 2; i < 4; i++) { if (bbox[i] > file_bbox[i]) { file_bbox[i] = bbox[i]; } } if (seq % 10000 == 0) { fprintf(stderr, "Read %.2f million features\r", seq / 1000000.0); } seq++; } json_free(j); /* XXX check for any non-features in the outer object */ } json_end(jp); fclose(fp); } fclose(metafile); fclose(geomfile); fclose(indexfile); struct stat geomst; struct stat metast; if (fstat(geomfd, &geomst) != 0) { perror("stat geom\n"); exit(EXIT_FAILURE); } if (fstat(metafd, &metast) != 0) { perror("stat meta\n"); exit(EXIT_FAILURE); } if (geomst.st_size == 0 || metast.st_size == 0) { fprintf(stderr, "did not read any valid geometries\n"); exit(EXIT_FAILURE); } char *meta = (char *) mmap(NULL, metast.st_size, PROT_READ, MAP_PRIVATE, metafd, 0); if (meta == MAP_FAILED) { perror("mmap meta"); exit(EXIT_FAILURE); } struct pool file_keys1[nlayers]; struct pool *file_keys[nlayers]; int i; for (i = 0; i < nlayers; i++) { pool_init(&file_keys1[i], 0); file_keys[i] = &file_keys1[i]; } char *layernames[nlayers]; for (i = 0; i < nlayers; i++) { if (argc <= 1 && layername != NULL) { layernames[i] = strdup(layername); } else { char *src = argv[i]; if (argc < 1) { src = fname; } char *trunc = layernames[i] = malloc(strlen(src) + 1); const char *ocp, *use = src; for (ocp = src; *ocp; ocp++) { if (*ocp == '/' && ocp[1] != '\0') { use = ocp + 1; } } strcpy(trunc, use); char *cp = strstr(trunc, ".json"); if (cp != NULL) { *cp = '\0'; } cp = strstr(trunc, ".mbtiles"); if (cp != NULL) { *cp = '\0'; } layername = trunc; char *out = trunc; for (cp = trunc; *cp; cp++) { if (isalpha(*cp) || isdigit(*cp) || *cp == '_') { *out++ = *cp; } } *out = '\0'; printf("using layer %d name %s\n", i, trunc); } } /* Sort the index by geometry */ { int bytes = sizeof(struct index); fprintf(stderr, "Sorting %lld features\n", (long long) indexpos / bytes); int page = sysconf(_SC_PAGESIZE); long long unit = (50 * 1024 * 1024 / bytes) * bytes; while (unit % page != 0) { unit += bytes; } int nmerges = (indexpos + unit - 1) / unit; struct merge merges[nmerges]; long long start; for (start = 0; start < indexpos; start += unit) { long long end = start + unit; if (end > indexpos) { end = indexpos; } if (nmerges != 1) { fprintf(stderr, "Sorting part %lld of %d\r", start / unit + 1, nmerges); } merges[start / unit].start = start; merges[start / unit].end = end; merges[start / unit].next = NULL; void *map = mmap(NULL, end - start, PROT_READ | PROT_WRITE, MAP_PRIVATE, indexfd, start); if (map == MAP_FAILED) { perror("mmap"); exit(EXIT_FAILURE); } qsort(map, (end - start) / bytes, bytes, indexcmp); // Sorting and then copying avoids the need to // write out intermediate stages of the sort. void *map2 = mmap(NULL, end - start, PROT_READ | PROT_WRITE, MAP_SHARED, indexfd, start); if (map2 == MAP_FAILED) { perror("mmap (write)"); exit(EXIT_FAILURE); } memcpy(map2, map, end - start); munmap(map, end - start); munmap(map2, end - start); } if (nmerges != 1) { fprintf(stderr, "\n"); } void *map = mmap(NULL, indexpos, PROT_READ, MAP_PRIVATE, indexfd, 0); if (map == MAP_FAILED) { perror("mmap"); exit(EXIT_FAILURE); } FILE *f = fopen(indexname, "w"); if (f == NULL) { perror(indexname); exit(EXIT_FAILURE); } merge(merges, nmerges, (unsigned char *) map, f, bytes, indexpos / bytes); munmap(map, indexpos); fclose(f); close(indexfd); } /* Copy geometries to a new file in index order */ indexfd = open(indexname, O_RDONLY); if (indexfd < 0) { perror("reopen sorted index"); exit(EXIT_FAILURE); } struct index *index_map = mmap(NULL, indexpos, PROT_READ, MAP_PRIVATE, indexfd, 0); if (index_map == MAP_FAILED) { perror("mmap index"); exit(EXIT_FAILURE); } unlink(indexname); char *geom_map = mmap(NULL, geomst.st_size, PROT_READ, MAP_PRIVATE, geomfd, 0); if (geom_map == MAP_FAILED) { perror("mmap unsorted geometry"); exit(EXIT_FAILURE); } if (close(geomfd) != 0) { perror("close unsorted geometry"); } sprintf(geomname, "%s%s", tmpdir, "/geom.XXXXXXXX"); geomfd = mkstemp(geomname); if (geomfd < 0) { perror(geomname); exit(EXIT_FAILURE); } geomfile = fopen(geomname, "wb"); if (geomfile == NULL) { perror(geomname); exit(EXIT_FAILURE); } { geompos = 0; /* initial tile is 0/0/0 */ serialize_int(geomfile, 0, &geompos, fname); serialize_uint(geomfile, 0, &geompos, fname); serialize_uint(geomfile, 0, &geompos, fname); long long i; long long sum = 0; long long progress = 0; for (i = 0; i < indexpos / sizeof(struct index); i++) { fwrite_check(geom_map + index_map[i].start, sizeof(char), index_map[i].end - index_map[i].start, geomfile, fname); sum += index_map[i].end - index_map[i].start; long long p = 1000 * i / (indexpos / sizeof(struct index)); if (p != progress) { fprintf(stderr, "Reordering geometry: %3.1f%%\r", p / 10.0); progress = p; } } /* end of tile */ serialize_byte(geomfile, -2, &geompos, fname); fclose(geomfile); } if (munmap(index_map, indexpos) != 0) { perror("unmap sorted index"); } if (munmap(geom_map, geomst.st_size) != 0) { perror("unmap unsorted geometry"); } if (close(indexfd) != 0) { perror("close sorted index"); } /* Traverse and split the geometries for each zoom level */ geomfd = open(geomname, O_RDONLY); if (geomfd < 0) { perror("reopen sorted geometry"); exit(EXIT_FAILURE); } unlink(geomname); if (fstat(geomfd, &geomst) != 0) { perror("stat sorted geom\n"); exit(EXIT_FAILURE); } int fd[4]; off_t size[4]; fd[0] = geomfd; size[0] = geomst.st_size; int j; for (j = 1; j < 4; j++) { fd[j] = -1; size[j] = 0; } fprintf(stderr, "%lld features, %lld bytes of geometry, %lld bytes of metadata\n", seq, (long long) geomst.st_size, (long long) metast.st_size); int written = traverse_zooms(fd, size, meta, file_bbox, file_keys, &midx, &midy, layernames, maxzoom, minzoom, outdb, droprate, buffer, fname, tmpdir, gamma, nlayers, prevent); if (maxzoom != written) { fprintf(stderr, "\n\n\n*** NOTE TILES ONLY COMPLETE THROUGH ZOOM %d ***\n\n\n", written); maxzoom = written; ret = EXIT_FAILURE; } if (munmap(meta, metast.st_size) != 0) { perror("munmap meta"); } if (close(metafd) < 0) { perror("close meta"); } double minlat = 0, minlon = 0, maxlat = 0, maxlon = 0, midlat = 0, midlon = 0; tile2latlon(midx, midy, maxzoom, &maxlat, &minlon); tile2latlon(midx + 1, midy + 1, maxzoom, &minlat, &maxlon); midlat = (maxlat + minlat) / 2; midlon = (maxlon + minlon) / 2; tile2latlon(file_bbox[0], file_bbox[1], 32, &maxlat, &minlon); tile2latlon(file_bbox[2], file_bbox[3], 32, &minlat, &maxlon); if (midlat < minlat) { midlat = minlat; } if (midlat > maxlat) { midlat = maxlat; } if (midlon < minlon) { midlon = minlon; } if (midlon > maxlon) { midlon = maxlon; } mbtiles_write_metadata(outdb, fname, layernames, minzoom, maxzoom, minlat, minlon, maxlat, maxlon, midlat, midlon, file_keys, nlayers); // XXX layers for (i = 0; i < nlayers; i++) { pool_free_strings(&file_keys1[i]); free(layernames[i]); } return ret; }
// master不保存settings,直接传给plugin_init() int load_task_setting(ls_master_t* master) { LOG("load_task_setting()\n"); const char* setting_file = "task/setting.json"; char* buf; long len; FILE* f = fopen(setting_file, "r"); if (f == NULL) { LOGE("Failed to open setting_file: %s\n", setting_file);// TODO errno return -1; } fseek(f, 0, SEEK_END); len = ftell(f); rewind(f); buf = (char*) malloc(len + 1); if (NULL == buf) { LOGE("Failed to malloc for task_setting: %s\n", strerror(errno)); return -1; } len = fread(buf, 1, len, f); buf[len] = '\0'; JSONNODE* setting = json_parse(buf); master->num_plugins = json_size(setting); master->plugins = (ls_plugin_t*)malloc(master->num_plugins * sizeof(ls_plugin_t)); ls_plugin_t* plugin; size_t plugin_index = 0; char plugin_path[128]; for (JSONNODE_ITERATOR i = json_begin(setting); i != json_end(setting); ++i, ++plugin_index) { plugin = master->plugins + plugin_index; plugin->plugin_index = plugin_index; json_char* plugin_name = json_name(*i); if (NULL == plugin_name) { LOGE("Failed to get plugin_name from task_setting\n"); return -1; } snprintf(plugin_path, sizeof(plugin_path), "plugin/%s/%s.so", plugin_name, plugin_name); if (uv_dlopen(plugin_path, &plugin->plugin_lib) < 0) { LOGE(" Failed to uv_dlopen: %s\n", uv_dlerror(&plugin->plugin_lib)); return -1; } if (uv_dlsym(&(plugin->plugin_lib), "plugin_declare", (void**)&(plugin->plugin_declare)) < 0) { LOGE(" Failed to uv_dlsym\n"); return -1; } if ((plugin->plugin_declare(plugin) < 0)) { LOGE(" Failed to plugin_declare\n"); return -1; } JSONNODE* settings = *i; // if (plugin->master_init != NULL && (plugin->master_init)(master, settings) < 0) if (plugin->plugin_init != NULL && (plugin->plugin_init)(settings) < 0) { LOGE("ERROR failed to plugin_init()\n"); return -1; } } // TODO json_free return 0; }
void SdkHandler::thirdLoginFinished(char *bufferchar) { CCLOG("in function:[%s], buff:[%s]", __FUNCTION__, bufferchar); CCLOG("userName:[%s], password:[%s]", userName.c_str(), password.c_str()); int state = 0; int userId = 0; int flag = 0; const char* loginKey = NULL; JSONNODE *n = json_parse(bufferchar); if (n == NULL){ return; } JSONNODE_ITERATOR i = json_begin(n); while (i != json_end(n)){ CCLOG("Start Parse Json in [%s]", __FUNCTION__); if (*i == NULL){ break; } // recursively call ourselves to dig deeper into the tree if (json_type(*i) == JSON_ARRAY || json_type(*i) == JSON_NODE){ break; } // get the node name and value as a string json_char *node_name = json_name(*i); // find out where to store the values if (strcmp(node_name, "STATE") == 0){ json_int_t node_value = json_as_int(*i); state = node_value; } else if (strcmp(node_name, "USER_ID") == 0){ json_int_t node_value = json_as_int(*i); userId = node_value; } else if (strcmp(node_name, "LOGIN_KEY") == 0){ json_char *node_value = json_as_string(*i); loginKey = node_value; } else if (strcmp(node_name, "ERROR_TYPE") == 0){ json_int_t node_value = json_as_int(*i); flag = node_value; } // cleanup and increment the iterator json_free(node_name); ++i; } if (flag == 20) { SGNotificationCenter::sharedNotificationCenter()->postNotification(INVALID_INFO_TIP,new CCInteger(20),false); return ; } CCLOG("state:[%d], err_type:[%d]", state, flag); if (state == 1) { SdkInfoData *sdkLoginData = new SdkInfoData(); sdkLoginData->accountId = userId; sdkLoginData->errorFlag = flag; sdkLoginData->loginKey = std::string(loginKey); sdkLoginData->userName = this->userName; sdkLoginData->password = this->password; sdkLoginData->isEx = this->isExist; CCLOG("loginKey:[%s]", loginKey); //需要将登陆数据写入本地,在此非主线程,需要将当前数据发至主线程写入本地文件 SGNotificationCenter::sharedNotificationCenter()->postNotification(THIRDPARTYLOGIN,sdkLoginData,false); } else if (flag == 13) { SGNotificationCenter::sharedNotificationCenter()->postNotification(BIND_FAILED, false); } else { CCLOG("Login Failed!"); } }
void JSON_GET_CHILD_NODES(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT json; ARRAY_TEXT nodes; ARRAY_LONGINT types; ARRAY_TEXT names; json.fromParamAtIndex(pParams, 1); nodes.setSize(1); types.setSize(1); names.setSize(1); JSONNODE *n = _fromHex(json); if(n){ JSONNODE_ITERATOR i = json_begin(n); while (i != json_end(n)){ if (*i){ json_char *s = json_name(*i); std::wstring w = std::wstring(s); C_TEXT t; _copyString(w, t); json_free(s); CUTF16String nodeName; t.copyUTF16String(&nodeName); names.appendUTF16String(&nodeName); C_TEXT h; _toHex(*i, h); CUTF16String nodeRef; h.copyUTF16String(&nodeRef); nodes.appendUTF16String(&nodeRef); switch (json_type(*i)) { case JSON_NULL: types.appendIntValue(0); break; case JSON_STRING: types.appendIntValue(1); break; case JSON_NUMBER: types.appendIntValue(2); break; case JSON_BOOL: types.appendIntValue(3); break; case JSON_ARRAY: types.appendIntValue(4); break; case JSON_NODE: types.appendIntValue(5); break; } } ++i; } } nodes.toParamAtIndex(pParams, 2); types.toParamAtIndex(pParams, 3); names.toParamAtIndex(pParams, 4); }
void SdkHandler::registFinished(char *bufferchar) { CCLOG("in function:[%s], buff:[%s]", __FUNCTION__, bufferchar); int state = 0; int userId = 0; int flag = 0; const char* loginKey = NULL; JSONNODE *n = json_parse(bufferchar); if (n == NULL){ return; } JSONNODE_ITERATOR i = json_begin(n); while (i != json_end(n)){ CCLOG("Start Parse Json in [%s]", __FUNCTION__); if (*i == NULL){ break; } // recursively call ourselves to dig deeper into the tree if (json_type(*i) == JSON_ARRAY || json_type(*i) == JSON_NODE){ break; } // get the node name and value as a string json_char *node_name = json_name(*i); // find out where to store the values if (strcmp(node_name, "STATE") == 0){ json_int_t node_value = json_as_int(*i); state = node_value; } else if (strcmp(node_name, "USER_ID") == 0){ json_int_t node_value = json_as_int(*i); userId = node_value; } else if (strcmp(node_name, "LOGIN_KEY") == 0){ json_char *node_value = json_as_string(*i); loginKey = node_value; } else if (strcmp(node_name, "ERROR_TYPE") == 0){ json_int_t node_value = json_as_int(*i); flag = node_value; } // cleanup and increment the iterator json_free(node_name); ++i; } //这里做状态判定 if (flag == 10) { SGNotificationCenter::sharedNotificationCenter()->postNotification(INVALID_INFO_TIP,new CCInteger(10),false); return ; } else if (flag == 11) { SGNotificationCenter::sharedNotificationCenter()->postNotification(INVALID_INFO_TIP,new CCInteger(11),false); return ; } else if (flag == 12) { SGNotificationCenter::sharedNotificationCenter()->postNotification(INVALID_INFO_TIP,new CCInteger(12),false); return ; } CCLOG("Json parse completed! in fun : [%s]", __FUNCTION__); SdkInfoData *sdkLoginData = new SdkInfoData(); sdkLoginData->state = state; sdkLoginData->accountId = userId; sdkLoginData->flag = flag; sdkLoginData->loginKey = std::string(loginKey); sdkLoginData->userName = userName; sdkLoginData->password = password; sdkLoginData->isEx = true; CCLOG("####state:[%d]m accountId : [%d], flag : [%d], loginKey:[%s]####", state, userId, flag, loginKey); SGNotificationCenter::sharedNotificationCenter()->postNotification(REREGISTFLAG,sdkLoginData,false); }
void TestSuite::TestInspectors(void){ UnitTest::SetPrefix("TestInspectors.cpp - Inspectors"); #ifdef JSON_LIBRARY JSONNODE * test = json_new(JSON_NULL); assertEquals(json_type(test), JSON_NULL); json_char * res = json_as_string(test); assertCStringSame(res, JSON_TEXT("")); json_free(res); assertEquals_Primitive(json_as_int(test), 0); assertEquals_Primitive(json_as_float(test), 0.0f); assertEquals(json_as_bool(test), false); json_set_f(test, 15.5f); assertEquals(json_type(test), JSON_NUMBER); #ifdef JSON_CASTABLE res = json_as_string(test); assertCStringSame(res, JSON_TEXT("15.5")); json_free(res); #endif assertEquals_Primitive(json_as_int(test), 15); assertEquals_Primitive(json_as_float(test), 15.5f); #ifdef JSON_CASTABLE assertEquals(json_as_bool(test), true); #endif json_set_f(test, 0.0f); assertEquals(json_type(test), JSON_NUMBER); #ifdef JSON_CASTABLE res = json_as_string(test); assertCStringSame(res, JSON_TEXT("0")); json_free(res); #endif assertEquals_Primitive(json_as_int(test), 0); assertEquals_Primitive(json_as_float(test), 0.0f); #ifdef JSON_CASTABLE assertEquals(json_as_bool(test), false); #endif json_set_b(test, true); assertEquals(json_type(test), JSON_BOOL); #ifdef JSON_CASTABLE res = json_as_string(test); assertCStringSame(res, JSON_TEXT("true")); json_free(res); assertEquals_Primitive(json_as_int(test), 1); assertEquals_Primitive(json_as_float(test), 1.0f); #endif assertEquals(json_as_bool(test), true); json_set_b(test, false); assertEquals(json_type(test), JSON_BOOL); #ifdef JSON_CASTABLE res = json_as_string(test); assertCStringSame(res, JSON_TEXT("false")); json_free(res); assertEquals_Primitive(json_as_int(test), 0); assertEquals_Primitive(json_as_float(test), 0.0f); #endif assertEquals(json_as_bool(test), false); #ifdef JSON_CASTABLE json_cast(test, JSON_NODE); assertEquals(json_type(test), JSON_NODE); assertEquals(json_size(test), 0); json_push_back(test, json_new_a(JSON_TEXT("hi"), JSON_TEXT("world"))); json_push_back(test, json_new_a(JSON_TEXT("hello"), JSON_TEXT("mars"))); json_push_back(test, json_new_a(JSON_TEXT("salut"), JSON_TEXT("france"))); assertEquals(json_size(test), 3); TestSuite::testParsingItself(test); JSONNODE * casted = json_as_array(test); #ifdef JSON_UNIT_TEST assertNotEquals(((JSONNode*)casted) -> internal, ((JSONNode*)test) -> internal); #endif assertEquals(json_type(casted), JSON_ARRAY); assertEquals(json_type(test), JSON_NODE); assertEquals(json_size(test), 3); assertEquals(json_size(casted), 3); TestSuite::testParsingItself(casted); #endif UnitTest::SetPrefix("TestInspectors.cpp - Location"); #ifdef JSON_CASTABLE #define CheckAt(parent, locale, text)\ if(JSONNODE * temp = json_at(parent, locale)){\ json_char * _res = json_as_string(temp);\ assertCStringSame(_res, text);\ json_free(_res);\ } else {\ FAIL(std::string("CheckAt: ") + #parent + "[" + #locale + "]");\ } #define CheckNameAt(parent, locale, text)\ if(JSONNODE * temp = json_at(parent, locale)){\ json_char * _res = json_name(temp);\ assertCStringSame(_res, text);\ json_free(_res);\ } else {\ FAIL(std::string("CheckNameAt: ") + #parent + "[" + #locale + "]");\ } CheckAt(casted, 0, JSON_TEXT("world")); CheckAt(casted, 1, JSON_TEXT("mars")); CheckAt(casted, 2, JSON_TEXT("france")); CheckNameAt(casted, 0, JSON_TEXT("")); CheckNameAt(casted, 1, JSON_TEXT("")); CheckNameAt(casted, 2, JSON_TEXT("")); CheckAt(test, 0, JSON_TEXT("world")); CheckAt(test, 1, JSON_TEXT("mars")); CheckAt(test, 2, JSON_TEXT("france")); CheckNameAt(test, 0, JSON_TEXT("hi")); CheckNameAt(test, 1, JSON_TEXT("hello")); CheckNameAt(test, 2, JSON_TEXT("salut")); #define CheckGet(parent, locale, text)\ if(JSONNODE * temp = json_get(parent, locale)){\ json_char * _res = json_as_string(temp);\ assertCStringSame(_res, text);\ json_free(_res);\ } else {\ FAIL(std::string("CheckGet: ") + #parent + "[" + #locale + "]");\ } #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS #define CheckGetNoCase(parent, locale, text)\ if(JSONNODE * temp = json_get_nocase(parent, locale)){\ json_char * _res = json_as_string(temp);\ assertCStringSame(_res, text);\ json_free(_res);\ } else {\ FAIL(std::string("CheckGetNoCase: ") + #parent + "[" + #locale + "]");\ } #else #define CheckGetNoCase(parent, locale, text) #endif CheckGet(test, JSON_TEXT("hi"), JSON_TEXT("world")); CheckGetNoCase(test, JSON_TEXT("HI"), JSON_TEXT("world")); CheckGet(test, JSON_TEXT("hello"), JSON_TEXT("mars")); CheckGetNoCase(test, JSON_TEXT("HELLO"), JSON_TEXT("mars")); CheckGet(test, JSON_TEXT("salut"), JSON_TEXT("france")); CheckGetNoCase(test, JSON_TEXT("SALUT"), JSON_TEXT("france")); assertNull(json_get(test, JSON_TEXT("meh"))); #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS assertNull(json_get_nocase(test, JSON_TEXT("meh"))); #endif #endif #ifdef JSON_ITERATORS #ifdef JSON_CASTABLE UnitTest::SetPrefix("TestInspectors.cpp - Iterators"); for(JSONNODE_ITERATOR it = json_begin(casted), end = json_end(casted); it != end; ++it){ json_char * _res = json_name(*it); assertCStringSame(_res, JSON_TEXT("")); json_free(_res); } #endif #endif #ifdef JSON_BINARY UnitTest::SetPrefix("TestInspectors.cpp - Binary"); json_set_binary(test, (const unsigned char *)"Hello World", 11); assertEquals(json_type(test), JSON_STRING); json_char * _res = json_as_string(test); assertCStringSame(_res, JSON_TEXT("SGVsbG8gV29ybGQ=")); json_free(_res); unsigned long i; if(char * bin = (char*)json_as_binary(test, &i)){ assertEquals(i, 11); char * terminated = (char*)std::memcpy(std::malloc(i + 1), bin, i); terminated[i] = '\0'; assertCStringEquals(terminated, "Hello World"); json_free(bin); std::free(terminated); } else { FAIL("as_binary failed"); } json_set_a(test, JSON_TEXT("Hello World")); assertEquals(json_type(test), JSON_STRING); _res = json_as_string(test); assertCStringSame(_res, JSON_TEXT("Hello World")); json_free(_res); #ifdef JSON_SAFE assertEquals(json_as_binary(test, &i), 0); assertEquals(i, 0); #endif #endif json_delete(test); #ifdef JSON_CASTABLE json_delete(casted); #endif #else JSONNode test = JSONNode(JSON_NULL); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("")); assertEquals(test.as_int(), 0); assertEquals(test.as_float(), 0.0f); assertEquals(test.as_bool(), false); #endif test = 15.5f; assertEquals(test.type(), JSON_NUMBER); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("15.5")); #endif assertEquals(test.as_int(), 15); assertEquals(test.as_float(), 15.5f); #ifdef JSON_CASTABLE assertEquals(test.as_bool(), true); #endif test = 0.0f; assertEquals(test.type(), JSON_NUMBER); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("0")); #endif assertEquals(test.as_int(), 0); assertEquals(test.as_float(), 0.0f); #ifdef JSON_CASTABLE assertEquals(test.as_bool(), false); #endif test = true; assertEquals(test.type(), JSON_BOOL); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("true")); assertEquals(test.as_int(), 1); assertEquals(test.as_float(), 1.0f); #endif assertEquals(test.as_bool(), true); test = false; assertEquals(test.type(), JSON_BOOL); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("false")); assertEquals(test.as_int(), 0); assertEquals(test.as_float(), 0.0f); #endif assertEquals(test.as_bool(), false); #ifdef JSON_CASTABLE test.cast(JSON_NODE); #else test = JSONNode(JSON_NODE); #endif assertEquals(test.type(), JSON_NODE); assertEquals(test.size(), 0); test.push_back(JSONNode(JSON_TEXT("hi"), JSON_TEXT("world"))); test.push_back(JSONNode(JSON_TEXT("hello"), JSON_TEXT("mars"))); test.push_back(JSONNode(JSON_TEXT("salut"), JSON_TEXT("france"))); assertEquals(test.size(), 3); TestSuite::testParsingItself(test); #ifdef JSON_CASTABLE JSONNode casted = test.as_array(); #ifdef JSON_UNIT_TEST assertNotEquals(casted.internal, test.internal); #endif assertEquals(casted.type(), JSON_ARRAY); assertEquals(test.type(), JSON_NODE); assertEquals(test.size(), 3); assertEquals(casted.size(), 3); TestSuite::testParsingItself(casted); #endif UnitTest::SetPrefix("TestInspectors.cpp - Location"); try { #ifdef JSON_CASTABLE assertEquals(casted.at(0), JSON_TEXT("world")); assertEquals(casted.at(1), JSON_TEXT("mars")); assertEquals(casted.at(2), JSON_TEXT("france")); assertEquals(casted.at(0).name(), JSON_TEXT("")); assertEquals(casted.at(1).name(), JSON_TEXT("")); assertEquals(casted.at(2).name(), JSON_TEXT("")); #endif assertEquals(test.at(0), JSON_TEXT("world")); assertEquals(test.at(1), JSON_TEXT("mars")); assertEquals(test.at(2), JSON_TEXT("france")); assertEquals(test.at(0).name(), JSON_TEXT("hi")); assertEquals(test.at(1).name(), JSON_TEXT("hello")); assertEquals(test.at(2).name(), JSON_TEXT("salut")); } catch (std::out_of_range){ FAIL("exception caught"); } try { assertEquals(test.at(JSON_TEXT("hi")), JSON_TEXT("world")); assertEquals(test.at(JSON_TEXT("hello")), JSON_TEXT("mars")); assertEquals(test.at(JSON_TEXT("salut")), JSON_TEXT("france")); #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS assertEquals(test.at_nocase(JSON_TEXT("SALUT")), JSON_TEXT("france")); assertEquals(test.at_nocase(JSON_TEXT("HELLO")), JSON_TEXT("mars")); assertEquals(test.at_nocase(JSON_TEXT("HI")), JSON_TEXT("world")); #endif } catch (std::out_of_range){ FAIL("exception caught"); } assertException(test.at(JSON_TEXT("meh")), std::out_of_range); #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS assertException(test.at_nocase(JSON_TEXT("meh")), std::out_of_range); #endif assertEquals(test[JSON_TEXT("hi")], json_string(JSON_TEXT("world"))); assertEquals(test[JSON_TEXT("hello")], json_string(JSON_TEXT("mars"))); assertEquals(test[JSON_TEXT("salut")], json_string(JSON_TEXT("france"))); assertEquals(test[0], JSON_TEXT("world")); assertEquals(test[1], JSON_TEXT("mars")); assertEquals(test[2], JSON_TEXT("france")); #ifdef JSON_ITERATORS #ifdef JSON_CASTABLE UnitTest::SetPrefix("TestInspectors.cpp - Iterators"); for(JSONNode::iterator it = casted.begin(), end = casted.end(); it != end; ++it){ assertEquals((*it).name(), JSON_TEXT("")); } #endif #endif #ifdef JSON_BINARY UnitTest::SetPrefix("TestInspectors.cpp - Binary"); test.set_binary((const unsigned char *)"Hello World", 11); assertEquals(test.type(), JSON_STRING); assertEquals(test.as_string(), JSON_TEXT("SGVsbG8gV29ybGQ=")); assertEquals(test.as_binary(), "Hello World"); assertEquals(test.as_binary().size(), 11); test = JSON_TEXT("Hello World"); assertEquals(test.type(), JSON_STRING); assertEquals(test.as_string(), JSON_TEXT("Hello World")); #ifdef JSON_SAFE assertEquals(test.as_binary(), ""); #endif #endif #ifdef JSON_READ_PRIORITY //This is a regression test for a bug in at() json_string buffer(JSON_TEXT("{ \"myValue1\" : \"foo\", \"myValue2\" : \"bar\"}")); JSONNode current = libjson::parse(buffer); try { JSONNode & value1 = current[JSON_TEXT("myValue1")]; assertEquals(value1.as_string(), JSON_TEXT("foo")); JSONNode & value2 = current[JSON_TEXT("myValue2")]; assertEquals(value2.as_string(), JSON_TEXT("bar")); } catch (...){ assertTrue(false); } #endif #endif }
void TestSuite::TestFunctions(void){ UnitTest::SetPrefix("TestFunctions.cpp - Swap"); #ifdef JSON_LIBRARY JSONNODE * test1 = json_new(JSON_NODE); JSONNODE * test2 = json_new(JSON_NODE); json_set_i(test1, 14); json_set_i(test2, 35); json_swap(test1, test2); assertEquals_Primitive(json_as_int(test1), 35); assertEquals_Primitive(json_as_int(test2), 14); UnitTest::SetPrefix("TestFunctions.cpp - Duplicate"); json_delete(test1); test1 = json_duplicate(test2); #ifdef JSON_UNIT_TEST assertNotEquals(((JSONNode*)test1) -> internal, ((JSONNode*)test2) -> internal); #endif assertTrue(json_equal(test1, test2)); UnitTest::SetPrefix("TestFunctions.cpp - Duplicate with children"); JSONNODE * node = json_new(JSON_NODE); json_push_back(node, json_new_i(JSON_TEXT(""), 15)); json_push_back(node, json_new_f(JSON_TEXT(""), 27.4f)); json_push_back(node, json_new_b(JSON_TEXT(""), true)); TestSuite::testParsingItself(node); JSONNODE * dup = json_duplicate(node); assertEquals(json_size(dup), 3); #ifdef JSON_UNIT_TEST assertNotEquals(((JSONNode*)node) -> internal, ((JSONNode*)dup) -> internal); #endif assertEquals(json_type(dup), JSON_NODE); TestSuite::testParsingItself(node); TestSuite::testParsingItself(dup); assertEquals_Primitive(json_as_int(json_at(dup, 0)), 15); assertEquals_Primitive(json_as_float(json_at(dup, 1)), 27.4f); assertEquals(json_as_bool(json_at(dup, 2)), true); assertTrue(json_equal(json_at(dup, 0), json_at(node, 0))); assertTrue(json_equal(json_at(dup, 1), json_at(node, 1))); assertTrue(json_equal(json_at(dup, 2), json_at(node, 2))); TestSuite::testParsingItself(dup); #ifdef JSON_ITERATORS for(JSONNODE_ITERATOR it = json_begin(node), end = json_end(node), dup_it = json_begin(dup); it != end; ++it, ++dup_it){ assertTrue(json_equal(*it, *dup_it)); #ifdef JSON_UNIT_TEST assertNotEquals(((JSONNode*)(*it)) -> internal, ((JSONNode*)(*dup_it)) -> internal); #endif } #endif UnitTest::SetPrefix("TestFunctions.cpp - Nullify"); json_nullify(test1); assertEquals(json_type(test1), JSON_NULL); json_char * res = json_name(test1); assertCStringSame(res, JSON_TEXT("")); json_free(res); #ifdef JSON_CASTABLE UnitTest::SetPrefix("TestFunctions.cpp - Cast"); json_cast(test1, JSON_NULL); json_set_i(test2, 1); json_cast(test2, JSON_BOOL); assertEquals(json_type(test1), JSON_NULL); assertEquals(json_type(test2), JSON_BOOL); assertEquals(json_as_bool(test2), true); json_set_b(test2, true); assertEquals(json_as_bool(test2), true); json_cast(test2, JSON_NUMBER); assertEquals_Primitive(json_as_float(test2), 1.0f); json_set_f(test2, 0.0f); assertEquals_Primitive(json_as_float(test2), 0.0f); json_cast(test2, JSON_BOOL); assertEquals(json_as_bool(test2), false); #endif UnitTest::SetPrefix("TestFunctions.cpp - Merge"); json_set_a(test1, JSON_TEXT("hello")); json_set_a(test2, JSON_TEXT("hello")); #ifdef JSON_UNIT_TEST assertNotEquals(((JSONNode*)test1) -> internal, ((JSONNode*)test2) -> internal); #endif assertTrue(json_equal(test1, test2)); json_merge(test1, test2); #ifdef JSON_UNIT_TEST #ifdef JSON_REF_COUNT assertEquals(((JSONNode*)test1) -> internal, ((JSONNode*)test2) -> internal); #else assertNotEquals(((JSONNode*)test1) -> internal, ((JSONNode*)test2) -> internal); #endif #endif #ifdef JSON_CASTABLE json_cast(test1, JSON_NODE); json_cast(test2, JSON_NODE); assertEquals(json_type(test1), JSON_NODE); assertEquals(json_type(test2), JSON_NODE); json_push_back(test1, json_new_a(JSON_TEXT("hi"), JSON_TEXT("world"))); json_push_back(test2, json_new_a(JSON_TEXT("hi"), JSON_TEXT("world"))); TestSuite::testParsingItself(test1); TestSuite::testParsingItself(test2); json_merge(test1, test2); #ifdef JSON_UNIT_TEST #ifdef JSON_REF_COUNT assertEquals(((JSONNode*)test1) -> internal, ((JSONNode*)test2) -> internal); #else assertNotEquals(((JSONNode*)test1) -> internal, ((JSONNode*)test2) -> internal); #endif #endif TestSuite::testParsingItself(test1); TestSuite::testParsingItself(test2); #endif json_delete(test1); json_delete(test2); json_delete(node); json_delete(dup); #else JSONNode test1; JSONNode test2; test1 = JSON_TEXT("hello"); test2 = JSON_TEXT("world"); test1.swap(test2); assertEquals(test1, JSON_TEXT("world")); assertEquals(test2, JSON_TEXT("hello")); UnitTest::SetPrefix("TestFunctions.cpp - Duplicate"); test1 = test2.duplicate(); #ifdef JSON_UNIT_TEST assertNotEquals(test1.internal, test2.internal); #endif assertEquals(test1, test2); UnitTest::SetPrefix("TestFunctions.cpp - Duplicate with children"); JSONNode node = JSONNode(JSON_NODE); node.push_back(JSONNode(JSON_TEXT(""), 15)); node.push_back(JSONNode(JSON_TEXT(""), JSON_TEXT("hello world"))); node.push_back(JSONNode(JSON_TEXT(""), true)); TestSuite::testParsingItself(node); JSONNode dup = node.duplicate(); assertEquals(dup.size(), 3); #ifdef JSON_UNIT_TEST assertNotEquals(node.internal, dup.internal); #endif assertEquals(dup.type(), JSON_NODE); TestSuite::testParsingItself(node); TestSuite::testParsingItself(dup); try { assertEquals(dup.at(0), 15); assertEquals(dup.at(1), JSON_TEXT("hello world")); assertEquals(dup.at(2), true); assertEquals(dup.at(0), node.at(0)); assertEquals(dup.at(1), node.at(1)); assertEquals(dup.at(2), node.at(2)); } catch (std::out_of_range){ FAIL("exception caught"); } TestSuite::testParsingItself(dup); #ifdef JSON_ITERATORS for(JSONNode::iterator it = node.begin(), end = node.end(), dup_it = dup.begin(); it != end; ++it, ++dup_it){ assertEquals(*it, *dup_it); #ifdef JSON_UNIT_TEST assertNotEquals((*it).internal, (*dup_it).internal); #endif } #endif UnitTest::SetPrefix("TestFunctions.cpp - Nullify"); test1.nullify(); assertEquals(test1.type(), JSON_NULL); assertEquals(test1.name(), JSON_TEXT("")); #ifdef JSON_CASTABLE UnitTest::SetPrefix("TestFunctions.cpp - Cast"); test1.cast(JSON_NULL); test2 = 1; test2.cast(JSON_BOOL); assertEquals(test1.type(), JSON_NULL); assertEquals(test2.type(), JSON_BOOL); assertEquals(test2, true); test2 = true; assertEquals(test2, true); test2.cast(JSON_NUMBER); assertEquals(test2, 1.0f); test2 = 0.0f; assertEquals(test2, 0.0f); test2.cast(JSON_BOOL); assertEquals(test2, false); #endif UnitTest::SetPrefix("TestFunctions.cpp - Merge"); test1 = JSON_TEXT("hello"); test2 = JSON_TEXT("hello"); #ifdef JSON_UNIT_TEST assertNotEquals(test1.internal, test2.internal); #endif assertEquals(test1, test2); test1.merge(test2); #ifdef JSON_UNIT_TEST #ifdef JSON_REF_COUNT assertEquals(test1.internal, test2.internal); #else assertNotEquals(test1.internal, test2.internal); #endif #endif #ifdef JSON_CASTABLE test1.cast(JSON_NODE); test2.cast(JSON_NODE); #else test1 = JSONNode(JSON_NODE); test2 = JSONNode(JSON_NODE); #endif assertEquals(test1.type(), JSON_NODE); assertEquals(test2.type(), JSON_NODE); test1.push_back(JSONNode(JSON_TEXT("hi"), JSON_TEXT("world"))); test2.push_back(JSONNode(JSON_TEXT("hi"), JSON_TEXT("world"))); TestSuite::testParsingItself(test1); TestSuite::testParsingItself(test2); test1.merge(test2); #ifdef JSON_UNIT_TEST #ifdef JSON_REF_COUNT assertEquals(test1.internal, test2.internal); #else assertNotEquals(test1.internal, test2.internal); #endif #endif TestSuite::testParsingItself(test1); TestSuite::testParsingItself(test2); #endif }