Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
JSONInterface.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Carsten D. Burgard, DESY/ATLAS, Dec 2021
5 *
6 * Copyright (c) 2022, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
14
15#include "JSONReadRecorder.h"
16
17#include <nlohmann/json.hpp>
18
19#include <algorithm>
20#include <deque>
21#include <istream>
22#include <iterator>
23#include <memory>
24#include <sstream>
25
26// This is the only translation unit where the JSON parsing library is used.
27// Hence, the JSON engine backing the RooFit::Detail::JSONTree could in
28// principle be swapped out by changing only this file.
29
30namespace RooFit {
31namespace Detail {
32
33namespace {
34
35nlohmann::json &json(void *value)
36{
37 return *static_cast<nlohmann::json *>(value);
38}
39
40// Receives the events of a parse that only looks for the syntax error, see parse().
41class SyntaxErrorRecorder : public nlohmann::json_sax<nlohmann::json> {
42public:
43 bool null() override { return true; }
44 bool boolean(bool) override { return true; }
45 bool number_integer(number_integer_t) override { return true; }
46 bool number_unsigned(number_unsigned_t) override { return true; }
47 bool number_float(number_float_t, const string_t &) override { return true; }
48 bool string(string_t &) override { return true; }
49 bool binary(binary_t &) override { return true; }
50 bool start_object(std::size_t) override { return true; }
51 bool key(string_t &) override { return true; }
52 bool end_object() override { return true; }
53 bool start_array(std::size_t) override { return true; }
54 bool end_array() override { return true; }
55 bool parse_error(std::size_t, const std::string &, const nlohmann::json::exception &ex) override
56 {
57 message = ex.what();
58 return false;
59 }
60
61 std::string message;
62};
63
64// Parse without exceptions of the JSON library. For invalid input, the input is parsed again to get the message.
65nlohmann::json parse(std::string const &input)
66{
67 nlohmann::json out = nlohmann::json::parse(input, nullptr, /*allow_exceptions=*/false);
68 if (out.is_discarded()) {
69 SyntaxErrorRecorder recorder;
70 nlohmann::json::sax_parse(input, &recorder);
71 throw std::runtime_error(recorder.message);
72 }
73 return out;
74}
75
76// Reading a value with the wrong type throws an std::runtime_error, not an exception of the JSON library.
77[[noreturn]] void throwTypeError(std::string const &key, const char *expected, nlohmann::json const &value)
78{
79 throw std::runtime_error("node \"" + key + "\": expected " + expected + ", but the value is of type " +
80 value.type_name());
81}
82
83// To check whether it's allowed to reset the type of an object. We allow
84// this for nodes that have no type yet, or nodes with an empty string.
85bool isResettingPossible(nlohmann::json const &node)
86{
87
88 if (node.type() == nlohmann::json::value_t::null) {
89 return true;
90 }
91
92 if (node.type() == nlohmann::json::value_t::string) {
93 if (node.get<std::string>().empty()) {
94 return true;
95 }
96 }
97 return false;
98}
99
100} // namespace
101
102// The nodes live as long as the tree, because the interface hands out references to them.
104public:
105 // Not root{value}, which would be an array that contains the value.
106 explicit Storage(nlohmann::json value) : root(std::move(value)), rootNode{*this, &root, ""} {}
107
108 JSONNode &makeNode(nlohmann::json &value, std::string key)
109 {
110 nodes.push_back(std::unique_ptr<JSONNode>{new JSONNode{*this, &value, std::move(key)}});
111 return *nodes.back();
112 }
113
114 // The children are created up front, such that the iterators can be pointers into this list.
115 std::vector<JSONNode *> const &makeChildren(nlohmann::json &value)
116 {
117 childLists.emplace_back();
118 std::vector<JSONNode *> &list = childLists.back();
119 if (!value.is_structured()) {
120 return list;
121 }
122 list.reserve(value.size());
123 for (auto it = value.begin(); it != value.end(); ++it) {
124 list.push_back(&makeNode(it.value(), value.is_array() ? "" : it.key()));
125 }
126 return list;
127 }
128
129 // Tells the recorders that the component `key` of `value` is read, where nullptr means all components.
130 void recordRead(void const *value, std::string const *key)
131 {
132 for (JSONReadRecorder *recorder : recorders) {
133 recorder->record(value, key);
134 }
135 }
136
137 nlohmann::json root;
139 std::vector<std::unique_ptr<JSONNode>> nodes;
140 std::deque<std::vector<JSONNode *>> childLists;
141 std::vector<JSONReadRecorder *> recorders;
142};
143
144// JSONNode
145
146JSONNode::JSONNode(Storage &storage, void *value, std::string key)
147 : _storage{&storage}, _value{value}, _key{std::move(key)}
148{
149}
150
151void JSONNode::writeJSON(std::ostream &os) const
152{
153 _storage->recordRead(_value, nullptr);
154 os << json(_value);
155}
156
157JSONNode &JSONNode::operator<<(std::string const &s)
158{
159 json(_value) = s;
160 return *this;
161}
162
164{
165 json(_value) = i;
166 return *this;
167}
168
170{
171 json(_value) = d;
172 return *this;
173}
174
176{
177 json(_value) = b;
178 return *this;
179}
180
181const JSONNode &JSONNode::operator>>(std::string &v) const
182{
183 if (!json(_value).is_string()) {
184 throwTypeError(_key, "a string", json(_value));
185 }
186 v = json(_value).get<std::string>();
187 return *this;
188}
189
190JSONNode &JSONNode::operator[](std::string const &k)
191{
192 return _storage->makeNode(json(_value)[k], k);
193}
194
195const JSONNode &JSONNode::operator[](std::string const &k) const
196{
198 return _storage->makeNode(json(_value)[k], k);
199}
200
202{
203 return json(_value).is_array() || json(_value).is_object();
204}
205
207{
208 return json(_value).is_object();
209}
210
212{
213 return json(_value).is_array();
214}
215
217{
218 return json(_value).is_null();
219}
220
222{
223 return json(_value).is_number();
224}
225
227{
228 if (json(_value).type() == nlohmann::json::value_t::object)
229 return *this;
230
231 if (isResettingPossible(json(_value))) {
232 json(_value) = nlohmann::json::object();
233 } else {
234 throw std::runtime_error("cannot declare \"" + this->key() + "\" to be of map - type, already of type " +
235 json(_value).type_name());
236 }
237 return *this;
238}
239
241{
242 if (json(_value).type() == nlohmann::json::value_t::array)
243 return *this;
244
245 if (isResettingPossible(json(_value))) {
246 json(_value) = nlohmann::json::array();
247 } else {
248 throw std::runtime_error("cannot declare \"" + this->key() + "\" to be of seq - type, already of type " +
249 json(_value).type_name());
250 }
251 return *this;
252}
253
255{
256 json(_value) = nullptr;
257 return *this;
258}
259
261{
262 json(_value).clear();
263}
264
265void JSONNode::erase(std::string const &key)
266{
267 json(_value).erase(key);
268}
269
270std::string JSONNode::key() const
271{
272 return _key;
273}
274
275std::string JSONNode::val() const
276{
277 nlohmann::json const &value = json(_value);
278 switch (value.type()) {
279 case nlohmann::json::value_t::string: return value.get<std::string>();
280 case nlohmann::json::value_t::boolean: return value.get<bool>() ? "true" : "false";
281 case nlohmann::json::value_t::number_integer: return std::to_string(value.get<int>());
282 case nlohmann::json::value_t::number_unsigned: return std::to_string(value.get<unsigned int>());
283 case nlohmann::json::value_t::number_float: {
284 std::stringstream ss;
285 ss << value.get<double>();
286 return ss.str();
287 }
288 default:
289 throw std::runtime_error("node \"" + _key + "\": implicit string conversion for type " + value.type_name() +
290 " not supported!");
291 }
292}
293
295{
296 auto const &nd = json(_value);
297 if (!nd.is_number() && !nd.is_boolean()) {
298 throwTypeError(_key, "a number", nd);
299 }
300 return nd.get<int>();
301}
303{
304 auto const &nd = json(_value);
305 if (!nd.is_number() && !nd.is_boolean()) {
306 throwTypeError(_key, "a number", nd);
307 }
308 return nd.get<double>();
309}
311{
312 std::optional<bool> out = try_val_bool();
313 if (!out) {
314 throwTypeError(_key, "a boolean", json(_value));
315 }
316 return *out;
317}
318
319std::optional<bool> JSONNode::try_val_bool() const
320{
321 auto const &nd = json(_value);
322 if (nd.is_boolean()) {
323 return nd.get<bool>();
324 }
325 // Attempting to convert zeroes and ones to bools.
326 if (nd.is_number_unsigned() && nd.get<nlohmann::json::number_unsigned_t>() <= 1) {
327 return nd.get<nlohmann::json::number_unsigned_t>() == 1;
328 }
329 return std::nullopt;
330}
331
333{
334 return !_key.empty();
335}
336
338{
339 return json(_value).is_primitive();
340}
341
342bool JSONNode::has_child(std::string const &c) const
343{
345 return json(_value).find(c) != json(_value).end();
346}
347
349{
350 json(_value).push_back("");
351 return _storage->makeNode(json(_value).back(), "");
352}
353
355{
356 return json(_value).size();
357}
358
360{
361 return _storage->makeNode(json(_value).at(pos), "");
362}
363
364const JSONNode &JSONNode::child(size_t pos) const
365{
366 _storage->recordRead(_value, nullptr);
367 return _storage->makeNode(json(_value).at(pos), "");
368}
369
371{
372 std::vector<JSONNode *> const &list = _storage->makeChildren(json(_value));
373 return {child_iterator{list.data()}, child_iterator{list.data() + list.size()}};
374}
375
377{
378 _storage->recordRead(_value, nullptr);
379 std::vector<JSONNode *> const &list = _storage->makeChildren(json(_value));
380 return {const_child_iterator{list.data()}, const_child_iterator{list.data() + list.size()}};
381}
382
383std::ostream &operator<<(std::ostream &os, JSONNode const &s)
384{
385 s.writeJSON(os);
386 return os;
387}
388
389// JSONTree
390
391JSONTree::JSONTree(std::unique_ptr<JSONNode::Storage> storage) : _storage{std::move(storage)} {}
392
393JSONTree::~JSONTree() = default;
394
396{
397 return _storage->rootNode;
398}
399
400std::unique_ptr<JSONTree> JSONTree::create()
401{
402 return std::unique_ptr<JSONTree>{new JSONTree{std::make_unique<JSONNode::Storage>(nlohmann::json{})}};
403}
404
405std::unique_ptr<JSONTree> JSONTree::create(std::istream &is)
406{
407 return JSONTree::create(std::string{std::istreambuf_iterator<char>{is}, std::istreambuf_iterator<char>{}});
408}
409
410std::unique_ptr<JSONTree> JSONTree::create(std::string const &str)
411{
412 return std::unique_ptr<JSONTree>{new JSONTree{std::make_unique<JSONNode::Storage>(parse(str))}};
413}
414
415// JSONReadRecorder
416
418{
419 _node._storage->recorders.push_back(this);
420}
421
423{
424 auto &recorders = _node._storage->recorders;
425 recorders.erase(std::find(recorders.begin(), recorders.end(), this));
426}
427
428void JSONReadRecorder::record(void const *value, std::string const *key)
429{
430 if (value != _node._value) {
431 return;
432 }
433 if (key) {
434 _read.insert(*key);
435 } else {
436 _readAll = true;
437 }
438}
439
440std::vector<std::string> JSONReadRecorder::unreadComponents() const
441{
442 std::vector<std::string> out;
443 nlohmann::json const &value = json(_node._value);
444 if (!value.is_object() || _readAll) {
445 return out;
446 }
447 for (auto const &item : value.items()) {
448 if (_read.count(item.key()) == 0) {
449 out.push_back(item.key());
450 }
451 }
452 return out;
453}
454
455} // namespace Detail
456} // namespace RooFit
std::string message
nlohmann::json json
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
JSONNode & makeNode(nlohmann::json &value, std::string key)
std::vector< JSONReadRecorder * > recorders
std::vector< JSONNode * > const & makeChildren(nlohmann::json &value)
void recordRead(void const *value, std::string const *key)
std::deque< std::vector< JSONNode * > > childLists
std::vector< std::unique_ptr< JSONNode > > nodes
JSONNode & operator[](std::string const &k)
void erase(std::string const &key)
Remove a child of a map, if it exists.
void writeJSON(std::ostream &os) const
JSONNode & operator<<(std::string const &s)
std::string val() const
JSONNode(JSONNode const &)=delete
const JSONNode & operator>>(std::string &v) const
bool has_child(std::string const &) const
std::string key() const
std::optional< bool > try_val_bool() const
The value as a boolean, or std::nullopt if it is none. Like val_bool(), the integers 0 and 1 are acce...
JSONNode & child(size_t pos)
Records which components of a JSON map are read while it is alive, through any node of the tree.
std::set< std::string > _read
void record(void const *value, std::string const *key)
Called by the tree for every read of the component key of value, where nullptr means all components.
JSONReadRecorder(JSONNode const &node)
std::vector< std::string > unreadComponents() const
The components of the map that were not read.
static std::unique_ptr< JSONTree > create()
JSONTree(std::unique_ptr< JSONNode::Storage > storage)
std::unique_ptr< JSONNode::Storage > _storage
Double_t ex[n]
Definition legend1.C:17
std::ostream & operator<<(std::ostream &os, RooFit::Detail::JSONNode const &s)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73
null_t< F > null()