Skip to content

Commit 0374b54

Browse files
committed
common: Use jansson support header
Signed-off-by: Philipp Jungkamp <philipp.jungkamp@rwth-aachen.de>
1 parent 965d819 commit 0374b54

5 files changed

Lines changed: 49 additions & 46 deletions

File tree

common/lib/hist.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <villas/config.hpp>
1212
#include <villas/exceptions.hpp>
1313
#include <villas/hist.hpp>
14+
#include <villas/jansson.hpp>
1415
#include <villas/table.hpp>
1516
#include <villas/utils.hpp>
1617

@@ -203,15 +204,24 @@ char *Hist::dump() const {
203204
json_t *Hist::toJson() const {
204205
json_t *json_buckets, *json_hist;
205206

206-
json_hist = json_pack("{ s: f, s: f, s: i }", "low", low, "high", high,
207-
"total", total);
207+
json_hist = janssonPack("{ s: f, s: f, s: I }", //
208+
"low", low, //
209+
"high", high, //
210+
"total", static_cast<json_int_t>(total))
211+
.release();
208212

209213
if (total > 0) {
210-
json_object_update(json_hist,
211-
json_pack("{ s: i, s: i, s: f, s: f, s: f, s: f, s: f }",
212-
"higher", higher, "lower", lower, "highest",
213-
highest, "lowest", lowest, "mean", getMean(),
214-
"variance", getVar(), "stddev", getStddev()));
214+
json_object_update_new(
215+
json_hist,
216+
janssonPack("{ s: I, s: I, s: f, s: f, s: f, s: f, s: f }", //
217+
"higher", static_cast<json_int_t>(higher), //
218+
"lower", static_cast<json_int_t>(lower), //
219+
"highest", highest, //
220+
"lowest", lowest, //
221+
"mean", getMean(), //
222+
"variance", getVar(), //
223+
"stddev", getStddev())
224+
.release());
215225
}
216226

217227
if (total - lower - higher > 0) {
@@ -220,7 +230,7 @@ json_t *Hist::toJson() const {
220230
for (auto elm : data)
221231
json_array_append(json_buckets, json_integer(elm));
222232

223-
json_object_set(json_hist, "buckets", json_buckets);
233+
json_object_set_new(json_hist, "buckets", json_buckets);
224234
}
225235

226236
return json_hist;

common/lib/log.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
*/
77

88
#include <algorithm>
9-
#include <list>
109
#include <unordered_map>
1110

1211
#include <fnmatch.h>
1312
#include <spdlog/sinks/basic_file_sink.h>
1413
#include <spdlog/sinks/syslog_sink.h>
1514

1615
#include <villas/exceptions.hpp>
16+
#include <villas/jansson.hpp>
1717
#include <villas/log.hpp>
1818
#include <villas/terminal.hpp>
1919

@@ -100,17 +100,15 @@ void Log::parse(json_t *json) {
100100
const char *path = nullptr;
101101
const char *pattern = nullptr;
102102

103-
int ret, syslog = 0;
104-
105-
json_error_t err;
103+
int syslog = 0;
106104
json_t *json_expressions = nullptr;
107105

108-
ret = json_unpack_ex(json, &err, JSON_STRICT,
109-
"{ s?: s, s?: s, s?: o, s?: b, s?: s }", "level", &level,
110-
"file", &path, "expressions", &json_expressions,
111-
"syslog", &syslog, "pattern", &pattern);
112-
if (ret)
113-
throw ConfigError(json, err, "node-config-logging");
106+
janssonUnpack(json, "{ s?: s, s?: s, s?: o, s?: b, s?: s }", //
107+
"level", &level, //
108+
"file", &path, //
109+
"expressions", &json_expressions, //
110+
"syslog", &syslog, //
111+
"pattern", &pattern);
114112

115113
if (level)
116114
setLevel(level);
@@ -179,22 +177,16 @@ Log::Level Log::getLevel() const { return level; }
179177
std::string Log::getLevelName() const {
180178
auto sv = spdlog::level::to_string_view(level);
181179

182-
return std::string(sv.data());
180+
return std::string(sv.begin(), sv.end());
183181
}
184182

185183
Log::Expression::Expression(json_t *json) {
186-
int ret;
187-
188184
const char *nme;
189185
const char *lvl;
190186

191-
json_error_t err;
192-
193-
ret = json_unpack_ex(json, &err, JSON_STRICT, "{ s: s, s: s }", "name", &nme,
194-
"level", &lvl);
195-
if (ret)
196-
throw ConfigError(json, err, "node-config-logging-expressions");
197-
187+
janssonUnpack(json, "{ s: s, s: s }", //
188+
"name", &nme, //
189+
"level", &lvl);
198190
level = spdlog::level::from_str(lvl);
199191
name = nme;
200192
}

common/tests/unit/buffer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
#include <ctime>
1010

1111
#include <criterion/criterion.h>
12-
#include <jansson.h>
1312

1413
#include <villas/buffer.hpp>
14+
#include <villas/jansson.hpp>
1515

1616
using namespace villas;
1717

@@ -95,7 +95,7 @@ Test(buffer, multiple) {
9595
std::srand(std::time(nullptr));
9696

9797
for (int i = 0; i < N; i++) {
98-
k[i] = json_pack("{ s: i }", "id", std::rand());
98+
k[i] = janssonPack("{ s: i }", "id", std::rand()).release();
9999
cr_assert_not_null(k[i]);
100100

101101
ret = buf.encode(k[i]);

lib/node_direction.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ int NodeDirection::parse(json_t *json) {
3030

3131
config = json;
3232

33-
janssonUnpack(json, "{ s?o, s?o, s?i, s?b, s?b }", //
34-
"hooks", &json_hooks, //
35-
"signals", &json_signals, //
36-
"vectorize", &vectorize, //
37-
"builtin", &builtin, //
33+
janssonUnpack(json, "{ s?: o, s?: o, s?: i, s?: b, s?: b }", //
34+
"hooks", &json_hooks, //
35+
"signals", &json_signals, //
36+
"vectorize", &vectorize, //
37+
"builtin", &builtin, //
3838
"enabled", &enabled);
3939

4040
if (node->getFactory()->getFlags() &
@@ -52,7 +52,7 @@ int NodeDirection::parse(json_t *json) {
5252
json_t *json_name, *json_signal = json_signals;
5353
int count;
5454

55-
janssonUnpack(json_signal, "{ s:i }", "count", &count);
55+
janssonUnpack(json_signal, "{ s: i }", "count", &count);
5656

5757
json_signals = json_array();
5858
for (int i = 0; i < count; i++) {

lib/nodes/webrtc.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,17 @@ int WebRTCNode::parse(json_t *json) {
6363
json_t *json_format = nullptr;
6464

6565
janssonUnpack(json,
66-
"{ s:s, s?s, s?s, s?i, s?i, s?b, s?{ s?o, s?b }, s?o }", //
67-
"session", &sess, //
68-
"peer", &pr, //
69-
"server", &svr, //
70-
"wait_seconds", &wait_seconds, //
71-
"max_retransmits", &rexmit, //
72-
"ordered", &ord, //
73-
"ice", //
74-
/* ice */ "servers", &json_servers, //
75-
/* ice */ "tcp", &tcp, //
66+
"{ s: s, s?: s, s?: s, s?: i, s?: i, s?: b, s?: { s?: o, s?: b "
67+
"}, s?: o }", //
68+
"session", &sess, //
69+
"peer", &pr, //
70+
"server", &svr, //
71+
"wait_seconds", &wait_seconds, //
72+
"max_retransmits", &rexmit, //
73+
"ordered", &ord, //
74+
"ice", //
75+
/* ice */ "servers", &json_servers, //
76+
/* ice */ "tcp", &tcp, //
7677
"format", &json_format);
7778

7879
session = sess;

0 commit comments

Comments
 (0)