2023-08-28 12:31:18 +02:00
|
|
|
/* A simple buffer for encoding streamed JSON messages.
|
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-10-27 20:23:47 +01:00
|
|
|
#include <villas/buffer.hpp>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <villas/compat.hpp>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
using namespace villas;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
json_t *Buffer::decode() {
|
|
|
|
json_t *j;
|
|
|
|
json_error_t err;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
j = json_loadb(data(), size(), JSON_DISABLE_EOF_CHECK, &err);
|
|
|
|
if (!j)
|
|
|
|
return nullptr;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Remove decoded JSON document from beginning
|
|
|
|
erase(begin(), begin() + err.position);
|
2020-10-20 22:13:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
return j;
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
int Buffer::encode(json_t *j, int flags) {
|
|
|
|
return json_dump_callback(j, callback, this, flags);
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
int Buffer::callback(const char *data, size_t len, void *ctx) {
|
|
|
|
Buffer *b = static_cast<Buffer *>(ctx);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Append junk of JSON to buffer
|
|
|
|
b->insert(b->end(), &data[0], &data[len]);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
return 0;
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|