2025-01-14 14:42:39 +00:00
|
|
|
/* JSON serializtion of sample data.
|
2017-04-15 21:22:19 +02:00
|
|
|
*
|
2025-01-14 14:42:39 +00: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
|
|
|
|
*/
|
2017-04-15 21:22:19 +02:00
|
|
|
|
2020-03-04 13:07:20 +01:00
|
|
|
#include <villas/compat.hpp>
|
2021-02-16 14:15:14 +01:00
|
|
|
#include <villas/exceptions.hpp>
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <villas/formats/json.hpp>
|
|
|
|
#include <villas/sample.hpp>
|
|
|
|
#include <villas/signal.hpp>
|
2021-02-16 14:15:14 +01:00
|
|
|
|
|
|
|
using namespace villas;
|
2021-05-10 00:12:30 +02:00
|
|
|
using namespace villas::node;
|
2017-04-15 20:38:58 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
enum SignalType JsonFormat::detect(const json_t *val) {
|
|
|
|
int type = json_typeof(val);
|
2018-08-20 18:30:24 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
switch (type) {
|
|
|
|
case JSON_REAL:
|
|
|
|
return SignalType::FLOAT;
|
2018-08-20 18:30:24 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case JSON_INTEGER:
|
|
|
|
return SignalType::INTEGER;
|
2018-08-20 18:30:24 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case JSON_TRUE:
|
|
|
|
case JSON_FALSE:
|
|
|
|
return SignalType::BOOLEAN;
|
2018-08-20 18:30:24 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case JSON_OBJECT:
|
|
|
|
return SignalType::COMPLEX; // must be a complex number
|
2018-08-20 18:30:24 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
default:
|
|
|
|
return SignalType::INVALID;
|
|
|
|
}
|
2018-08-20 18:30:24 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_t *JsonFormat::packFlags(const struct Sample *smp) {
|
|
|
|
json_t *json_flags = json_array();
|
|
|
|
|
|
|
|
if (flags & (int)SampleFlags::NEW_SIMULATION) {
|
|
|
|
if (smp->flags & (int)SampleFlags::NEW_SIMULATION)
|
|
|
|
json_array_append_new(json_flags, json_string("new_simulation"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & (int)SampleFlags::NEW_FRAME) {
|
|
|
|
if (smp->flags & (int)SampleFlags::NEW_FRAME)
|
|
|
|
json_array_append_new(json_flags, json_string("new_frame"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_array_size(json_flags) == 0) {
|
|
|
|
json_decref(json_flags);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
int JsonFormat::unpackFlags(json_t *json_flags, struct Sample *smp) {
|
|
|
|
if (!json_flags)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!json_is_array(json_flags))
|
|
|
|
throw RuntimeError{"The JSON object flags member is not an array."};
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
json_t *json_flag;
|
|
|
|
json_array_foreach(json_flags, i, json_flag) {
|
|
|
|
char *flag;
|
|
|
|
json_error_t err;
|
|
|
|
if (auto ret = json_unpack_ex(json_flag, &err, 0, "s", &flag))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!strcmp(flag, "new_frame"))
|
|
|
|
smp->flags |= (int)SampleFlags::NEW_FRAME;
|
|
|
|
else
|
|
|
|
smp->flags &= ~(int)SampleFlags::NEW_FRAME;
|
|
|
|
|
|
|
|
if (!strcmp(flag, "new_simulation"))
|
|
|
|
smp->flags |= (int)SampleFlags::NEW_SIMULATION;
|
|
|
|
else
|
|
|
|
smp->flags &= ~(int)SampleFlags::NEW_SIMULATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_t *JsonFormat::packTimestamps(const struct Sample *smp) {
|
|
|
|
json_t *json_ts = json_object();
|
2018-05-09 09:17:40 +02:00
|
|
|
|
2019-04-17 18:18:40 +02:00
|
|
|
#ifdef __arm__ // TODO: check
|
2025-01-14 14:42:39 +00:00
|
|
|
const char *fmt = "[ i, i ]";
|
2019-04-17 18:18:40 +02:00
|
|
|
#else
|
2025-01-14 14:42:39 +00:00
|
|
|
const char *fmt = "[ I, I ]";
|
2019-04-17 18:18:40 +02:00
|
|
|
#endif
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (flags & (int)SampleFlags::HAS_TS_ORIGIN) {
|
|
|
|
if (smp->flags & (int)SampleFlags::HAS_TS_ORIGIN)
|
|
|
|
json_object_set(
|
|
|
|
json_ts, "origin",
|
|
|
|
json_pack(fmt, smp->ts.origin.tv_sec, smp->ts.origin.tv_nsec));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & (int)SampleFlags::HAS_TS_RECEIVED) {
|
|
|
|
if (smp->flags & (int)SampleFlags::HAS_TS_RECEIVED)
|
|
|
|
json_object_set(
|
|
|
|
json_ts, "received",
|
|
|
|
json_pack(fmt, smp->ts.received.tv_sec, smp->ts.received.tv_nsec));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_object_size(json_ts) == 0) {
|
|
|
|
json_decref(json_ts);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_ts;
|
2018-05-09 09:17:40 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int JsonFormat::unpackTimestamps(json_t *json_ts, struct Sample *smp) {
|
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
|
|
|
json_t *json_ts_origin = nullptr;
|
|
|
|
json_t *json_ts_received = nullptr;
|
2018-05-09 09:17:40 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_unpack_ex(json_ts, &err, 0, "{ s?: o, s?: o }", "origin",
|
|
|
|
&json_ts_origin, "received", &json_ts_received);
|
2018-05-09 09:17:40 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
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;
|
2018-05-09 09:17:40 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
smp->flags |= (int)SampleFlags::HAS_TS_ORIGIN;
|
|
|
|
}
|
2018-05-09 09:17:40 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
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;
|
2018-05-09 09:17:40 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
smp->flags |= (int)SampleFlags::HAS_TS_RECEIVED;
|
|
|
|
}
|
2018-05-09 09:17:40 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return 0;
|
2018-05-09 09:17:40 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int JsonFormat::packSample(json_t **json_smp, const struct Sample *smp) {
|
|
|
|
json_t *json_root;
|
|
|
|
json_error_t err;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_root = json_pack_ex(&err, 0, "{ s: o*, s: o* }", "ts",
|
|
|
|
packTimestamps(smp), "flags", packFlags(smp));
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (flags & (int)SampleFlags::HAS_SEQUENCE) {
|
|
|
|
if (smp->flags & (int)SampleFlags::HAS_SEQUENCE) {
|
|
|
|
json_t *json_sequence = json_integer(smp->sequence);
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_object_set_new(json_root, "sequence", json_sequence);
|
|
|
|
}
|
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (flags & (int)SampleFlags::HAS_DATA) {
|
|
|
|
json_t *json_data = json_array();
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
for (unsigned i = 0; i < smp->length; i++) {
|
|
|
|
auto sig = smp->signals->getByIndex(i);
|
|
|
|
if (!sig)
|
|
|
|
return -1;
|
2020-07-01 17:02:22 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_t *json_value = smp->data[i].toJson(sig->type);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_array_append_new(json_data, json_value);
|
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_object_set_new(json_root, "data", json_data);
|
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
*json_smp = json_root;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return 0;
|
2017-04-15 20:38:58 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int JsonFormat::packSamples(json_t **json_smps,
|
|
|
|
const struct Sample *const smps[], unsigned cnt) {
|
|
|
|
int ret;
|
|
|
|
json_t *json_root = json_array();
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
for (unsigned i = 0; i < cnt; i++) {
|
|
|
|
json_t *json_smp;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = packSample(&json_smp, smps[i]);
|
|
|
|
if (ret)
|
|
|
|
break;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_array_append_new(json_root, json_smp);
|
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
*json_smps = json_root;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return cnt;
|
2017-08-22 12:21:17 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int JsonFormat::unpackSample(json_t *json_smp, struct Sample *smp) {
|
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
|
|
|
json_t *json_data, *json_value, *json_ts = nullptr, *json_flags = nullptr;
|
|
|
|
size_t i;
|
|
|
|
int64_t sequence = -1;
|
|
|
|
|
|
|
|
smp->signals = signals;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = json_unpack_ex(json_smp, &err, 0, "{ s?: o, s?: o, s?: I, s: o }", "ts",
|
|
|
|
&json_ts, "flags", &json_flags, "sequence", &sequence,
|
|
|
|
"data", &json_data);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-08-20 18:30:24 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
smp->flags = 0;
|
|
|
|
smp->length = 0;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (json_ts) {
|
|
|
|
ret = unpackTimestamps(json_ts, smp);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2018-05-09 09:17:40 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = unpackFlags(json_flags, smp);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-05-09 09:17:40 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (!json_is_array(json_data))
|
|
|
|
return -1;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (sequence >= 0) {
|
|
|
|
smp->sequence = sequence;
|
|
|
|
smp->flags |= (int)SampleFlags::HAS_SEQUENCE;
|
|
|
|
}
|
2017-04-15 20:38:58 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_array_foreach(json_data, i, json_value) {
|
|
|
|
if (i >= smp->capacity)
|
|
|
|
break;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
auto sig = smp->signals->getByIndex(i);
|
|
|
|
if (!sig)
|
|
|
|
return -1;
|
2017-04-15 20:38:58 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
enum SignalType fmt = detect(json_value);
|
|
|
|
if (sig->type != fmt)
|
|
|
|
throw RuntimeError("Received invalid data type in JSON payload: Received "
|
|
|
|
"{}, expected {} for signal {} (index {}).",
|
|
|
|
signalTypeToString(fmt), signalTypeToString(sig->type),
|
|
|
|
sig->name, i);
|
2018-08-20 18:30:24 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = smp->data[i].parseJson(sig->type, json_value);
|
|
|
|
if (ret)
|
|
|
|
return -3;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
smp->length++;
|
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (smp->length > 0)
|
|
|
|
smp->flags |= (int)SampleFlags::HAS_DATA;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return 0;
|
2017-04-15 20:38:58 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int JsonFormat::unpackSamples(json_t *json_smps, struct Sample *const smps[],
|
|
|
|
unsigned cnt) {
|
|
|
|
int ret;
|
|
|
|
json_t *json_smp;
|
|
|
|
size_t i;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (!json_is_array(json_smps))
|
|
|
|
return -1;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_array_foreach(json_smps, i, json_smp) {
|
|
|
|
if (i >= cnt)
|
|
|
|
break;
|
2017-08-22 12:21:17 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = unpackSample(json_smp, smps[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
}
|
2017-08-22 12:21:17 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return i;
|
2017-08-22 12:21:17 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int JsonFormat::sprint(char *buf, size_t len, size_t *wbytes,
|
|
|
|
const struct Sample *const smps[], unsigned cnt) {
|
|
|
|
int ret;
|
|
|
|
json_t *json;
|
|
|
|
size_t wr;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = packSamples(&json, smps, cnt);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
wr = json_dumpb(json, buf, len, dump_flags);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_decref(json);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (wbytes)
|
|
|
|
*wbytes = wr;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return ret;
|
2017-04-15 20:38:58 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int JsonFormat::sscan(const char *buf, size_t len, size_t *rbytes,
|
|
|
|
struct Sample *const smps[], unsigned cnt) {
|
|
|
|
int ret;
|
|
|
|
json_t *json;
|
|
|
|
json_error_t err;
|
2017-04-15 20:38:58 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json = json_loadb(buf, len, 0, &err);
|
|
|
|
if (!json)
|
|
|
|
return -1;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = unpackSamples(json, smps, cnt);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_decref(json);
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2018-05-09 09:17:40 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (rbytes)
|
|
|
|
*rbytes = err.position;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return ret;
|
2017-04-15 20:38:58 +02:00
|
|
|
}
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int JsonFormat::print(FILE *f, const struct Sample *const smps[],
|
|
|
|
unsigned cnt) {
|
|
|
|
int ret;
|
|
|
|
unsigned i;
|
|
|
|
json_t *json;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
for (i = 0; i < cnt; i++) {
|
|
|
|
ret = packSample(&json, smps[i]);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = json_dumpf(json, f, dump_flags);
|
|
|
|
fputc('\n', f);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_decref(json);
|
2018-05-12 18:02:37 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return i;
|
2017-07-28 18:11:52 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int JsonFormat::scan(FILE *f, struct Sample *const smps[], unsigned cnt) {
|
|
|
|
int ret;
|
|
|
|
unsigned i;
|
|
|
|
json_t *json;
|
|
|
|
json_error_t err;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
for (i = 0; i < cnt; i++) {
|
|
|
|
if (feof(f))
|
|
|
|
return -1;
|
2018-10-22 13:11:05 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
skip:
|
|
|
|
json = json_loadf(f, JSON_DISABLE_EOF_CHECK, &err);
|
|
|
|
if (!json)
|
|
|
|
break;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = unpackSample(json, smps[i]);
|
|
|
|
if (ret)
|
|
|
|
goto skip;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_decref(json);
|
|
|
|
}
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return i;
|
2017-07-28 18:11:52 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
void JsonFormat::parse(json_t *json) {
|
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int indent = 0;
|
|
|
|
int compact = 0;
|
|
|
|
int ensure_ascii = 0;
|
|
|
|
int escape_slash = 0;
|
|
|
|
int sort_keys = 0;
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: i, s?: b, s?: b, s?: b, s?: b }",
|
|
|
|
"indent", &indent, "compact", &compact, "ensure_ascii",
|
|
|
|
&ensure_ascii, "escape_slash", &escape_slash,
|
|
|
|
"sort_keys", &sort_keys
|
2019-04-09 14:27:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(json, err, "node-config-format-json",
|
|
|
|
"Failed to parse format configuration");
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (indent > JSON_MAX_INDENT)
|
|
|
|
throw ConfigError(json, "node-config-format-json-indent",
|
|
|
|
"The maximum indentation level is {}", JSON_MAX_INDENT);
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
dump_flags = 0;
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (indent)
|
|
|
|
dump_flags |= JSON_INDENT(indent);
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (compact)
|
|
|
|
dump_flags |= JSON_COMPACT;
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (ensure_ascii)
|
|
|
|
dump_flags |= JSON_ENSURE_ASCII;
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (escape_slash)
|
|
|
|
dump_flags |= JSON_ESCAPE_SLASH;
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (sort_keys)
|
|
|
|
dump_flags |= JSON_SORT_KEYS;
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
Format::parse(json);
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (real_precision)
|
|
|
|
dump_flags |= JSON_REAL_PRECISION(real_precision);
|
2019-04-09 14:27:55 +02:00
|
|
|
}
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Register format
|
2021-05-10 00:12:30 +02:00
|
|
|
static char n[] = "json";
|
|
|
|
static char d[] = "Javascript Object Notation";
|
2025-01-14 14:42:39 +00:00
|
|
|
static FormatPlugin<
|
|
|
|
JsonFormat, n, d,
|
|
|
|
(int)SampleFlags::HAS_TS_ORIGIN | (int)SampleFlags::HAS_SEQUENCE |
|
|
|
|
(int)SampleFlags::HAS_DATA | (int)SampleFlags::NEW_FRAME |
|
|
|
|
(int)SampleFlags::NEW_SIMULATION>
|
|
|
|
p;
|