modbusd
modbus master daemon
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
cJSON_Utils.c File Reference
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "cJSON_Utils.h"
Include dependency graph for cJSON_Utils.c:

Functions

char * cJSONUtils_FindPointerFromObjectTo (cJSON *object, cJSON *target)
 
cJSONcJSONUtils_GetPointer (cJSON *object, const char *pointer)
 
int cJSONUtils_ApplyPatches (cJSON *object, cJSON *patches)
 
void cJSONUtils_AddPatchToArray (cJSON *array, const char *op, const char *path, cJSON *val)
 
cJSONcJSONUtils_GeneratePatches (cJSON *from, cJSON *to)
 
void cJSONUtils_SortObject (cJSON *object)
 
cJSONcJSONUtils_MergePatch (cJSON *target, cJSON *patch)
 
cJSONcJSONUtils_GenerateMergePatch (cJSON *from, cJSON *to)
 

Function Documentation

void cJSONUtils_AddPatchToArray ( cJSON array,
const char *  op,
const char *  path,
cJSON val 
)
238 {cJSONUtils_GeneratePatch(array,op,path,0,val);}
int cJSONUtils_ApplyPatches ( cJSON object,
cJSON patches 
)

References cJSON::child, cJSON_Array, cJSON::next, and cJSON::type.

210 {
211  int err;
212  if (patches->type!=cJSON_Array) return 1; /* malformed patches. */
213  if (patches) patches=patches->child;
214  while (patches)
215  {
216  if ((err=cJSONUtils_ApplyPatch(object,patches))) return err;
217  patches=patches->next;
218  }
219  return 0;
220 }
struct cJSON * next
Definition: cJSON.h:48
struct cJSON * child
Definition: cJSON.h:49
#define cJSON_Array
Definition: cJSON.h:40
int type
Definition: cJSON.h:50
char* cJSONUtils_FindPointerFromObjectTo ( cJSON object,
cJSON target 
)

References cJSON::child, cJSON_Array, cJSON_Object, cJSONUtils_FindPointerFromObjectTo(), cJSON::next, and cJSON::string.

Referenced by cJSONUtils_FindPointerFromObjectTo().

40 {
41  int type=object->type,c=0;cJSON *obj=0;
42 
43  if (object==target) return strdup("");
44 
45  for (obj=object->child;obj;obj=obj->next,c++)
46  {
47  char *found=cJSONUtils_FindPointerFromObjectTo(obj,target);
48  if (found)
49  {
50  if (type==cJSON_Array)
51  {
52  char *ret=(char*)malloc(strlen(found)+23);
53  sprintf(ret,"/%d%s",c,found);
54  free(found);
55  return ret;
56  }
57  else if (type==cJSON_Object)
58  {
59  char *ret=(char*)malloc(strlen(found)+cJSONUtils_PointerEncodedstrlen(obj->string)+2);
60  *ret='/';cJSONUtils_PointerEncodedstrcpy(ret+1,obj->string);
61  strcat(ret,found);
62  free(found);
63  return ret;
64  }
65  free(found);
66  return 0;
67  }
68  }
69  return 0;
70 }
struct cJSON * next
Definition: cJSON.h:48
struct cJSON * child
Definition: cJSON.h:49
#define cJSON_Object
Definition: cJSON.h:41
char * cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *target)
Definition: cJSON_Utils.c:39
#define cJSON_Array
Definition: cJSON.h:40
char * string
Definition: cJSON.h:59
Definition: cJSON.h:47

Here is the call graph for this function:

Here is the caller graph for this function:

cJSON* cJSONUtils_GenerateMergePatch ( cJSON from,
cJSON to 
)

References cJSON::child, cJSON_AddItemToObject(), cJSON_CreateNull(), cJSON_CreateObject(), cJSON_Delete(), cJSON_Duplicate(), cJSON_Object, cJSONUtils_GenerateMergePatch(), cJSONUtils_SortObject(), cJSON::next, cJSON::string, and cJSON::type.

Referenced by cJSONUtils_GenerateMergePatch().

366 {
367  cJSON *patch=0;
368  if (!to) return cJSON_CreateNull();
369  if (to->type!=cJSON_Object || !from || from->type!=cJSON_Object) return cJSON_Duplicate(to,1);
370  cJSONUtils_SortObject(from);
372  from=from->child;to=to->child;
373  patch=cJSON_CreateObject();
374  while (from || to)
375  {
376  int compare=from?(to?strcmp(from->string,to->string):-1):1;
377  if (compare<0)
378  {
380  from=from->next;
381  }
382  else if (compare>0)
383  {
385  to=to->next;
386  }
387  else
388  {
389  if (cJSONUtils_Compare(from,to)) cJSON_AddItemToObject(patch,to->string,cJSONUtils_GenerateMergePatch(from,to));
390  from=from->next;to=to->next;
391  }
392  }
393  if (!patch->child) {cJSON_Delete(patch);return 0;}
394  return patch;
395 }
cJSON * cJSON_CreateNull(void)
Definition: cJSON.c:696
void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
Definition: cJSON.c:676
struct cJSON * next
Definition: cJSON.h:48
struct cJSON * child
Definition: cJSON.h:49
#define cJSON_Object
Definition: cJSON.h:41
cJSON * cJSON_CreateObject(void)
Definition: cJSON.c:708
cJSON * cJSON_Duplicate(cJSON *item, int recurse)
Definition: cJSON.c:721
void cJSON_Delete(cJSON *c)
Definition: cJSON.c:81
int type
Definition: cJSON.h:50
char * string
Definition: cJSON.h:59
void cJSONUtils_SortObject(cJSON *object)
Definition: cJSON_Utils.c:344
Definition: cJSON.h:47
cJSON * cJSONUtils_GenerateMergePatch(cJSON *from, cJSON *to)
Definition: cJSON_Utils.c:365

