marshalling dynamic arrays works now

This commit is contained in:
overflowerror 2021-05-06 11:53:08 +02:00
parent c89a02ae6a
commit 99a539f243
7 changed files with 71 additions and 5 deletions

View file

@ -5,6 +5,7 @@
typedef struct {
long long uid;
char* username;
char** aliases;
char* email;
} user_t;

View file

@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <alloca.h>
#include <json.h>
#include <marshaller.h>
@ -15,6 +16,7 @@ int main() {
\"user\": {\
\"uid\": 1000,\
\"username\": \"overflowerror\",\
\"aliases\": null,\
\"email\": \"overflow@persei.net\"\
}\
}";
@ -32,12 +34,19 @@ int main() {
free(post->content);
post->content = "Just do it.";
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;
post->user.aliases = NULL;
json_free_struct(post_t, post);
return 0;

View file

@ -76,13 +76,19 @@ char* generateMarshallFunction(FILE* output, struct structinfo* info, char* suff
fprintf(output, "\t\t\"%s\", ", member->name);
const char* reference = "";
const char* comma = i == info->memberno - 1 ? "" : ",";
if (!member->type->isPointer && strcmp(member->type->type, "string") != 0) {
reference = "&";
if (member->type->isArray) {
fprintf(output, "_json_marshall_array_value(\"%s\", (void*) (d->%s))%s\n", member->type->type, member->name, comma);
} else {
const char* reference = "";
if (!member->type->isPointer && strcmp(member->type->type, "string") != 0) {
reference = "&";
}
fprintf(output, "_json_marshall_value(\"%s\", (void*) %s(d->%s))%s\n", member->type->type, reference, member->name, comma);
}
fprintf(output, "_json_marshall_value(\"%s\", (void*) %s(d->%s))%s\n", member->type->type, reference, member->name, i == info->memberno - 1 ? "" : ",");
}
fprintf(output, "\t);\n");

View file

@ -102,6 +102,23 @@ jsonValue_t* json_array(bool freeAfterwards, size_t size, ...) {
return value;
}
jsonValue_t* json_array_direct(bool freeAfterwards, size_t size, jsonValue_t* values[]) {
jsonValue_t* value = json_value();
value->type = JSON_ARRAY;
value->value.array.size = size;
value->value.array.entries = malloc(sizeof(jsonValue_t) * size);
for (size_t i = 0; i < size; i++) {
value->value.array.entries[i] = *values[i];
if (freeAfterwards) {
free(values[i]);
}
}
return value;
}
jsonValue_t* json_object(bool freeAfterwards, size_t size, ...) {
jsonValue_t* value = json_value();
value->type = JSON_OBJECT;

View file

@ -52,6 +52,8 @@ jsonValue_t* json_string(const char* s);
jsonValue_t* json_array(bool freeAfterwards, size_t size, ...);
jsonValue_t* json_object(bool freeAfterwards, size_t size, ...);
jsonValue_t* json_array_direct(bool freeAfterwards, size_t size, jsonValue_t* values[]);
void json_print(jsonValue_t* value);
jsonValue_t* json_clone(jsonValue_t* value);

View file

@ -2,6 +2,7 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <alloca.h>
#include "json.h"
#include "marshaller.h"
@ -89,6 +90,22 @@ static jsonValue_t* json_marshall_bool(void* value) {
return json_bool(*((bool*) value));
}
jsonValue_t* _json_marshall_array_value(const char* type, void* value) {
size_t size;
for (size = 0; *(((void**) value) + size) != NULL; size++);
jsonValue_t** array = alloca(size * sizeof(jsonValue_t*));
for(size_t i = 0; i < size; i++) {
array[i] = _json_marshall_value(type, *(((void**) value) + i));
if (array[i] == NULL) {
return NULL;
}
}
return json_array_direct(true, size, array);
}
jsonValue_t* _json_marshall_value(const char* type, void* value) {
if (value == NULL) {
return json_null();
@ -129,6 +146,17 @@ char* _json_marshall(const char* type, void* value) {
return result;
}
char* _json_marshall_array(const char* type, void* value) {
jsonValue_t* json = _json_marshall_array_value(type, value);
if (json == NULL)
return NULL;
char* result = json_stringify(json);
json_free(json);
return result;
}
static void* json_unmarshall_char(jsonValue_t* value) {
if (value->type != JSON_LONG)
return NULL;

View file

@ -4,7 +4,9 @@
#include <stdbool.h>
jsonValue_t* _json_marshall_value(const char* type, void* value);
jsonValue_t* _json_marshall_array_value(const char* type, void* value);
char* _json_marshall(const char* type, void* value);
char* _json_marshall_array(const char* type, void* value);
void* _json_unmarshall_value(const char* type, jsonValue_t* value);
void* _json_unmarshall(const char* type, const char* json);
@ -12,6 +14,7 @@ void* _json_unmarshall(const char* type, const char* json);
void _json_free_struct(const char* type, void* value, bool this);
#define json_marshall(t, v) _json_marshall(# t, (void*) v)
#define json_marshall_array(t, v) _json_marshall_array(# t, (void*) v)
#define json_unmarshall(t, j) (t*) _json_unmarshall(#t, j)
#define json_free_struct(t, v) _json_free_struct(#t, v, true)