| 1389 | } |
| 1390 | |
| 1391 | static cJSON *generate_merge_patch(cJSON * const from, cJSON * const to, const cJSON_bool case_sensitive) |
| 1392 | { |
| 1393 | cJSON *from_child = NULL; |
| 1394 | cJSON *to_child = NULL; |
| 1395 | cJSON *patch = NULL; |
| 1396 | if (to == NULL) |
| 1397 | { |
| 1398 | /* patch to delete everything */ |
| 1399 | return cJSON_CreateNull(); |
| 1400 | } |
| 1401 | if (!cJSON_IsObject(to) || !cJSON_IsObject(from)) |
| 1402 | { |
| 1403 | return cJSON_Duplicate(to, 1); |
| 1404 | } |
| 1405 | |
| 1406 | sort_object(from, case_sensitive); |
| 1407 | sort_object(to, case_sensitive); |
| 1408 | |
| 1409 | from_child = from->child; |
| 1410 | to_child = to->child; |
| 1411 | patch = cJSON_CreateObject(); |
| 1412 | if (patch == NULL) |
| 1413 | { |
| 1414 | return NULL; |
| 1415 | } |
| 1416 | while (from_child || to_child) |
| 1417 | { |
| 1418 | int diff; |
| 1419 | if (from_child != NULL) |
| 1420 | { |
| 1421 | if (to_child != NULL) |
| 1422 | { |
| 1423 | diff = strcmp(from_child->string, to_child->string); |
| 1424 | } |
| 1425 | else |
| 1426 | { |
| 1427 | diff = -1; |
| 1428 | } |
| 1429 | } |
| 1430 | else |
| 1431 | { |
| 1432 | diff = 1; |
| 1433 | } |
| 1434 | |
| 1435 | if (diff < 0) |
| 1436 | { |
| 1437 | /* from has a value that to doesn't have -> remove */ |
| 1438 | cJSON_AddItemToObject(patch, from_child->string, cJSON_CreateNull()); |
| 1439 | |
| 1440 | from_child = from_child->next; |
| 1441 | } |
| 1442 | else if (diff > 0) |
| 1443 | { |
| 1444 | /* to has a value that from doesn't have -> add to patch */ |
| 1445 | cJSON_AddItemToObject(patch, to_child->string, cJSON_Duplicate(to_child, 1)); |
| 1446 | |
| 1447 | to_child = to_child->next; |
| 1448 | } |
no test coverage detected
searching dependent graphs…