2023-08-28 12:31:18 +02:00
|
|
|
/* Compatability for different library versions.
|
2018-08-22 11:29:39 +02:00
|
|
|
*
|
2023-08-31 11:17:07 +02:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2019-06-23 16:26:44 +02:00
|
|
|
#include <cstring>
|
2018-08-22 11:29:39 +02:00
|
|
|
#include <jansson.h>
|
2019-02-09 20:09:20 +00:00
|
|
|
#include <unistd.h>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
#include <villas/compat.hpp>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
#if JANSSON_VERSION_HEX < 0x020A00
|
2023-09-07 13:19:19 +02:00
|
|
|
size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags) {
|
|
|
|
char *str;
|
|
|
|
size_t len;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
str = json_dumps(json, flags);
|
|
|
|
if (!str)
|
|
|
|
return 0;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
len = strlen(str); // Not \0 terminated
|
|
|
|
if (buffer && len <= size)
|
|
|
|
memcpy(buffer, str, len);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
free(str);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
return len;
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
2019-02-09 20:09:20 +00:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
static size_t json_loadfd_callback(void *buffer, size_t buflen, void *data) {
|
|
|
|
int *fd = (int *)data;
|
2019-02-09 20:09:20 +00:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
return (size_t)read(*fd, buffer, buflen);
|
2019-02-09 20:09:20 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
json_t *json_loadfd(int input, size_t flags, json_error_t *error) {
|
|
|
|
return json_load_callback(json_loadfd_callback, (void *)&input, flags, error);
|
2019-02-09 20:09:20 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
static int json_dumpfd_callback(const char *buffer, size_t size, void *data) {
|
2019-02-09 20:09:20 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2023-09-07 13:19:19 +02:00
|
|
|
int *dest = (int *)data;
|
2019-02-09 20:09:20 +00:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
if (write(*dest, buffer, size) == (ssize_t)size)
|
|
|
|
return 0;
|
2019-04-08 10:35:52 +01:00
|
|
|
#else
|
2023-09-07 13:19:19 +02:00
|
|
|
(void)buffer;
|
|
|
|
(void)size;
|
|
|
|
(void)data;
|
2022-12-02 17:16:44 +01:00
|
|
|
#endif // HAVE_UNISTD_H
|
2019-02-09 20:09:20 +00:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
return -1;
|
2019-02-09 20:09:20 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
int json_dumpfd(const json_t *json, int output, size_t flags) {
|
|
|
|
return json_dump_callback(json, json_dumpfd_callback, (void *)&output, flags);
|
2019-02-09 20:09:20 +00:00
|
|
|
}
|
2022-12-02 17:16:44 +01:00
|
|
|
#endif // JANSSON_VERSION_HEX < 0x020A00
|