Here is the call graph for this function:

Here is the caller graph for this function:

cJSON* cJSONUtils_GeneratePatches ( cJSON from,
cJSON to 
)

References cJSON_CreateArray().

299 {
300  cJSON *patches=cJSON_CreateArray();
301  cJSONUtils_CompareToPatch(patches,"",from,to);
302  return patches;
303 }
cJSON * cJSON_CreateArray(void)
Definition: cJSON.c:707
Definition: cJSON.h:47

Here is the call graph for this function:

cJSON* cJSONUtils_GetPointer ( cJSON object,
const char *  pointer 
)

References cJSON_Array, cJSON_GetArrayItem(), cJSON_Object, cJSON::string, and cJSON::type.

73 {
74  while (*pointer++=='/' && object)
75  {
76  if (object->type==cJSON_Array)
77  {
78  int which=0; while (*pointer>='0' && *pointer<='9') which=(10*which) + *pointer++ - '0';
79  if (*pointer && *pointer!='/') return 0;
80  object=cJSON_GetArrayItem(object,which);
81  }
82  else if (object->type==cJSON_Object)
83  {
84  object=object->child; while (object && cJSONUtils_Pstrcasecmp(object->string,pointer)) object=object->next; /* GetObjectItem. */
85  while (*pointer && *pointer!='/') pointer++;
86  }
87  else return 0;
88  }
89  return object;
90 }
#define cJSON_Object
Definition: cJSON.h:41
#define cJSON_Array
Definition: cJSON.h:40
int type
Definition: cJSON.h:50
cJSON * cJSON_GetArrayItem(cJSON *array, int item)
Definition: cJSON.c:665
char * string
Definition: cJSON.h:59

Here is the call graph for this function:

cJSON* cJSONUtils_MergePatch ( cJSON target,
cJSON patch 
)

References cJSON::child, cJSON_AddItemToObject(), cJSON_CreateObject(), cJSON_Delete(), cJSON_DeleteItemFromObject(), cJSON_DetachItemFromObject(), cJSON_Duplicate(), cJSON_NULL, cJSON_Object, cJSONUtils_MergePatch(), cJSON::next, cJSON::string, and cJSON::type.

Referenced by cJSONUtils_MergePatch().

347 {
348  if (!patch || patch->type != cJSON_Object) {cJSON_Delete(target);return cJSON_Duplicate(patch,1);}
349  if (!target || target->type != cJSON_Object) {cJSON_Delete(target);target=cJSON_CreateObject();}
350 
351  patch=patch->child;
352  while (patch)
353  {
354  if (patch->type == cJSON_NULL) cJSON_DeleteItemFromObject(target,patch->string);
355  else
356  {
357  cJSON *replaceme=cJSON_DetachItemFromObject(target,patch->string);
358  cJSON_AddItemToObject(target,patch->string,cJSONUtils_MergePatch(replaceme,patch));
359  }
360  patch=patch->next;
361  }
362  return target;
363 }
void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
Definition: cJSON.c:676
void cJSON_DeleteItemFromObject(cJSON *object, const char *string)
Definition: cJSON.c:685
struct cJSON * next
Definition: cJSON.h:48
struct cJSON * child
Definition: cJSON.h:49
#define cJSON_Object
Definition: cJSON.h:41
cJSON * cJSON_CreateObject(void)
Definition: cJSON.c:708
cJSON * cJSON_Duplicate(cJSON *item, int recurse)
Definition: cJSON.c:721
#define cJSON_NULL
Definition: cJSON.h:37
cJSON * cJSONUtils_MergePatch(cJSON *target, cJSON *patch)
Definition: cJSON_Utils.c:346
cJSON * cJSON_DetachItemFromObject(cJSON *object, const char *string)
Definition: cJSON.c:684
void cJSON_Delete(cJSON *c)
Definition: cJSON.c:81
int type
Definition: cJSON.h:50
char * string
Definition: cJSON.h:59
Definition: cJSON.h:47

Here is the call graph for this function:

Here is the caller graph for this function:

void cJSONUtils_SortObject ( cJSON object)

References cJSON::child.

Referenced by cJSONUtils_GenerateMergePatch().

344 {object->child=cJSONUtils_SortList(object->child);}
struct cJSON * child
Definition: cJSON.h:49

Here is the caller graph for this function: