libargo/demo/marshaller.c

62 lines
1.4 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
2021-05-06 09:53:08 +00:00
#include <alloca.h>
#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\"\
],\
\"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");
free(post->content);
post->content = "Just do it.";
json_free_prim_array(post->user.aliases);
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;
char* newJson = json_marshall(post_t, post);
printf("%s\n", newJson);
free(newJson);
// set to NULL so it doesn't get freed
post->content = NULL;
2021-05-06 09:53:08 +00:00
post->user.aliases = NULL;
json_free_struct(post_t, post);
return 0;
}