mirror of
https://github.com/sigmasternchen/libargo
synced 2025-03-15 21:28:54 +00:00
marshalling dynamic arrays works now
This commit is contained in:
parent
c89a02ae6a
commit
99a539f243
7 changed files with 71 additions and 5 deletions
|
@ -5,6 +5,7 @@
|
||||||
typedef struct {
|
typedef struct {
|
||||||
long long uid;
|
long long uid;
|
||||||
char* username;
|
char* username;
|
||||||
|
char** aliases;
|
||||||
char* email;
|
char* email;
|
||||||
} user_t;
|
} user_t;
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <alloca.h>
|
||||||
|
|
||||||
#include <json.h>
|
#include <json.h>
|
||||||
#include <marshaller.h>
|
#include <marshaller.h>
|
||||||
|
@ -15,6 +16,7 @@ int main() {
|
||||||
\"user\": {\
|
\"user\": {\
|
||||||
\"uid\": 1000,\
|
\"uid\": 1000,\
|
||||||
\"username\": \"overflowerror\",\
|
\"username\": \"overflowerror\",\
|
||||||
|
\"aliases\": null,\
|
||||||
\"email\": \"overflow@persei.net\"\
|
\"email\": \"overflow@persei.net\"\
|
||||||
}\
|
}\
|
||||||
}";
|
}";
|
||||||
|
@ -32,12 +34,19 @@ int main() {
|
||||||
free(post->content);
|
free(post->content);
|
||||||
post->content = "Just do it.";
|
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);
|
char* newJson = json_marshall(post_t, post);
|
||||||
printf("%s\n", newJson);
|
printf("%s\n", newJson);
|
||||||
free(newJson);
|
free(newJson);
|
||||||
|
|
||||||
// set to NULL so it doesn't get freed
|
// set to NULL so it doesn't get freed
|
||||||
post->content = NULL;
|
post->content = NULL;
|
||||||
|
post->user.aliases = NULL;
|
||||||
json_free_struct(post_t, post);
|
json_free_struct(post_t, post);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -76,13 +76,19 @@ char* generateMarshallFunction(FILE* output, struct structinfo* info, char* suff
|
||||||
|
|
||||||
fprintf(output, "\t\t\"%s\", ", member->name);
|
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) {
|
if (member->type->isArray) {
|
||||||
reference = "&";
|
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");
|
fprintf(output, "\t);\n");
|
||||||
|
|
17
src/base.c
17
src/base.c
|
@ -102,6 +102,23 @@ jsonValue_t* json_array(bool freeAfterwards, size_t size, ...) {
|
||||||
|
|
||||||
return value;
|
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* json_object(bool freeAfterwards, size_t size, ...) {
|
||||||
jsonValue_t* value = json_value();
|
jsonValue_t* value = json_value();
|
||||||
value->type = JSON_OBJECT;
|
value->type = JSON_OBJECT;
|
||||||
|
|
|
@ -52,6 +52,8 @@ jsonValue_t* json_string(const char* s);
|
||||||
jsonValue_t* json_array(bool freeAfterwards, size_t size, ...);
|
jsonValue_t* json_array(bool freeAfterwards, size_t size, ...);
|
||||||
jsonValue_t* json_object(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);
|
void json_print(jsonValue_t* value);
|
||||||
|
|
||||||
jsonValue_t* json_clone(jsonValue_t* value);
|
jsonValue_t* json_clone(jsonValue_t* value);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <alloca.h>
|
||||||
|
|
||||||
#include "json.h"
|
#include "json.h"
|
||||||
#include "marshaller.h"
|
#include "marshaller.h"
|
||||||
|
@ -89,6 +90,22 @@ static jsonValue_t* json_marshall_bool(void* value) {
|
||||||
return json_bool(*((bool*) 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) {
|
jsonValue_t* _json_marshall_value(const char* type, void* value) {
|
||||||
if (value == NULL) {
|
if (value == NULL) {
|
||||||
return json_null();
|
return json_null();
|
||||||
|
@ -129,6 +146,17 @@ char* _json_marshall(const char* type, void* value) {
|
||||||
return result;
|
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) {
|
static void* json_unmarshall_char(jsonValue_t* value) {
|
||||||
if (value->type != JSON_LONG)
|
if (value->type != JSON_LONG)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
@ -4,7 +4,9 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
jsonValue_t* _json_marshall_value(const char* type, void* value);
|
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(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_value(const char* type, jsonValue_t* value);
|
||||||
void* _json_unmarshall(const char* type, const char* json);
|
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);
|
void _json_free_struct(const char* type, void* value, bool this);
|
||||||
|
|
||||||
#define json_marshall(t, v) _json_marshall(# t, (void*) v)
|
#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_unmarshall(t, j) (t*) _json_unmarshall(#t, j)
|
||||||
|
|
||||||
#define json_free_struct(t, v) _json_free_struct(#t, v, true)
|
#define json_free_struct(t, v) _json_free_struct(#t, v, true)
|
||||||
|
|
Loading…
Reference in a new issue