Skip to content

Commit

Permalink
[C][Client] Support freeform object
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuhui committed Jun 9, 2022
1 parent 14aef2c commit b78bba2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
36 changes: 28 additions & 8 deletions samples/client/petstore/c/model/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion samples/client/petstore/c/model/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit b78bba2

Please sign in to comment.