cbool cjson_set_null_idx(cjson* pJson, int idx) { if(!pJson) { return 0; } cJSON* lp_dest_json = pJson->child; if(!lp_dest_json || lp_dest_json->type != cJSON_Array) { return 0; } if(cJSON_GetArrayItem(lp_dest_json, idx)!= NULL) { cJSON_ReplaceItemInArray(lp_dest_json, idx, cJSON_CreateNull() ); } else { cJSON_AddItemToArray(lp_dest_json, cJSON_CreateNull() ); } return 1; }
cJSON *cJSONUtils_GenerateMergePatch(cJSON *from, cJSON *to) { cJSON *patch = 0; if (!to) { /* patch to delete everything */ return cJSON_CreateNull(); } if ((to->type != cJSON_Object) || !from || (from->type != cJSON_Object)) { return cJSON_Duplicate(to, 1); } cJSONUtils_SortObject(from); cJSONUtils_SortObject(to); from = from->child; to = to->child; patch = cJSON_CreateObject(); while (from || to) { int compare = from ? (to ? strcmp(from->string, to->string) : -1) : 1; if (compare < 0) { /* from has a value that to doesn't have -> remove */ cJSON_AddItemToObject(patch, from->string, cJSON_CreateNull()); from = from->next; } else if (compare > 0) { /* to has a value that from doesn't have -> add to patch */ cJSON_AddItemToObject(patch, to->string, cJSON_Duplicate(to, 1)); to = to->next; } else { /* object key exists in both objects */ if (cJSONUtils_Compare(from, to)) { /* not identical --> generate a patch */ cJSON_AddItemToObject(patch, to->string, cJSONUtils_GenerateMergePatch(from, to)); } /* next key in the object */ from = from->next; to = to->next; } } if (!patch->child) { cJSON_Delete(patch); return 0; } return patch; }
ssap_message* generateSubscribeMessageWithQueryType(const char* sessionKey, const char* ontology, const char* query, SSAPQueryType queryType, int msRefresh){ ssap_message* subscribeMessage = allocateSsapMessage(); cJSON *body; body=cJSON_CreateObject(); cJSON_AddItemToObject(body, "data", cJSON_CreateNull()); cJSON_AddItemToObject(body, "query", cJSON_CreateString(query)); cJSON_AddItemToObject(body, "msRefresh", cJSON_CreateNumber(msRefresh)); switch(queryType){ case NATIVE: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("NATIVE")); break; case SQLLIKE: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("SQLLIKE")); break; case SIB_DEFINED: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("SIB_DEFINED")); break; case CEP: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("CEP")); break; case BDH: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("BDH")); break; default: cJSON_AddItemToObject(body, "queryType", cJSON_CreateNull()); break; } subscribeMessage->body=cJSON_PrintUnformatted(body); subscribeMessage->direction = REQUEST; subscribeMessage->messageId = NULL; subscribeMessage->messageType=SUBSCRIBE; if(ontology!=NULL){ subscribeMessage->ontology=(char*) malloc((strlen(ontology)+1)*sizeof(char)); strcpy(subscribeMessage->ontology, ontology); }else{ subscribeMessage->ontology = NULL; } subscribeMessage->sessionKey=(char*) malloc((strlen(sessionKey)+1)*sizeof(char)); strcpy(subscribeMessage->sessionKey, sessionKey); subscribeMessage->persistenceType=MONGODB; cJSON_Delete(body); return subscribeMessage; }
ssap_message* generateQueryMessageWithQueryType(const char* sessionKey, const char* ontology, const char* query, SSAPQueryType queryType){ ssap_message* queryMessage = allocateSsapMessage(); cJSON *body; //Crea la raiz del JSON body=cJSON_CreateObject(); //Añade las propiedades al body cJSON_AddItemToObject(body, "data", cJSON_CreateNull()); cJSON_AddItemToObject(body, "query", cJSON_CreateString(query)); switch(queryType){ case NATIVE: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("NATIVE")); break; case SQLLIKE: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("SQLLIKE")); break; case SIB_DEFINED: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("SIB_DEFINED")); break; case BDH: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("BDH")); break; default: cJSON_AddItemToObject(body, "queryType", cJSON_CreateNull()); break; } queryMessage->body=cJSON_PrintUnformatted(body); queryMessage->direction = REQUEST; queryMessage->messageId = NULL; queryMessage->messageType=QUERY; if(ontology!=NULL){ queryMessage->ontology=(char*) malloc((strlen(ontology)+1)*sizeof(char)); strcpy(queryMessage->ontology, ontology); }else{ queryMessage->ontology = NULL; } queryMessage->sessionKey=(char*) malloc((strlen(sessionKey)+1)*sizeof(char)); strcpy(queryMessage->sessionKey, sessionKey); queryMessage->persistenceType=MONGODB; cJSON_Delete(body); return queryMessage; }
void JSONItem::arrayAppend(const JSONItem& element) { if(!_json) { return; } cJSON* p = NULL; switch(element.getType()) { case cJSON_False: p = cJSON_CreateFalse(); break; case cJSON_True: p = cJSON_CreateTrue(); break; case cJSON_NULL: p = cJSON_CreateNull(); break; case cJSON_Number: p = cJSON_CreateNumber(element.getValue().GetDouble()); break; case cJSON_String: p = cJSON_CreateString(element.getValue().GetString().mb_str(wxConvUTF8).data()); break; case cJSON_Array: case cJSON_Object: p = element._json; break; } if(p) { cJSON_AddItemToArray(_json, p); } }
cJSON *Value::toCJSON(const Value &value) { switch (value.type()) { case Value::Type_Boolean: return value.toBool() ? cJSON_CreateTrue() : cJSON_CreateFalse(); case Value::Type_Date: case Value::Type_Integer: return cJSON_CreateNumber(value.toInteger()); case Value::Type_Double: return cJSON_CreateNumber(value.toDouble()); case Value::Type_String: return cJSON_CreateString(value.toString().constData()); case Value::Type_List: { cJSON *array = cJSON_CreateArray(); for (const auto &v : *value.listPtr()) cJSON_AddItemToArray(array, toCJSON(v)); return array; } case Value::Type_Map: { cJSON *object = cJSON_CreateObject(); for (const auto &v : *value.mapPtr()) cJSON_AddItemToObject(object, v.first.constData(), v.second.toCJSON(v.second)); return object; } case Value::Type_Invalid: break; case Value::Type_Undefined: break; case Value::Type_Custom: if (std::shared_ptr<Value::Custom> custom = value.toCustom()) { cJSON *ret = cJSON_CreateString(custom->toString().constData()); if (ret) { ret->type = cJSON_RawString; return ret; } } break; } return cJSON_CreateNull(); }
ssap_message* generateSIBDefinedSubscribeMessageWithParam(const char* sessionKey, const char* query, map_t *params){ ssap_message* queryMessage = allocateSsapMessage(); cJSON *body; body=cJSON_CreateObject(); cJSON_AddItemToObject(body, "data", cJSON_CreateNull()); cJSON_AddItemToObject(body, "query", cJSON_CreateString(query)); cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("SIB_DEFINED")); cJSON *queryParams; queryParams=cJSON_CreateObject(); size_t i; for(i=0;i<params->size;i++){ cJSON_AddItemToObject(queryParams, params->data[i].key, cJSON_CreateString(params->data[i].value)); } cJSON_AddItemToObject(body, "queryParams", queryParams); queryMessage->body=cJSON_PrintUnformatted(body); queryMessage->direction = REQUEST; queryMessage->messageId = NULL; queryMessage->messageType=QUERY; queryMessage->ontology=NULL; queryMessage->sessionKey=(char*) malloc((strlen(sessionKey)+1)*sizeof(char)); strcpy(queryMessage->sessionKey, sessionKey); queryMessage->persistenceType=MONGODB; cJSON_Delete(body); //This method frees the memory of the key-value pairs freeMap(params); return queryMessage; }
int ont_video_dev_fileinfo_upload(void *_dev, int channel, t_ont_video_file *list, int n) { ont_device_t *dev = _dev; char dsname[16]; int i = 0; ont_platform_snprintf(dsname, sizeof(dsname), "ont_video_%d_mqtttestvideo", channel); cJSON *json = NULL; char *jsonValue = NULL; //example //"beginTime" : "2016-10-19 16:30:30", //"endTime" : "2016-10-20 16:30:30", //"vedioDesc" : "video2" for (i = 0; i < n; i++) { json = cJSON_CreateNull(); cJSON_AddItemToObject(json, "dst", cJSON_CreateString("video")); cJSON_AddItemToObject(json, "beginTime", cJSON_CreateString(list[i].begin_time)); cJSON_AddItemToObject(json, "endTime", cJSON_CreateString(list[i].end_time)); cJSON_AddItemToObject(json, "vedioDesc", cJSON_CreateString(list[i].descrtpion)); jsonValue = cJSON_PrintUnformatted(json); ont_device_add_dp_object(dev, dsname, jsonValue); ont_platform_free(jsonValue); cJSON_Delete(json); } ont_device_send_dp(dev); return 0; }
ssap_message* generateSubscribeMessage(const char* sessionKey, const char* ontology, const char* query, int msRefresh){ ssap_message* subscribeMessage = allocateSsapMessage(); cJSON *body; body=cJSON_CreateObject(); cJSON_AddItemToObject(body, "data", cJSON_CreateNull()); cJSON_AddItemToObject(body, "query", cJSON_CreateString(query)); cJSON_AddItemToObject(body, "msRefresh", cJSON_CreateNumber(msRefresh)); subscribeMessage->body=cJSON_PrintUnformatted(body); subscribeMessage->direction = REQUEST; subscribeMessage->messageId = NULL; subscribeMessage->messageType=SUBSCRIBE; subscribeMessage->ontology=(char*) malloc((strlen(ontology)+1)*sizeof(char)); strcpy(subscribeMessage->ontology, ontology); subscribeMessage->sessionKey=(char*) malloc((strlen(sessionKey)+1)*sizeof(char)); strcpy(subscribeMessage->sessionKey, sessionKey); subscribeMessage->persistenceType=MONGODB; cJSON_Delete(body); return subscribeMessage; }
// lua_to_json inline cJSON* lua_to_json(lua_State* L){ if(lua_objlen(L, -1) > 0){ cJSON* ele =cJSON_CreateArray(); const int len =lua_objlen(L, -1); for(int i=1; i<=len; ++i){ lua_rawgeti(L, -1, i); if(lua_isnil(L, -1)){ cJSON_AddItemToArray(ele, cJSON_CreateNull()); } else if(lua_isboolean(L, -1)){ if(lua_toboolean(L, -1)){ cJSON_AddItemToArray(ele, cJSON_CreateTrue()); } else{ cJSON_AddItemToArray(ele, cJSON_CreateFalse()); } } else if(lua_isnumber(L, -1)){ cJSON_AddItemToArray(ele, cJSON_CreateNumber(lua_tonumber(L, -1))); } else if(lua_isstring(L, -1)){ cJSON_AddItemToArray(ele, cJSON_CreateString(lua_tostring(L, -1))); } else if(lua_istable(L, -1)){ cJSON_AddItemToArray(ele, lua_to_json(L)); } lua_pop(L, 1); } return ele; } else{ cJSON* ele =cJSON_CreateObject(); lua_pushnil(L); while(0 != lua_next(L, -2)){ if(lua_isstring(L, -2)){ const char* name =lua_tostring(L, -2); if(lua_isboolean(L, -1)){ if(lua_toboolean(L, -1)){ cJSON_AddTrueToObject(ele, name); } else{ cJSON_AddFalseToObject(ele, name); } } else if(lua_isnumber(L, -1)){ cJSON_AddNumberToObject(ele, name, lua_tonumber(L, -1)); } else if(lua_isstring(L, -1)){ cJSON_AddStringToObject(ele, name, lua_tostring(L, -1)); } else if(lua_istable(L, -1)){ cJSON_AddItemToObject(ele, name, lua_to_json(L)); } } lua_pop(L, 1); } return ele; } }
int sg_json_set_object(sg_json_doc_t *doc, const char *json_pointer, sg_json_doc_t *value) { cJSON *json = cast_to_cJSON(doc); if (value == NULL) value = cJSON_CreateNull(); return set_value(json, json_pointer, cast_to_cJSON(value)); }
char* ssap_messageToJson(ssap_message* message){ cJSON *root; root=cJSON_CreateObject(); if(message->body==NULL || strcmp(message->body, "")==0){ cJSON_AddItemToObject(root, "body", cJSON_CreateNull()); }else{ cJSON_AddItemToObject(root, "body", cJSON_CreateString(message->body)); } switch(message->direction){ case REQUEST: cJSON_AddItemToObject(root, "direction", cJSON_CreateString("REQUEST")); break; case RESPONSE: cJSON_AddItemToObject(root, "direction", cJSON_CreateString("RESPONSE")); break; default: cJSON_AddItemToObject(root, "direction", cJSON_CreateNull()); break; } cJSON_AddItemToObject(root, "messageType", cJSON_CreateString(messageTypeToString(message->messageType))); if(message->messageId==NULL || strcmp(message->messageId, "")==0){ cJSON_AddItemToObject(root, "messageId", cJSON_CreateNull()); }else{ cJSON_AddItemToObject(root, "messageId", cJSON_CreateString(message->messageId)); } if(message->ontology==NULL || strcmp(message->ontology, "")==0){ cJSON_AddItemToObject(root, "ontology", cJSON_CreateNull()); }else{ cJSON_AddItemToObject(root, "ontology", cJSON_CreateString(message->ontology)); } if(message->sessionKey==NULL || strcmp(message->sessionKey, "")==0){ cJSON_AddItemToObject(root, "sessionKey", cJSON_CreateNull()); }else{ cJSON_AddItemToObject(root, "sessionKey", cJSON_CreateString(message->sessionKey)); } char* retorno = cJSON_PrintUnformatted(root); cJSON_Delete(root); return retorno; }
virtual void visitObject(const CCObject *p) { if (CCNull *p = dynamic_cast<CCNull *>(p)) { m_json = cJSON_CreateNull(); } else { CCLOGERROR("Object of type '%s' must not be converted to JSON.", typeid(*p).name()); m_json = NULL; } }
ssap_message* generateConfigMessage(const char* kp, const char* instance, const char* token, const char* assetService, map_t* assetServiceParam) { ssap_message* configMessage = allocateSsapMessage(); cJSON *body; cJSON* assetServiceParam_json = NULL; body = cJSON_CreateObject(); cJSON_AddItemToObject(body, "token", cJSON_CreateString(token)); cJSON_AddItemToObject(body, "instanciaKp", cJSON_CreateString(instance)); cJSON_AddItemToObject(body, "kp", cJSON_CreateString(kp)); if (assetService != NULL){ cJSON_AddItemToObject(body, "assetService", cJSON_CreateString(assetService)); } else { cJSON_AddItemToObject(body, "assetService", cJSON_CreateNull()); } if (assetServiceParam != NULL){ assetServiceParam_json = cJSON_CreateObject(); size_t i; for(i = 0; i < assetServiceParam->size; i++){ cJSON_AddItemToObject(assetServiceParam_json, assetServiceParam->data[i].key, cJSON_CreateString(assetServiceParam->data[i].value)); } cJSON_AddItemToObject(body, "assetServiceParam", cJSON_CreateString(cJSON_PrintUnformatted(assetServiceParam_json))); } else { cJSON_AddItemToObject(body, "assetServiceParam", cJSON_CreateNull()); } configMessage->body=cJSON_PrintUnformatted(body); configMessage->direction = REQUEST; configMessage->messageId=NULL; configMessage->messageType=CONFIG; configMessage->ontology=NULL; configMessage->sessionKey=NULL; configMessage->persistenceType=MONGODB; cJSON_Delete(body); if (assetServiceParam_json != NULL){ cJSON_Delete(assetServiceParam_json); freeMap(assetServiceParam); } return configMessage; }
JSON::JSON(int type) : _json(NULL) { if(type == cJSON_Array) _json = cJSON_CreateArray(); else if(type == cJSON_NULL) _json = cJSON_CreateNull(); else _json = cJSON_CreateObject(); }
ssap_message* generateInsertMessageWithQueryType(const char* sessionKey, const char* ontology, const char* data, SSAPQueryType queryType){ ssap_message* insertMessage = allocateSsapMessage(); cJSON *body; body=cJSON_CreateObject(); switch(queryType){ case NATIVE: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("NATIVE")); cJSON_AddItemToObject(body, "query", cJSON_CreateNull()); cJSON_AddItemToObject(body, "data", cJSON_CreateString(data)); break; case SQLLIKE: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("SQLLIKE")); cJSON_AddItemToObject(body, "query", cJSON_CreateString(data)); cJSON_AddItemToObject(body, "data", cJSON_CreateNull()); break; case BDH: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("BDH")); cJSON_AddItemToObject(body, "query", cJSON_CreateString(data)); cJSON_AddItemToObject(body, "data", cJSON_CreateNull()); break; default: cJSON_AddItemToObject(body, "queryType", cJSON_CreateNull()); cJSON_AddItemToObject(body, "query", cJSON_CreateNull()); cJSON_AddItemToObject(body, "data", cJSON_CreateString(data)); break; } insertMessage->body=cJSON_PrintUnformatted(body); insertMessage->direction = REQUEST; insertMessage->messageId = NULL; insertMessage->messageType=INSERT; insertMessage->ontology=(char*) malloc((strlen(ontology)+1)*sizeof(char)); strcpy(insertMessage->ontology, ontology); insertMessage->sessionKey=(char*) malloc((strlen(sessionKey)+1)*sizeof(char)); strcpy(insertMessage->sessionKey, sessionKey); insertMessage->persistenceType=MONGODB; cJSON_Delete(body); return insertMessage; }
cJSON *cJSON_CreateString(const char *string) { if (!string) return cJSON_CreateNull(); cJSON *item = cJSON_New_Item(); if (item) { item->type = cJSON_String; item->valuestring = cJSON_strdup(string); } return item; }
static cJSON* json_encode_tree(lua_State *L) { int type = lua_type(L, -1); if(type == LUA_TNIL) { cJSON *json = cJSON_CreateNull(); return json; } else if(type == LUA_TBOOLEAN) { int b = lua_toboolean(L, -1); cJSON* json = cJSON_CreateBool(b); return json; } else if(type == LUA_TNUMBER) { double num = lua_tonumber(L, -1); cJSON* json = cJSON_CreateNumber(num); return json; } else if(type == LUA_TSTRING) { size_t str_len; const char *string = lua_tolstring(L, -1, &str_len); cJSON* json = cJSON_CreateString(string); return json; } else if(type == LUA_TTABLE) { cJSON *json = cJSON_CreateObject(); if(json == NULL) { LOG_ERROR("null"); return NULL; } lua_pushnil(L); while(lua_next(L, -2) != 0) { if(lua_type(L, -2) == LUA_TSTRING) { size_t str_len = 0; const char *k = lua_tolstring(L, -2, &str_len); cJSON *cjson = json_encode_tree(L); if(cjson == NULL) { LOG_ERROR("null"); break; } cJSON_AddItemToObject(json, k, cjson); } else if(lua_type(L, -2) == LUA_TNUMBER) { int k = (int)lua_tonumber(L, -2); cJSON *cjson = json_encode_tree(L); if(cjson == NULL) { LOG_ERROR("null"); break; } char kname[32]; sprintf(kname, "%d", k); cJSON_AddItemToObject(json, kname, cjson); } lua_pop(L, 1); } return json; } LOG_ERROR("unspport type"); return NULL; }
cbool cjson_set_null(cjson* pJson, const char* name) { if(!pJson || !name) { return 0; } cJSON* lp_dest_json = pJson->child; if(!lp_dest_json ||lp_dest_json->type != cJSON_Object) { return 0; } if(cJSON_GetObjectItem(lp_dest_json, name)!= NULL) { cJSON_ReplaceItemInObject(lp_dest_json, name, cJSON_CreateNull() ); } else { cJSON_AddItemToObject(lp_dest_json, name, cJSON_CreateNull() ); } return 1; }
static cJSON * azy_value_serialize_json(const Eina_Value *val) { const Eina_Value_Type *type; EINA_SAFETY_ON_NULL_RETURN_VAL(val, NULL); type = eina_value_type_get(val); if (type == EINA_VALUE_TYPE_ARRAY) return azy_value_serialize_array_json(val); if (type == EINA_VALUE_TYPE_STRUCT) return azy_value_serialize_struct_json(val); else if (type) return azy_value_serialize_basic_json(val); return cJSON_CreateNull(); }
int ont_video_dev_set_channels(void *_dev, int channels) { ont_device_t *dev = _dev; char dsname[32]; int i = 0; cJSON *json = cJSON_CreateNull(); cJSON_AddItemToObject(json, "null", cJSON_CreateString("null")); char *jsonValue = cJSON_PrintUnformatted(json); for (i = 0; i < channels; i++) { ont_platform_snprintf(dsname, sizeof(dsname), "ont_video_%d_mqtttestvideo", i + 1); ont_device_add_dp_object(dev, dsname, jsonValue); } ont_device_send_dp(dev); ont_platform_free(jsonValue); cJSON_Delete(json); return 0; }
cJSON *JSONNode::spawnNode(int nodeType, char *name) { cJSON *node = NULL; if(nodeType == cJSON_Object) { node = cJSON_CreateObject(); node -> string = name; }else if(nodeType == cJSON_Array){ node = cJSON_CreateArray(); node -> string = name; }else if(nodeType == cJSON_True){ node = cJSON_CreateTrue(); node -> string = name; }else if(nodeType == cJSON_False){ node = cJSON_CreateFalse(); node -> string = name; }else if(nodeType == cJSON_NULL){ node = cJSON_CreateNull(); node -> string = name; } return node; }
static cJSON * _encode_item(lua_State *L, int idx) { switch(lua_type(L, idx)) { case LUA_TNIL: return cJSON_CreateNull(); case LUA_TBOOLEAN: return cJSON_CreateBool(lua_toboolean(L, idx)); case LUA_TNUMBER: return cJSON_CreateNumber(luaL_checknumber(L, idx)); case LUA_TSTRING: return cJSON_CreateString(luaL_checkstring(L, idx)); case LUA_TTABLE: if(!lua_objlen(L, idx)) return _encode_object(L, idx); else return _encode_array(L, idx); default: fprintf(stderr, "cannot encode %s", lua_typename(L, idx)); return NULL; } }
static cJSON *tocJSON(const QVariant &variant) { cJSON *ret = 0; switch (variant.type()) { case QVariant::Invalid: ret = cJSON_CreateNull(); break; case QVariant::Bool: ret = (variant.toBool() ? cJSON_CreateTrue() : cJSON_CreateFalse()); break; case QVariant::Int: ret = cJSON_CreateNumber(variant.toInt()); break; case QVariant::UInt: ret = cJSON_CreateNumber(variant.toUInt()); break; case QVariant::LongLong: ret = cJSON_CreateNumber(variant.toLongLong()); break; case QVariant::ULongLong: ret = cJSON_CreateNumber(variant.toULongLong()); break; case QVariant::Double: ret = cJSON_CreateNumber(variant.toDouble()); break; case QVariant::String: ret = cJSON_CreateString(variant.toString().toUtf8().constData()); break; case QVariant::ByteArray: ret = cJSON_CreateString(variant.toByteArray().constData()); break; case QVariant::List: ret = cJSON_CreateArray(); Q_ASSERT(ret); foreach(const QVariant &item, variant.toList()) cJSON_AddItemToArray(ret, tocJSON(item)); break; case QVariant::Map: { ret = cJSON_CreateObject(); Q_ASSERT(ret); const QVariantMap map = variant.toMap(); for (QMap<QString, QVariant>::const_iterator it = map.begin(); it != map.end(); ++it) cJSON_AddItemToObject(ret, it.key().toUtf8().constData(), tocJSON(it.value())); break; } case QVariant::StringList: ret = cJSON_CreateArray(); Q_ASSERT(ret); foreach(const QString &string, variant.toStringList()) cJSON_AddItemToArray(ret, tocJSON(string)); break; default: qWarning("Can't convert variant to json %d", variant.type()); } return ret; }
static cJSON *generate_merge_patch(cJSON * const from, cJSON * const to, const cJSON_bool case_sensitive) { cJSON *from_child = NULL; cJSON *to_child = NULL; cJSON *patch = NULL; if (to == NULL) { /* patch to delete everything */ return cJSON_CreateNull(); } if (!cJSON_IsObject(to) || !cJSON_IsObject(from)) { return cJSON_Duplicate(to, 1); } sort_object(from, case_sensitive); sort_object(to, case_sensitive); from_child = from->child; to_child = to->child; patch = cJSON_CreateObject(); while (from_child || to_child) { int diff; if (from_child != NULL) { if (to_child != NULL) { diff = strcmp(from_child->string, to_child->string); } else { diff = -1; } } else { diff = 1; } if (diff < 0) { /* from has a value that to doesn't have -> remove */ cJSON_AddItemToObject(patch, from_child->string, cJSON_CreateNull()); from_child = from_child->next; } else if (diff > 0) { /* to has a value that from doesn't have -> add to patch */ cJSON_AddItemToObject(patch, to_child->string, cJSON_Duplicate(to_child, 1)); to_child = to_child->next; } else { /* object key exists in both objects */ if (!compare_json(from_child, to_child, case_sensitive)) { /* not identical --> generate a patch */ cJSON_AddItemToObject(patch, to_child->string, cJSONUtils_GenerateMergePatch(from_child, to_child)); } /* next key in the object */ from_child = from_child->next; to_child = to_child->next; } } if (patch->child == NULL) { /* no patch generated */ cJSON_Delete(patch); return NULL; } return patch; }