2021-05-03 19:06:58 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2021-05-06 09:53:08 +00:00
|
|
|
#include <alloca.h>
|
2021-05-03 19:06:58 +00:00
|
|
|
|
|
|
|
#include <json.h>
|
|
|
|
#include <marshaller.h>
|
|
|
|
|
|
|
|
#include "demo.h"
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
const char* json = "{\
|
|
|
|
\"name\": \"How To Write A JSON Marshaller In C\",\
|
|
|
|
\"content\": \"TODO\",\
|
|
|
|
\"views\": null,\
|
|
|
|
\"user\": {\
|
|
|
|
\"uid\": 1000,\
|
|
|
|
\"username\": \"overflowerror\",\
|
2021-05-06 12:35:24 +00:00
|
|
|
\"aliases\": [\
|
|
|
|
\"foo\",\
|
|
|
|
\"bar\"\
|
|
|
|
],\
|
2021-05-03 19:06:58 +00:00
|
|
|
\"email\": \"overflow@persei.net\"\
|
|
|
|
}\
|
|
|
|
}";
|
|
|
|
|
|
|
|
post_t* post = json_unmarshall(post_t, json);
|
|
|
|
|
|
|
|
printf("Name: %s\n", post->name);
|
|
|
|
printf("Content: %s\n", post->content);
|
|
|
|
printf("Views: %s (%ld)\n", post->views == NULL ? "null" : "", post->views == NULL ? 0 : *post->views);
|
|
|
|
printf("User.Uid: %lld\n", post->user.uid);
|
|
|
|
printf("User.Username: %s\n", post->user.username);
|
|
|
|
printf("User.Email: %s\n", post->user.email);
|
2021-05-06 12:35:24 +00:00
|
|
|
printf("User.Aliases: ");
|
|
|
|
for (size_t i = 0; post->user.aliases[i] != NULL; i++) {
|
|
|
|
printf("%s ", post->user.aliases[i]);
|
|
|
|
}
|
|
|
|
printf("\n\n");
|
2021-05-03 19:06:58 +00:00
|
|
|
|
2021-05-04 15:07:51 +00:00
|
|
|
free(post->content);
|
2021-05-03 19:06:58 +00:00
|
|
|
post->content = "Just do it.";
|
|
|
|
|
2021-05-06 09:53:08 +00:00
|
|
|
char** aliases = alloca(sizeof(char*) * 3);
|
|
|
|
aliases[0] = "overflowerror";
|
|
|
|
aliases[1] = "overflow";
|
|
|
|
aliases[2] = NULL;
|
|
|
|
post->user.aliases = aliases;
|
|
|
|
|
2021-05-03 19:06:58 +00:00
|
|
|
char* newJson = json_marshall(post_t, post);
|
|
|
|
printf("%s\n", newJson);
|
|
|
|
free(newJson);
|
2021-05-04 15:07:51 +00:00
|
|
|
|
|
|
|
// set to NULL so it doesn't get freed
|
|
|
|
post->content = NULL;
|
2021-05-06 09:53:08 +00:00
|
|
|
post->user.aliases = NULL;
|
2021-05-04 15:07:51 +00:00
|
|
|
json_free_struct(post_t, post);
|
2021-05-03 19:06:58 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|