[C][Client] Support freeform object (#12557)

This commit is contained in:
Hui Yu 2022-06-13 15:52:02 +08:00 committed by GitHub
parent a7db213c2d
commit bf22f38015
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 18 deletions

View File

@ -4,28 +4,48 @@
#include "object.h"
object_t *object_create() {
object_t *object = malloc(sizeof(object_t));
object_t *object = calloc(1, sizeof(object_t));
return object;
}
void object_free(object_t *object) {
if (!object) {
return ;
}
if (object->temporary) {
free(object->temporary);
object->temporary = NULL;
}
free (object);
}
cJSON *object_convertToJSON(object_t *object) {
cJSON *item = cJSON_CreateObject();
return item;
fail:
cJSON_Delete(item);
if (!object) {
return NULL;
}
object_t *object_parseFromJSON(char *jsonString){
object_t *object = NULL;
if (!object->temporary) {
return cJSON_Parse("{}");
}
return cJSON_Parse(object->temporary);
}
object_t *object_parseFromJSON(cJSON *json){
if (!json) {
goto end;
}
object_t *object = object_create();
if (!object) {
goto end;
}
object->temporary = cJSON_Print(json);
return object;
end:
return NULL;
}

View File

@ -20,7 +20,7 @@ object_t *object_create();
void object_free(object_t *object);
object_t *object_parseFromJSON(char *jsonString);
object_t *object_parseFromJSON(cJSON *json);
cJSON *object_convertToJSON(object_t *object);

View File

@ -4,28 +4,48 @@
#include "object.h"
object_t *object_create() {
object_t *object = malloc(sizeof(object_t));
object_t *object = calloc(1, sizeof(object_t));
return object;
}
void object_free(object_t *object) {
if (!object) {
return ;
}
if (object->temporary) {
free(object->temporary);
object->temporary = NULL;
}
free (object);
}
cJSON *object_convertToJSON(object_t *object) {
cJSON *item = cJSON_CreateObject();
return item;
fail:
cJSON_Delete(item);
if (!object) {
return NULL;
}
object_t *object_parseFromJSON(char *jsonString){
object_t *object = NULL;
if (!object->temporary) {
return cJSON_Parse("{}");
}
return cJSON_Parse(object->temporary);
}
object_t *object_parseFromJSON(cJSON *json){
if (!json) {
goto end;
}
object_t *object = object_create();
if (!object) {
goto end;
}
object->temporary = cJSON_Print(json);
return object;
end:
return NULL;
}

View File

@ -20,7 +20,7 @@ object_t *object_create();
void object_free(object_t *object);
object_t *object_parseFromJSON(char *jsonString);
object_t *object_parseFromJSON(cJSON *json);
cJSON *object_convertToJSON(object_t *object);