From dcc3d8a9fc9a8c57a1004712fbf29d776477d00b Mon Sep 17 00:00:00 2001 From: Hui Yu Date: Tue, 14 Jun 2022 10:38:10 +0800 Subject: [PATCH 1/2] Support free-form objects --- kubernetes/model/object.c | 36 ++++++++++++++++++++++++++++-------- kubernetes/model/object.h | 2 +- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/kubernetes/model/object.c b/kubernetes/model/object.c index d1066938..9606d41d 100644 --- a/kubernetes/model/object.c +++ b/kubernetes/model/object.c @@ -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(); + if (!object) { + return NULL; + } - return item; -fail: - cJSON_Delete(item); - return NULL; + if (!object->temporary) { + return cJSON_Parse("{}"); + } + + return cJSON_Parse(object->temporary); } -object_t *object_parseFromJSON(char *jsonString){ - object_t *object = NULL; +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; } diff --git a/kubernetes/model/object.h b/kubernetes/model/object.h index 18503e0c..35bfeb60 100644 --- a/kubernetes/model/object.h +++ b/kubernetes/model/object.h @@ -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); From 878fde5df9516451e3b5344fbcc026425826d276 Mon Sep 17 00:00:00 2001 From: Hui Yu Date: Thu, 16 Jun 2022 10:49:33 +0800 Subject: [PATCH 2/2] Set the default value for the null json --- kubernetes/model/object.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kubernetes/model/object.c b/kubernetes/model/object.c index 9606d41d..ff293bcd 100644 --- a/kubernetes/model/object.c +++ b/kubernetes/model/object.c @@ -28,7 +28,7 @@ cJSON *object_convertToJSON(object_t *object) { } if (!object->temporary) { - return cJSON_Parse("{}"); + return cJSON_Parse("null"); } return cJSON_Parse(object->temporary);