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
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-09-11 16:01:16 +02:00
|
|
|
#include <vector>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2020-10-20 22:13:35 +02:00
|
|
|
#include <cstdlib>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2020-10-20 22:13:35 +02:00
|
|
|
#include <jansson.h>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
namespace villas {
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2020-10-20 22:13:35 +02:00
|
|
|
class Buffer : public std::vector<char> {
|
2019-10-27 20:23:47 +01:00
|
|
|
|
2020-10-20 22:13:35 +02:00
|
|
|
protected:
|
2023-09-07 13:19:19 +02:00
|
|
|
static int callback(const char *data, size_t len, void *ctx);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2020-10-20 22:13:35 +02:00
|
|
|
public:
|
2023-09-07 13:19:19 +02:00
|
|
|
Buffer(const char *buf, size_type len) : std::vector<char>(buf, buf + len) {}
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Buffer(size_type count = 0) : std::vector<char>(count, 0) {}
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Encode JSON document /p j and append it to the buffer
|
|
|
|
int encode(json_t *j, int flags = 0);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Decode JSON document from the beginning of the buffer
|
|
|
|
json_t *decode();
|
2019-10-27 20:23:47 +01:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void append(const char *data, size_t len) { insert(end(), data, data + len); }
|
2019-10-27 20:23:47 +01:00
|
|
|
};
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
} // namespace villas
|