1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/formats/json.c

310 lines
6.4 KiB
C
Raw Permalink Normal View History

2017-07-28 18:11:52 +02:00
/** JSON serializtion of sample data.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
2017-04-27 12:56:43 +02:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
2017-04-27 12:56:43 +02:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2017-04-27 12:56:43 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
2017-12-09 02:19:28 +08:00
#include <villas/plugin.h>
#include <villas/sample.h>
#include <villas/compat.h>
2018-05-12 15:25:47 +02:00
#include <villas/io.h>
2018-05-12 13:56:12 +02:00
#include <villas/formats/json.h>
static json_t * json_pack_timestamps(struct sample *smp)
{
json_t *json_ts = json_object();
if (smp->flags & SAMPLE_HAS_ORIGIN)
json_object_set(json_ts, "origin", json_pack("[ I, I ]", smp->ts.origin.tv_sec, smp->ts.origin.tv_nsec));
if (smp->flags & SAMPLE_HAS_RECEIVED)
json_object_set(json_ts, "received", json_pack("[ I, I ]", smp->ts.received.tv_sec, smp->ts.received.tv_nsec));
return json_ts;
}
static int json_unpack_timestamps(json_t *json_ts, struct sample *smp)
{
int ret;
json_error_t err;
json_t *json_ts_origin = NULL, *json_ts_received = NULL;
json_unpack_ex(json_ts, &err, 0, "{ s?: o, s?: o }",
"origin", &json_ts_origin,
"received", &json_ts_received
);
if (json_ts_origin) {
ret = json_unpack_ex(json_ts_origin, &err, 0, "[ I, I ]", &smp->ts.origin.tv_sec, &smp->ts.origin.tv_nsec);
if (ret)
return ret;
smp->flags |= SAMPLE_HAS_ORIGIN;
}
if (json_ts_received) {
ret = json_unpack_ex(json_ts_received, &err, 0, "[ I, I ]", &smp->ts.received.tv_sec, &smp->ts.received.tv_nsec);
if (ret)
return ret;
smp->flags |= SAMPLE_HAS_RECEIVED;
}
return 0;
}
2018-05-12 18:11:39 +02:00
static int json_pack_sample(struct io *io, json_t **j, struct sample *smp)
{
2017-08-22 12:21:17 +02:00
json_t *json_smp;
json_error_t err;
json_smp = json_pack_ex(&err, 0, "{ s: o }", "ts", json_pack_timestamps(smp));
2018-05-12 18:01:48 +02:00
if (smp->flags & SAMPLE_HAS_SEQUENCE) {
2017-08-22 12:21:17 +02:00
json_t *json_sequence = json_integer(smp->sequence);
2017-08-22 12:21:17 +02:00
json_object_set(json_smp, "sequence", json_sequence);
}
2018-05-12 18:01:48 +02:00
if (smp->flags & SAMPLE_HAS_VALUES) {
2017-08-22 12:21:17 +02:00
json_t *json_data = json_array();
2017-08-22 12:21:17 +02:00
for (int i = 0; i < smp->length; i++) {
json_t *json_value = sample_get_data_format(smp, i)
? json_integer(smp->data[i].i)
: json_real(smp->data[i].f);
2017-08-22 12:21:17 +02:00
json_array_append(json_data, json_value);
}
2017-08-22 12:21:17 +02:00
json_object_set(json_smp, "data", json_data);
}
2017-08-22 12:21:17 +02:00
*j = json_smp;
return 0;
}
2018-05-12 18:11:39 +02:00
static int json_pack_samples(struct io *io, json_t **j, struct sample *smps[], unsigned cnt)
{
2017-08-22 12:21:17 +02:00
int ret;
json_t *json_smps = json_array();
2017-08-22 12:21:17 +02:00
for (int i = 0; i < cnt; i++) {
json_t *json_smp;
2018-05-12 18:01:48 +02:00
ret = json_pack_sample(io, &json_smp, smps[i]);
2017-08-22 12:21:17 +02:00
if (ret)
break;
2017-08-22 12:21:17 +02:00
json_array_append(json_smps, json_smp);
}
2017-08-22 12:21:17 +02:00
*j = json_smps;
2017-08-22 12:21:17 +02:00
return cnt;
}
2018-05-12 18:11:39 +02:00
static int json_unpack_sample(struct io *io, json_t *json_smp, struct sample *smp)
2017-08-22 12:21:17 +02:00
{
int ret;
json_error_t err;
json_t *json_data, *json_value, *json_ts = NULL;
2017-08-22 12:21:17 +02:00
size_t i;
int64_t sequence = -1;
ret = json_unpack_ex(json_smp, &err, 0, "{ s?: o, s?: I, s: o }",
"ts", &json_ts,
"sequence", &sequence,
"data", &json_data);
if (ret)
return ret;
smp->flags = 0;
smp->length = 0;
if (json_ts) {
ret = json_unpack_timestamps(json_ts, smp);
if (ret)
return ret;
}
2017-08-22 12:21:17 +02:00
if (!json_is_array(json_data))
return -1;
if (sequence >= 0) {
smp->sequence = sequence;
smp->flags |= SAMPLE_HAS_SEQUENCE;
}
json_array_foreach(json_data, i, json_value) {
2017-08-22 12:21:17 +02:00
if (i >= smp->capacity)
break;
switch (json_typeof(json_value)) {
case JSON_REAL:
2017-08-22 12:21:17 +02:00
smp->data[i].f = json_real_value(json_value);
sample_set_data_format(smp, i, SAMPLE_DATA_FORMAT_FLOAT);
break;
case JSON_INTEGER:
2017-08-22 12:21:17 +02:00
smp->data[i].f = json_integer_value(json_value);
sample_set_data_format(smp, i, SAMPLE_DATA_FORMAT_INT);
break;
default:
2017-08-22 12:21:17 +02:00
return -2;
}
2017-08-22 12:21:17 +02:00
smp->length++;
}
if (smp->length > 0)
smp->flags |= SAMPLE_HAS_VALUES;
return 0;
}
2018-05-12 18:11:39 +02:00
static int json_unpack_samples(struct io *io, json_t *json_smps, struct sample *smps[], unsigned cnt)
2017-08-22 12:21:17 +02:00
{
int ret;
json_t *json_smp;
size_t i;
2017-08-22 12:21:17 +02:00
if (!json_is_array(json_smps))
return -1;
2017-08-22 12:21:17 +02:00
json_array_foreach(json_smps, i, json_smp) {
if (i >= cnt)
break;
2018-05-12 18:01:48 +02:00
ret = json_unpack_sample(io, json_smp, smps[i]);
2017-08-22 12:21:17 +02:00
if (ret < 0)
break;
}
return i;
}
2018-05-12 18:01:48 +02:00
int json_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct sample *smps[], unsigned cnt)
{
2017-08-22 12:21:17 +02:00
int ret;
json_t *json;
2017-08-22 12:21:17 +02:00
size_t wr;
2018-05-12 18:01:48 +02:00
ret = json_pack_samples(io, &json, smps, cnt);
2017-08-22 12:21:17 +02:00
if (ret < 0)
return ret;
2017-08-05 21:02:09 +02:00
2017-08-22 12:21:17 +02:00
wr = json_dumpb(json, buf, len, 0);
2017-08-22 12:21:17 +02:00
json_decref(json);
2017-08-22 12:21:17 +02:00
if (wbytes)
*wbytes = wr;
2017-08-05 21:02:09 +02:00
2017-08-22 12:21:17 +02:00
return ret;
}
2018-05-12 18:01:48 +02:00
int json_sscan(struct io *io, char *buf, size_t len, size_t *rbytes, struct sample *smps[], unsigned cnt)
{
2017-08-22 12:21:17 +02:00
int ret;
json_t *json;
json_error_t err;
2017-08-22 12:21:17 +02:00
json = json_loadb(buf, len, 0, &err);
if (!json)
return -1;
2018-05-12 18:01:48 +02:00
ret = json_unpack_samples(io, json, smps, cnt);
2017-08-05 21:02:09 +02:00
2017-08-22 12:21:17 +02:00
json_decref(json);
if (ret < 0)
return ret;
2017-08-22 12:21:17 +02:00
if (rbytes)
*rbytes = err.position;
2017-08-05 21:02:09 +02:00
2017-08-22 12:21:17 +02:00
return ret;
}
2017-07-28 18:11:52 +02:00
2018-05-12 15:25:47 +02:00
int json_print(struct io *io, struct sample *smps[], unsigned cnt)
2017-07-28 18:11:52 +02:00
{
2017-08-05 21:02:09 +02:00
int ret, i;
json_t *json;
2018-05-12 15:25:47 +02:00
FILE *f = io_stream_output(io);
2017-08-05 21:02:09 +02:00
for (i = 0; i < cnt; i++) {
2018-05-12 18:01:48 +02:00
ret = json_pack_sample(io, &json, smps[i]);
2017-08-05 21:02:09 +02:00
if (ret)
return ret;
ret = json_dumpf(json, f, 0);
fputc(io->delimiter, f);
2017-08-05 21:02:09 +02:00
json_decref(json);
2018-05-12 18:02:37 +02:00
if (ret)
return ret;
2017-08-05 21:02:09 +02:00
}
return i;
2017-07-28 18:11:52 +02:00
}
2018-05-12 15:25:47 +02:00
int json_scan(struct io *io, struct sample *smps[], unsigned cnt)
2017-07-28 18:11:52 +02:00
{
2017-08-05 21:02:09 +02:00
int i, ret;
json_t *json;
json_error_t err;
2018-05-12 15:25:47 +02:00
FILE *f = io_stream_input(io);
2017-08-05 21:02:09 +02:00
for (i = 0; i < cnt; i++) {
skip: json = json_loadf(f, JSON_DISABLE_EOF_CHECK, &err);
if (!json)
break;
2018-05-12 18:01:48 +02:00
ret = json_unpack_sample(io, json, smps[i]);
2017-08-05 21:02:09 +02:00
if (ret)
goto skip;
json_decref(json);
}
return i;
2017-07-28 18:11:52 +02:00
}
static struct plugin p = {
.name = "json",
.description = "Javascript Object Notation",
2018-05-12 13:56:12 +02:00
.type = PLUGIN_TYPE_FORMAT,
.format = {
2018-05-12 15:25:47 +02:00
.scan = json_scan,
.print = json_print,
.sscan = json_sscan,
.sprint = json_sprint,
.size = 0,
.delimiter = '\n'
2017-07-28 18:11:52 +02:00
},
};
REGISTER_PLUGIN(&p);