示例#1
0
//native json_object_update(Handle:hObj, Handle:hOther);
static cell_t Native_json_object_update(IPluginContext *pContext, const cell_t *params) {
	HandleError err;
	HandleSecurity sec;
	sec.pOwner = NULL;
	sec.pIdentity = myself->GetIdentity();

	// Param 1
	json_t *object;
	Handle_t hndlObject = static_cast<Handle_t>(params[1]);
	if ((err=g_pHandleSys->ReadHandle(hndlObject, htJanssonObject, &sec, (void **)&object)) != HandleError_None)
    {
        return pContext->ThrowNativeError("Invalid <Object> handle %x (error %d)", hndlObject, err);
    }

	// Param 2
	json_t *other;
	Handle_t hndlOther = static_cast<Handle_t>(params[2]);
	if ((err=g_pHandleSys->ReadHandle(hndlOther, htJanssonObject, &sec, (void **)&other)) != HandleError_None)
    {
        return pContext->ThrowNativeError("Invalid <Object> handle %x (error %d)", hndlOther, err);
    }

	bool bSuccess = (json_object_update(object, other) == 0);
	return bSuccess;
}
示例#2
0
static char *make_json_response_payload (const char *request_payload,
                                         const char *route,
                                         uint32_t userid, uint32_t rolemask)
{
    json_t *o = NULL;
    json_t *add = NULL;
    char *result = NULL;

    if (!request_payload || !(o = json_loads (request_payload, 0, NULL))) {
        errno = EPROTO;
        goto done;
    }
    if (!(add = json_pack ("{s:s s:i s:i}", "route", route,
                                            "userid", userid,
                                            "rolemask", rolemask))) {
        errno = ENOMEM;
        goto done;
    }
    if (json_object_update (o, add) < 0) {
        errno = ENOMEM;
        goto done;
    }
    if (!(result = json_dumps (o, 0))) {
        errno = ENOMEM;
        goto done;
    }
done:
    json_decref (o);
    json_decref (add);
    return result;
}
示例#3
0
json_t *btAction_toJson(const void *vba)
{
	btAction_t	*ba = (btAction_t *)vba;
	json_t		*root;

	if (ba == NULL)
		return NULL;

	root = json_object();

	json_object_update(root, btFunction_toJson(ba->function));
	json_object_update(root, btEffect_toJson(ba->effect));
	json_object_set_new(root, "parameters",
				paramList_toJson(ba->pl));

	return root;
}
示例#4
0
void raceList_toJson(cnvList_t *rl, btstring_t *fname)
{
	json_t		*root;

	root = cnvList_toJsonObject(rl);
	cnvList_setToJson(rl, btRace_toJsonArray);
	json_object_update(root,cnvList_toJsonArray(rl));

	JSON_DUMP(root, fname);
}
示例#5
0
文件: ecdhes.c 项目: simo5/jose
static bool
alg_wrap_unw(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwe,
             const json_t *rcp, const json_t *jwk, json_t *cek)
{
    const json_t *epk = NULL;
    json_auto_t *exc = NULL;
    json_auto_t *der = NULL;
    json_auto_t *hdr = NULL;
    const char *wrap = NULL;

    hdr = jose_jwe_hdr(jwe, rcp);
    epk = json_object_get(hdr, "epk");
    if (!hdr || !epk)
        return false;

    /* If the JWK has a private key, perform the normal exchange. */
    if (json_object_get(jwk, "d")) {
        const jose_hook_alg_t *ecdh = NULL;

        ecdh = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_EXCH, "ECDH");
        if (!ecdh)
            return false;

        exc = ecdh->exch.exc(ecdh, cfg, jwk, epk);

    /* Otherwise, allow external exchanges. */
    } else if (json_equal(json_object_get(jwk, "crv"),
                          json_object_get(epk, "crv"))) {
        exc = json_deep_copy(jwk);
    }
    if (!exc)
        return false;

    der = derive(alg, cfg, hdr, cek, exc);
    if (!der)
        return false;

    wrap = strchr(alg->name, '+');
    if (wrap) {
        const jose_hook_alg_t *kw = NULL;

        kw = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_WRAP, &wrap[1]);
        if (!kw)
            return false;

        return kw->wrap.unw(kw, cfg, jwe, rcp, der, cek);
    }

    return json_object_update(cek, der) == 0;
}
示例#6
0
文件: exc.c 项目: latchset/jose
static int
jcmd_jwk_exc(int argc, char *argv[])
{
    jcmd_opt_auto_t opt = {};
    json_auto_t *key = NULL;
    json_t *tmpl = NULL;

    if (!jcmd_opt_parse(argc, argv, cfgs, &opt, prefix))
        return EXIT_FAILURE;

    if (json_array_size(opt.keys) > 1 && json_array_remove(opt.keys, 0) < 0)
        return EXIT_FAILURE;

    if (json_array_size(opt.lcl) != 1) {
        fprintf(stderr, "Local JWK must be specified exactly once!\n");
        return EXIT_FAILURE;
    }

    if (json_array_size(opt.rem) != 1) {
        fprintf(stderr, "Remote JWK must be specified exactly once!\n");
        return EXIT_FAILURE;
    }

    key = jose_jwk_exc(NULL, json_array_get(opt.lcl, 0),
                       json_array_get(opt.rem, 0));
    if (!key) {
        fprintf(stderr, "Error performing exchange!\n");
        return EXIT_FAILURE;
    }

    tmpl = json_array_get(opt.keys, json_array_size(opt.keys) - 1);

    if (json_object_update(tmpl, key) < 0)
        return EXIT_FAILURE;

    if (json_dumpf(tmpl, opt.output, JSON_COMPACT | JSON_SORT_KEYS) < 0) {
        fprintf(stderr, "Error writing JWK!\n");
        return EXIT_FAILURE;
    }

    if (isatty(fileno(opt.output)))
        fprintf(opt.output, "\n");

    return EXIT_SUCCESS;
}
示例#7
0
static json_t *monster_toJson(const void *vm)
{
	json_t		*monRoot;
	monster_t	*m = (monster_t *)vm;
	uint32_t	i;

	monRoot = json_object();

	JSON_BTSTRING(monRoot, "singular",	m->singular);
	JSON_BTSTRING(monRoot, "plural",	m->plural);
	JSON_BTSTRING(monRoot, "picture",	m->picture);
	JSON_STRING(monRoot, "pronoun",		getPronounString(m->pronoun));
	JSON_NUMBER(monRoot, "hpBase",		m->hpBase);
	JSON_NUMBER(monRoot, "hpRndDie",	m->hpRndDie);
	JSON_NUMBER(monRoot, "hpRndNDice",	m->hpRndNdice);
	JSON_NUMBER(monRoot, "reward",		m->reward);
	JSON_NUMBER(monRoot, "ac",		m->baseAC);
	JSON_NUMBER(monRoot, "breathSaveLo",	m->breathSaveLo);
	JSON_NUMBER(monRoot, "breathSaveHi",	m->breathSaveHi);
	JSON_NUMBER(monRoot, "spellSaveLo",	m->spellSaveLo);
	JSON_NUMBER(monRoot, "spellSaveHi",	m->spellSaveHi);
	JSON_NUMBER(monRoot, "toHitLo",		m->toHitLo);
	JSON_NUMBER(monRoot, "toHitHi",		m->toHitHi);
	JSON_NUMBER(monRoot, "priorityLo",	m->priorityLo);
	JSON_NUMBER(monRoot, "priorityHi",	m->priorityHi);
	JSON_BOOL(monRoot, "rndGroupSize",	m->rndGroupSize);
	JSON_NUMBER(monRoot, "groupSize",	m->groupSize);
	JSON_NUMBER(monRoot, "numAttacks",	m->numAttacks);
	/* XXX PRONOUN Handling */
	JSON_BOOL(monRoot, "willAdvance",	m->willAdvance);
	JSON_NUMBER(monRoot, "distance",	m->distance);
	JSON_NUMBER(monRoot, "advanceSpeed",	m->advSpeed);
	JSON_BOOL(monRoot, "isIllusion",	m->isIllusion);

	json_object_update(monRoot, repel_toJson(&m->repel));
	json_object_set_new(monRoot, "attacks", cnvList_toJsonArray(m->attacks));
	
	return monRoot;
}
示例#8
0
static json_t *btcity_toJson(const void *vc)
{
	btcity_t	*btc = (btcity_t *)vc;
	json_t		*root;
	json_t		*node;

	root = json_object();

	JSON_BTSTRING(root, "title", btc->title);
	JSON_BTSTRING(root, "guildExitSquare", btc->guildExitSquare);
	JSON_BTSTRING(root, "guildExitDir", btc->guildExitDir);
	json_object_set_new(root, "squares", cnvList_toJsonObject(btc->sqs));
	json_object_set_new(root, "buildings", cnvList_toJsonObject(btc->bls));

	node = json_object();
	json_object_set_new(node, "items",
				cnvList_toJsonArray(btc->day->items));
	json_object_set_new(node, "monsters",
				cnvList_toJsonArray(btc->day->monsters));
	JSON_NUMBER(node, "poisonDamage", btc->day->poisonDmg);
	JSON_NUMBER(node, "level", btc->day->level);
	if (btc->params)
		json_object_update(node, paramList_toJson(btc->params));

	json_object_set_new(root, "day", node);

	if (btc->night->level) {
		node = json_object();
		json_object_set_new(node, "items",
				cnvList_toJsonArray(btc->night->items));
		json_object_set_new(node, "monsters",
				cnvList_toJsonArray(btc->night->monsters));
		JSON_NUMBER(node, "poisonDamage", btc->night->poisonDmg);
		JSON_NUMBER(node, "level", btc->night->level);
		json_object_set_new(root, "night", node);
	}

	return root;
}
示例#9
0
static void test_update()
{
    json_t *object, *other, *nine, *ten;

    object = json_object();
    other = json_object();

    nine = json_integer(9);
    ten = json_integer(10);

    if(!object || !other)
        fail("unable to create object");
    if(!nine || !ten)
        fail("unable to create integer");


    /* update an empty object with an empty object */

    if(json_object_update(object, other))
        fail("unable to update an emtpy object with an empty object");

    if(json_object_size(object) != 0)
        fail("invalid size after update");

    if(json_object_size(other) != 0)
        fail("invalid size for updater after update");


    /* update an empty object with a nonempty object */

    if(json_object_set(other, "a", ten) ||
       json_object_set(other, "b", ten) ||
       json_object_set(other, "c", ten) ||
       json_object_set(other, "d", ten) ||
       json_object_set(other, "e", ten))
        fail("unable to set value");

    if(json_object_update(object, other))
        fail("unable to update an empty object");

    if(json_object_size(object) != 5)
        fail("invalid size after update");

    if(json_object_get(object, "a") != ten ||
       json_object_get(object, "b") != ten ||
       json_object_get(object, "c") != ten ||
       json_object_get(object, "d") != ten ||
       json_object_get(object, "e") != ten)
        fail("update works incorrectly");


    /* perform the same update again */

    if(json_object_update(object, other))
        fail("unable to update a non-empty object");

    if(json_object_size(object) != 5)
        fail("invalid size after update");

    if(json_object_get(object, "a") != ten ||
       json_object_get(object, "b") != ten ||
       json_object_get(object, "c") != ten ||
       json_object_get(object, "d") != ten ||
       json_object_get(object, "e") != ten)
        fail("update works incorrectly");


    /* update a nonempty object with a nonempty object with both old
       and new keys */

    if(json_object_clear(other))
        fail("clear failed");

    if(json_object_set(other, "a", nine) ||
       json_object_set(other, "b", nine) ||
       json_object_set(other, "f", nine) ||
       json_object_set(other, "g", nine) ||
       json_object_set(other, "h", nine))
        fail("unable to set value");

    if(json_object_update(object, other))
        fail("unable to update a nonempty object");

    if(json_object_size(object) != 8)
        fail("invalid size after update");

    if(json_object_get(object, "a") != nine ||
       json_object_get(object, "b") != nine ||
       json_object_get(object, "f") != nine ||
       json_object_get(object, "g") != nine ||
       json_object_get(object, "h") != nine)
        fail("update works incorrectly");

    json_decref(nine);
    json_decref(ten);
    json_decref(other);
    json_decref(object);
}
示例#10
0
int handle_response(json_t* response)
{
	int retval = 0;
	jsonrpc_request_t* req = NULL;
	json_t* return_obj = NULL;
	json_t* internal = NULL;
	char* freeme = NULL;


	/* check if json object */
	if(!json_is_object(response)){
		WARN("jsonrpc response is not an object\n");
		return -1;
	}

	/* check version */
	json_t* version = json_object_get(response, "jsonrpc");
	if(!version) {
		WARN("jsonrpc response does not have a version.\n");
		retval = -1;
		goto end;
	}

	const char* version_s = json_string_value(version);
	if(!version_s){
		WARN("jsonrpc response version is not a string.\n");
		retval = -1;
		goto end;
	}

	if (strlen(version_s) != (sizeof(JSONRPC_VERSION)-1)
			|| strncmp(version_s, JSONRPC_VERSION, sizeof(JSONRPC_VERSION)-1) != 0) {
		WARN("jsonrpc response version is not %s. version: %s\n",
				JSONRPC_VERSION, version_s);
		retval = -1;
		goto end;
	}

	/* check for an id */
	json_t* _id = json_object_get(response, "id");
	if(!_id) {
		WARN("jsonrpc response does not have an id.\n");
		retval = -1;
		goto end;
	}

	int id = json_integer_value(_id);
	if (!(req = pop_request(id))) {
		/* don't fail the server for an unrecognized id */
		retval = 0;
		goto end;
	}

	return_obj = json_object();

	json_t* error = json_object_get(response, "error");
	// if the error value is null, we don't care
	bool _error = error && (json_typeof(error) != JSON_NULL);

	json_t* result = json_object_get(response, "result");

	if(_error) {
		json_object_set(return_obj, "error", error);
	}

	if(result) {
		json_object_set(return_obj, "result", result);
	}

	if ((!result && !_error) || (result && _error)) {
		WARN("bad response\n");
		internal = internal_error(JRPC_ERR_BAD_RESP, req->payload);
		json_object_update(return_obj, internal);
		if(internal) json_decref(internal);
	}

	pv_value_t val;

	if(jsontoval(&val, &freeme, return_obj)<0) {
		fail_request(
				JRPC_ERR_TO_VAL,
				req,
				"Failed to convert response json to pv\n");
		retval = -1;
		goto end;
	}

	char* error_s = NULL;

	if(send_to_script(&val, req->cmd)>=0) {
		goto free_and_end;
	}

	if(_error) {
		// get code from error
		json_t* _code = json_object_get(error, "code");
		if(_code) {
			int code = json_integer_value(_code);

			// check if code is in global_retry_ranges
			retry_range_t* tmpr;
			for(tmpr = global_retry_ranges;
					tmpr != NULL;
					tmpr = tmpr->next) {
				if((tmpr->start < tmpr->end
						&& tmpr->start <= code && code <= tmpr->end)
				|| (tmpr->end < tmpr->start
						&& tmpr->end <= code && code <= tmpr->start)
				|| (tmpr->start == tmpr->end && tmpr->start == code)) {
					if(schedule_retry(req)==0) {
						goto end;
					}
					break;
				}
			}

		}
		error_s = json_dumps(error, JSON_COMPACT);
		if(error_s) {
			WARN("Request received an error: \n%s\n", error_s);
			free(error_s);
		} else {
			fail_request(
					JRPC_ERR_BAD_RESP,
					req,
					"Could not convert 'error' response to string");
			retval = -1;
			goto end;
		}
	}


free_and_end:
	free_req_cmd(req->cmd);
	free_request(req);

end:
	if(freeme) free(freeme);
	if(return_obj) json_decref(return_obj);
	return retval;
}
示例#11
0
文件: ecdhes.c 项目: simo5/jose
static bool
alg_wrap_wrp(const jose_hook_alg_t *alg, jose_cfg_t *cfg, json_t *jwe,
             json_t *rcp, const json_t *jwk, json_t *cek)
{
    const jose_hook_alg_t *ecdh = NULL;
    json_auto_t *exc = NULL;
    json_auto_t *hdr = NULL;
    json_auto_t *epk = NULL;
    json_auto_t *der = NULL;
    const char *wrap = NULL;
    json_t *h = NULL;

    if (json_object_get(cek, "k")) {
        if (strcmp(alg->name, "ECDH-ES") == 0)
            return false;
    } else if (!jose_jwk_gen(cfg, cek)) {
        return false;
    }

    hdr = jose_jwe_hdr(jwe, rcp);
    if (!hdr)
        return false;

    h = json_object_get(rcp, "header");
    if (!h && json_object_set_new(rcp, "header", h = json_object()) == -1)
        return false;

    epk = json_pack("{s:s,s:O}", "kty", "EC", "crv",
                    json_object_get(jwk, "crv"));
    if (!epk)
        return false;

    if (!jose_jwk_gen(cfg, epk))
        return false;

    ecdh = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_EXCH, "ECDH");
    if (!ecdh)
        return false;

    exc = ecdh->exch.exc(ecdh, cfg, epk, jwk);
    if (!exc)
        return false;

    if (!jose_jwk_pub(cfg, epk))
        return false;

    if (json_object_set(h, "epk", epk) == -1)
        return false;

    der = derive(alg, cfg, hdr, cek, exc);
    if (!der)
        return false;

    wrap = strchr(alg->name, '+');
    if (wrap) {
        const jose_hook_alg_t *kw = NULL;

        kw = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_WRAP, &wrap[1]);
        if (!kw)
            return false;

        return kw->wrap.wrp(kw, cfg, jwe, rcp, der, cek);
    }

    if (json_object_update(cek, der) < 0)
        return false;

    return add_entity(jwe, rcp, "recipients", "header", "encrypted_key", NULL);
}
示例#12
0
bool TrafficNetwork::saveToFile(const string out_prefix, bool newWeek){
	bool everythingOK = true;
	int metric = KPH; //save in KPH such that it can be used by external tool (and be human readable)
	std::vector<long> nIds;
	long crtId;
	char crtType;
	string out_fileName = (out_prefix+"_roads_stats.json");

	json_t *root = NULL;

	if(firstSaved){
		if(fileName != ""){
			json_error_t error;

			root = json_load_file(fileName.c_str(), 0, &error);

			if(!root){
				std::cout<<std::endl<<"WARNING: while opening "<<fileName<<" at line "<<error.line<<" - "<<error.text<<std::endl;
				root= NULL;
			}

			if(!json_is_object(root)){
				std::cout<<std::endl<<"WARNING: input file "<<fileName<<" has not the correct structure - expected root to be an object"<<std::endl;
				json_decref(root);
				root = NULL;
			}

			if(!root){
				std::cout<<"File "<<fileName<<" could not be using during saving process"<<std::endl<<"\t --> reverting to saving network based on stored data (possibility for loss of positional infos)"<<std::endl;
			}
		}

		if(!root){
			root = json_object();
			if ( root ) {
				json_object_set_new(root, "metric" ,json_integer(metric));
				json_t *_nodes = json_array();
				json_t *_roads = json_array();
				for(NodeVec::iterator it= nodes.begin() ; it!=nodes.end(); ++it){
					json_t * _node = json_object();
					crtId = (*it)->getId();
					crtType = (*it)->getType();
					json_object_set_new(_node,"id",json_integer(crtId));
					json_object_set_new(_node,"type",json_integer(crtType));
					json_array_append_new(_nodes,_node);
					nIds = (*it)->getNeighborsId();
					for(std::vector<long>::iterator jt = nIds.begin(); jt != nIds.end(); ++jt){
						Road r = *((*it)->roadTo(*jt));
						json_t * _road = json_object();
						json_object_set_new(_road,"name",json_string(r.getName().c_str()));
						json_object_set_new(_road,"startId",json_integer(crtId));
						json_object_set_new(_road,"endId",json_integer(r.getEndPoint()->getId()));
						json_object_set_new(_road,"speedLimit",json_integer(r.getSpeedLimit()*3.6)); //x3.6 to go from MPS to KPH
						json_object_set_new(_road,"length",json_real(r.getLength()/1000)); // /1000 to go from M to K
						json_object_set_new(_road,"nbBands",json_integer(r.getNbBands()));

						json_array_append_new(_roads,_road);
					}
				}
				json_object_set_new(root, "nodes" ,_nodes);
				json_object_set_new(root, "roads" ,_roads);
			}else{
				std::cout<<"ERROR: Could not create 'root' during saving process"<<std::endl;
				return false;
			}

		}
	}else{
		json_error_t error;

		root = json_load_file(out_fileName.c_str(), 0, &error);

		if(!root){
			std::cout<<std::endl<<"ERROR: while opening "<<out_fileName<<" at line "<<error.line<<" - "<<error.text<<std::endl;
			root= NULL;
			return false;
		}

		if(!json_is_object(root)){
			std::cout<<std::endl<<"ERROR: input file "<<fileName<<" has not the correct structure - expected root to be an object"<<std::endl;
			json_decref(root);
			root = NULL;
			return false;
		}
	}

	json_t *roadsInfos;
	if(monitered){
		bool first = false;
		if(firstSaved){
			roadsInfos = json_array();
			int nbRoads = json_array_size(json_object_get(root,"roads"));
			for(int i = 0; i < nbRoads; i++){
				json_array_append_new(roadsInfos,json_object());
			}
			json_object_set(root,"roadsInfos",roadsInfos);
			json_object_set_new(root,"timePrecision",json_integer(TIME_PRECISION));
			json_object_set_new(root,"time_index",json_integer(0));
			json_object_set_new(root,"driversCount_index",json_integer(1));
			firstSaved = false;
			first = true;
		}else
			roadsInfos = json_object_get(root,"roadsInfos");
		json_t *infos;
		for(NodeVec::iterator it= nodes.begin() ; it!=nodes.end(); ++it){
			nIds = (*it)->getNeighborsId();
			for(std::vector<long>::iterator jt = nIds.begin(); jt != nIds.end(); ++jt){
				Road* r = ((*it)->roadTo(*jt));
				infos = r->getMonitor()->getInfos();
				if(first){
					json_object_update(json_array_get(roadsInfos,r->getId()),infos);
				}else{
					json_array_extend(json_object_get(json_array_get(roadsInfos,r->getId()),"data"),json_object_get(infos,"data"));
				}
				r->getMonitor()->resetInfos(newWeek);
				json_object_clear(infos);
				json_decref(infos);
			}
		}
	}

	//actually save
	if(!(json_dump_file(root,out_fileName.c_str(),JSON_COMPACT) == 0)){
	//if(!(json_dump_file(root,out_fileName.c_str(),JSON_INDENT(2)) == 0)){ //<== to have pretty JSON file
		everythingOK = false;
		std::cout<< "Could not open file : "<<out_fileName << " to write down network "<< name <<std::endl;
	}
	if(monitered){
		json_array_clear(roadsInfos);
	}
	json_object_clear(root);
	json_decref(root);
	if(newWeek){
		firstSaved = true;
	}
	return everythingOK;
}
示例#13
0
int ast_json_object_update(struct ast_json *object, struct ast_json *other)
{
	return json_object_update((json_t *)object, (json_t *)other);
}
示例#14
0
int json_path_set(json_t *json, const char *path, json_t *value,
		unsigned int append)
{
	static const char array_open = '[';
	static const char object_delim = '.';
	static const char *path_delims = ".[";
	static const char *array_close = "]";

	json_t *cursor, *parent = NULL;
	char *token, *buf = NULL, *peek, delim = '\0';
	const char *expect;
	int index_saved = -1;

	if (!json || !path || !value) {
		ERR("invalid arguments\n");
		goto fail;
	} else {
		buf = jsonp_strdup(path);
	}

	peek = buf;
	token = buf;
	cursor = json;
	expect = path_delims;

	if (*token == array_open) {
		expect = array_close;
		token++;
	}

	while (peek && *peek && cursor)
	{
		char *last_peek = peek;
		peek = strpbrk(last_peek, expect);

		if (peek) {
			if (!token && peek != last_peek) {
				ERR("unexpected trailing chars in JSON path at pos %zu\n",
						last_peek - buf);
				goto fail;
			}
			delim = *peek;
			*peek++ = '\0';
		} else { // end of path
			if (expect == path_delims) {
				break;
			} else {
				ERR("missing ']' at pos %zu\n", peek - buf);
				goto fail;
			}
		}

		if (expect == path_delims) {
			if (token) {
				if (token[0] == '\0') {
					ERR("empty token at pos %zu\n", peek - buf);
					goto fail;
				}

				parent = cursor;
				cursor = json_object_get(parent, token);

				if (!cursor) {
					if (!json_is_object(parent)) {
						ERR("object expected at pos %zu\n", peek - buf);
						goto fail;
					}
					if (delim == object_delim) {
						cursor = json_object();
						json_object_set_new(parent, token, cursor);
					} else {
						ERR("new array is not allowed at pos %zu\n", peek - buf);
						goto fail;
					}
				}
			}
			expect = (delim == array_open ? array_close : path_delims);
			token = peek;
		} else if (expect == array_close) {
			char *endptr;
			size_t index;

			parent = cursor;
			if (!json_is_array(parent)) {
				ERR("array expected at pos %zu\n", peek - buf);
				goto fail;
			}

			index = strtol(token, &endptr, 0);
			if (*endptr) {
				ERR("invalid array index at pos %zu\n", peek - buf);
				goto fail;
			}

			cursor = json_array_get(parent, index);
			if (!cursor) {
				ERR("array index out of bound at pos %zu\n", peek - buf);
				goto fail;
			}

			index_saved = index;
			token = NULL;
			expect = path_delims;

		} else {
			ERR("fatal JSON error at pos %zu\n", peek - buf);
			goto fail;
		}
	}

	if (token && append) {

		if(strlen(token) > 0) {
			json_t* tmp  = json_object_get(cursor, token);
			if(json_is_array(tmp)) {
				json_array_append(tmp, value);
				json_object_set(cursor, token, tmp);
			} else if(json_is_object(tmp) && json_is_object(value) ) {
				json_object_update(tmp, value);
				json_object_set(cursor, token, tmp);
			} else {
				ERR("JSON array or object expected at pos %zu\n", peek - buf);
				goto fail;
			}
		} else if(json_is_array(cursor)) {
			json_array_append(cursor, value);

		} else if(json_is_object(cursor) && json_is_object(value)) {
			json_object_update(cursor, value);

		} else {
			ERR("JSON array or object expected at pos %zu\n", peek - buf);
			goto fail;
		}

	} else if (token && strlen(token) != 0 ) {

		if (json_is_object(cursor)) {
			json_object_set(cursor, token, value);

		} else {
			ERR("JSON object expected at pos %zu\n", peek - buf);
			goto fail;
		}

		cursor = json_object_get(cursor, token);
	} else if (index_saved != -1 && json_is_array(parent)) {
		json_array_set(parent, index_saved, value);
		cursor = json_array_get(parent, index_saved);

	} else {
		ERR("invalid JSON path at pos %zu\n", peek - buf);
		goto fail;
	}

	json_decref(value);
	jsonp_free(buf);
	return 0;

fail:
	json_decref(value);
	jsonp_free(buf);
	return -1;
}
示例#15
0
文件: codec-json.c 项目: Memeo/lounge
int la_codec_object_update(la_codec_value_t *object, la_codec_value_t *other)
{
    return json_object_update((json_t *) object, (json_t *) other);
}