str store_serialize(map_t _store) { cJSON *flat_map; str ret = STR_NULL; if (map_size(_store) == 0) return ret; cJSON_InitHooks(&shm_hooks); flat_map = cJSON_CreateObject(); if (!flat_map) { LM_ERR("oom\n"); return ret; } if (map_for_each(_store, push_kv_to_json, flat_map) != 0) LM_ERR("oom - serialized map is incomplete!\n"); ret.s = cJSON_PrintUnformatted(flat_map); if (!ret.s) { LM_ERR("oom\n"); goto out; } ret.len = strlen(ret.s); out: cJSON_Delete(flat_map); cJSON_InitHooks(NULL); return ret; }
void store_free_buffer(str *serialized) { if (serialized->s) { cJSON_InitHooks(&shm_hooks); cJSON_PurgeString(serialized->s); cJSON_InitHooks(NULL); } }
static void cjson_add_null_should_fail_on_allocation_failure(void) { cJSON *root = cJSON_CreateObject(); cJSON_InitHooks(&failing_hooks); TEST_ASSERT_NULL(cJSON_AddNullToObject(root, "null")); cJSON_InitHooks(NULL); cJSON_Delete(root); }
map_t store_deserialize(const str *input) { map_t map; cJSON *json_map, *obj; str key; int_str_t value; map = map_create(AVLMAP_SHARED); if (!map) { LM_ERR("oom\n"); return NULL; } cJSON_InitHooks(&shm_hooks); json_map = cJSON_Parse(input->s); if (!json_map) { LM_ERR("bad JSON input or oom\n"); goto out; } if (json_map->type != cJSON_Object) { LM_BUG("non-cJSON_Object kv_store col type (%d)", json_map->type); goto out; } for (obj = json_map->child; obj; obj = obj->next) { init_str(&key, obj->string); switch (obj->type) { case cJSON_String: value.is_str = 1; init_str(&value.s, obj->valuestring); break; case cJSON_Number: value.is_str = 0; value.i = obj->valueint; break; default: LM_BUG("unknown obj type (%d)", obj->type); continue; } if (!kv_put(map, &key, &value)) LM_ERR("oom, map will be incomplete\n"); } out: cJSON_Delete(json_map); cJSON_InitHooks(NULL); return map; }
/**@brief Initialize cJSON by assigning function hooks. */ void cJSON_Init(void) { m_iot_hooks.malloc_fn = malloc_fn_hook; m_iot_hooks.free_fn = free_fn_hook; cJSON_InitHooks(&m_iot_hooks); }
static int __get_json( const char *obj_name, int32_t timeout, cJSON **tree ) { const char const *url_fmt = "http://localhost:62000/config/%s"; size_t len; char *url; int rv; rv = -1; /* Do the heavy lifting in here. */ len = snprintf( NULL, 0, url_fmt, obj_name ); url = (char*) malloc( sizeof(char) * (len + 1) ); if( NULL != url ) { CURL *curl; sprintf( url, url_fmt, obj_name ); /* If the library has not been initialized, do it now for cURL. */ ccsplite_init(); if( 1 == __ccsplite_init ) { curl = curl_easy_init(); if( curl ) { CURLcode res; struct payload payload; payload.buf = NULL; payload.len = 0; /* Do the right thing here. */ curl_easy_setopt( curl, CURLOPT_URL, url ); curl_easy_setopt( curl, CURLOPT_TIMEOUT, (long) timeout ); curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, __write_fn ); curl_easy_setopt( curl, CURLOPT_WRITEDATA, &payload ); res = curl_easy_perform( curl ); if( CURLE_OK == res ) { cJSON *tmp; cJSON_Hooks hooks = { .malloc_fn = malloc, .free_fn = free, }; cJSON_InitHooks( &hooks ); tmp = cJSON_Parse( payload.buf ); if( NULL != tmp ) { *tree = tmp; rv = 0; } } if( NULL != payload.buf ) { free( payload.buf ); } curl_easy_cleanup( curl ); }
void OverrideJSONMallocFree() { OverrodeJSONMemoryAllocation = true; cJSON_Hooks hooks; hooks.malloc_fn = MyJSONMalloc; hooks.free_fn = MyJSONFree; cJSON_InitHooks( &hooks ); }
/** * struct2json library initialize * @note It will initialize cJSON library hooks. */ void s2j_init(S2jHook *hook) { /* initialize cJSON library */ cJSON_InitHooks((cJSON_Hooks *)hook); /* initialize hooks */ if (hook) { s2jHook.malloc_fn = (hook->malloc_fn) ? hook->malloc_fn : malloc; s2jHook.free_fn = (hook->free_fn) ? hook->free_fn : free; } else { hook->malloc_fn = malloc; hook->free_fn = free; } }
void StatQuery_Init( void ) { cJSON_Hooks hooks; if( sq_mempool == NULL ) { sq_mempool = Mem_AllocPool( NULL, "StatQuery" ); } // already initialized? if( sq_refcount++ > 0 ) { return; } // populate API structure sq_export.CreateQuery = StatQuery_CreateQuery; sq_export.DestroyQuery = StatQuery_DestroyQuery; sq_export.SetCallback = StatQuery_SetCallback; sq_export.Send = StatQuery_Send; sq_export.SetField = StatQuery_SetField; sq_export.GetRoot = StatQuery_GetRoot; sq_export.GetSection = StatQuery_GetSection; sq_export.GetNumber = StatQuery_GetNumber; sq_export.GetString = StatQuery_GetString; sq_export.GetArraySection = StatQuery_GetArraySection; sq_export.GetArrayNumber = StatQuery_GetArrayNumber; sq_export.GetArrayString = StatQuery_GetArrayString; sq_export.CreateSection = StatQuery_CreateSection; sq_export.CreateArray = StatQuery_CreateArray; sq_export.SetString = StatQuery_SetString; sq_export.SetNumber = StatQuery_SetNumber; sq_export.SetArrayString = StatQuery_SetArrayString; sq_export.SetArrayNumber = StatQuery_SetArrayNumber; sq_export.AddArrayString = StatQuery_AddArrayString; sq_export.AddArrayNumber = StatQuery_AddArrayNumber; sq_export.GetRawResponse = StatQuery_GetRawResponse; sq_export.GetTokenizedResponse = StatQuery_GetTokenizedResponse; sq_export.Poll = StatQuery_Poll; // init JSON hooks.malloc_fn = SQ_JSON_Alloc; hooks.free_fn = SQ_JSON_Free; cJSON_InitHooks( &hooks ); }
const char* SerializeReflection(ShaderInfo* psReflection) { cJSON* root; cJSON_Hooks hooks; hooks.malloc_fn = jsonMalloc; hooks.free_fn = jsonFree; cJSON_InitHooks(&hooks); root=cJSON_CreateObject(); cJSON_AddItemToObject(root, "ui32MajorVersion", cJSON_CreateNumber(psReflection->ui32MajorVersion)); cJSON_AddItemToObject(root, "ui32MinorVersion", cJSON_CreateNumber(psReflection->ui32MinorVersion)); cJSON_AddItemToObject(root, "ui32NumInputSignatures", cJSON_CreateNumber(psReflection->ui32NumInputSignatures)); for(uint32_t i = 0; i < psReflection->ui32NumInputSignatures; ++i) { std::string name; name += "input"; AppendIntToString(name, i); cJSON* obj = cJSON_CreateObject(); cJSON_AddItemToObject(root, name.c_str(), obj); WriteInOutSignature(psReflection->psInputSignatures+i, obj); } cJSON_AddItemToObject(root, "ui32NumOutputSignatures", cJSON_CreateNumber(psReflection->ui32NumOutputSignatures)); for(uint32_t i = 0; i < psReflection->ui32NumOutputSignatures; ++i) { std::string name; name += "output"; AppendIntToString(name, i); cJSON* obj = cJSON_CreateObject(); cJSON_AddItemToObject(root, name.c_str(), obj); WriteInOutSignature(psReflection->psOutputSignatures+i, obj); } cJSON_AddItemToObject(root, "ui32NumResourceBindings", cJSON_CreateNumber(psReflection->ui32NumResourceBindings)); for(uint32_t i = 0; i < psReflection->ui32NumResourceBindings; ++i) { std::string name; name += "resource"; AppendIntToString(name, i); cJSON* obj = cJSON_CreateObject(); cJSON_AddItemToObject(root, name.c_str(), obj); WriteResourceBinding(psReflection->psResourceBindings+i, obj); } cJSON_AddItemToObject(root, "ui32NumConstantBuffers", cJSON_CreateNumber(psReflection->ui32NumConstantBuffers)); for(uint32_t i = 0; i < psReflection->ui32NumConstantBuffers; ++i) { std::string name; name += "cbuf"; AppendIntToString(name, i); cJSON* obj = cJSON_CreateObject(); cJSON_AddItemToObject(root, name.c_str(), obj); WriteConstantBuffer(psReflection->psConstantBuffers+i, obj); } //psThisPointerConstBuffer is a cache. Don't need to write this out. //It just points to the $ThisPointer cbuffer within the psConstantBuffers array. for(uint32_t i = 0; i < psReflection->ui32NumClassTypes; ++i) { std::string name; name += "classType"; AppendIntToString(name, i); cJSON* obj = cJSON_CreateObject(); cJSON_AddItemToObject(root, name.c_str(), obj); WriteClassType(psReflection->psClassTypes+i, obj); } for(uint32_t i = 0; i < psReflection->ui32NumClassInstances; ++i) { std::string name; name += "classInst"; AppendIntToString(name, i); cJSON* obj = cJSON_CreateObject(); cJSON_AddItemToObject(root, name.c_str(), obj); WriteClassInstance(psReflection->psClassInstances+i, obj); } //psReflection->aui32TableIDToTypeID //psReflection->aui32ConstBufferBindpointRemap cJSON_AddItemToObject(root, "eTessPartitioning", cJSON_CreateNumber(psReflection->eTessPartitioning)); cJSON_AddItemToObject(root, "eTessOutPrim", cJSON_CreateNumber(psReflection->eTessOutPrim)); const char* jsonString = cJSON_Print(root); cJSON_Delete(root); return jsonString; }