mirror of
https://github.com/sigmasternchen/CShore
synced 2025-03-15 08:08:56 +00:00
add json response
This commit is contained in:
parent
5dccbcd798
commit
0bb7de5fa2
2 changed files with 29 additions and 0 deletions
|
@ -11,6 +11,7 @@
|
|||
#include <status.h>
|
||||
|
||||
#include <json.h>
|
||||
#include <marshaller.h>
|
||||
|
||||
#include "request.h"
|
||||
|
||||
|
@ -167,3 +168,28 @@ response_t fileResponse(const char* file) {
|
|||
|
||||
return response;
|
||||
}
|
||||
|
||||
static void jsonOutput(FILE* output, void* _userData, ctx_t ctx) {
|
||||
jsonValue_t* json = (jsonValue_t*) _userData;
|
||||
|
||||
char* result = json_stringify(json);
|
||||
json_free(json);
|
||||
|
||||
fprintf(output, "%s", result);
|
||||
free(result);
|
||||
}
|
||||
|
||||
response_t _jsonResponse(int status, const char* type, void* value) {
|
||||
response_t response = emptyResponse();
|
||||
|
||||
jsonValue_t* json = _json_marshall_value(type, value);
|
||||
if (json == NULL) {
|
||||
return statusResponse(500, strerror(errno));
|
||||
}
|
||||
|
||||
response.status = status;
|
||||
response._userData = json;
|
||||
response.output = jsonOutput;
|
||||
|
||||
return response;
|
||||
}
|
||||
|
|
|
@ -40,4 +40,7 @@ response_t rawResponse(int status, const char* txt);
|
|||
|
||||
response_t fileResponse(const char* file);
|
||||
|
||||
response_t _jsonResponse(int status, const char* type, void* value);
|
||||
#define jsonResponse(s, t, v) _jsonResponse(s, # t, v)
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue