/* { "func":"timeConvert", "args":60 } { "func":"timeConvert", "args":[1,3,4,5] } */ static cxConstChars jsonStrConvert(json_t *v) { cxJson json = cxJsonReference(v); cxStr txt = cxJsonDump(json); cxConstChars funcName = cxJsonConstChars(json, "func"); if(!cxConstCharsOK(funcName)){ return cxStrBody(txt); } cxConvert convert = cxGetConvert(funcName); if(convert == NULL){ return cxStrBody(txt); } CX_ASSERT(convert->func != NULL, "convert error"); cxStr ret = NULL; cxJson args = cxJsonAny(json, "args"); if(args == NULL){ ret = ((cxStr (*)(void))convert->func)(); }else if(cxJsonIsStr(args)){ cxConstChars a1 = cxJsonToConstChars(args); ret = ((cxStr (*)(cxConstChars))convert->func)(a1); }else if(cxJsonIsInt(args)){ cxInt a1 = cxJsonToInt(args, 0); ret = ((cxStr (*)(cxInt))convert->func)(a1); }else if(cxJsonIsDouble(args)){ cxDouble a1 = cxJsonToDouble(args, 0); ret = ((cxStr (*)(cxDouble))convert->func)(a1); }else if(cxJsonIsBool(args)){ cxBool a1 = cxJsonToBool(args, false); ret = ((cxStr (*)(cxBool))convert->func)(a1); }else{ ret = ((cxStr (*)(cxJson))convert->func)(args); } return ret == NULL ? NULL : cxStrBody(ret); }
//support key.key key.0.0.1, max 8 level static json_t *jsonGetJson(json_t *json,cxConstChars key) { CX_ASSERT(cxConstCharsOK(key), "key error"); json_t *rv = json_object_get(json, key); if(rv != NULL){ return rv; } cxChars ckey = allocator->strdup(key); cxInt num = 0; cxChars ckeys[16]; cxChars src = ckey; ckeys[num++] = src; while (*src++ != '\0') { if(*src != '.')continue; CX_ASSERT(num < 16, ". opt too more"); ckeys[num++] = src + 1; *src++ = '\0'; } json_t *pv = json; cxInt index = 0; for(cxInt i=0; i < num;i ++){ cxConstChars ckey = ckeys[i]; if(cxKeyIsNumber(ckey, &index)){ rv = json_array_get(pv, index); }else{ rv = json_object_get(pv, ckey); } if(rv == NULL)break; pv = rv; } allocator->free(ckey); return rv; }
cxBool cxEngineLuaRunChars(cxConstChars code) { CX_ASSERT(cxConstCharsOK(code), "code args error"); if(luaL_dostring(gL, code) != 0){ CX_ERROR("cxLuaLoader run code error:%s",lua_tostring(gL, -1)); return false; } return true; }
//if field is number cxBool cxKeyIsNumber(cxConstChars ckey,cxInt *num) { if(!cxConstCharsOK(ckey)){ return false; } cxInt len = (cxInt)strlen(ckey); for(cxInt i=0; i < len;i++){ if(!isdigit(ckey[i])){ return false; } } if(num != NULL){ *num = atoi(ckey); } return true; }
void cxSetConvert(cxConstChars name,cxAny func) { CX_ASSERT(convertFuncs != NULL, "global cxjson not init"); CX_ASSERT(cxConstCharsOK(name) && func != NULL, "name or func error"); cxHashSet(convertFuncs, cxHashStrKey(name), cxConvertCreate(func)); }
static cxStr jsonToStr(json_t *v) { CX_RETURN(v == NULL, NULL); cxConstChars str = jsonToConstChars(v); return cxConstCharsOK(str)?cxStrConstChars(str) : NULL; }
cxConvert cxGetConvert(cxConstChars name) { CX_ASSERT(convertFuncs != NULL, "global cxjson not init"); CX_ASSERT(cxConstCharsOK(name), "name error"); return cxHashGet(convertFuncs, cxHashStrKey(name)); }
static json_t *cxJsonGetJson(cxJson json,cxConstChars key) { CX_ASSERT_THIS(json, cxJson); CX_ASSERT(cxConstCharsOK(key), "key error"); return jsonGetJson(CX_JSON_PTR(this), key); }