17#include <nlohmann/json.hpp>
37 return *
static_cast<nlohmann::json *
>(
value);
41class SyntaxErrorRecorder :
public nlohmann::json_sax<nlohmann::json> {
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
65nlohmann::json parse(std::string
const &
input)
67 nlohmann::json out = nlohmann::json::parse(
input,
nullptr,
false);
68 if (out.is_discarded()) {
69 SyntaxErrorRecorder recorder;
70 nlohmann::json::sax_parse(
input, &recorder);
71 throw std::runtime_error(recorder.message);
77[[noreturn]]
void throwTypeError(std::string
const &key,
const char *expected, nlohmann::json
const &
value)
79 throw std::runtime_error(
"node \"" + key +
"\": expected " + expected +
", but the value is of type " +
85bool isResettingPossible(nlohmann::json
const &node)
88 if (node.type() == nlohmann::json::value_t::null) {
92 if (node.type() == nlohmann::json::value_t::string) {
93 if (node.get<std::string>().empty()) {
111 return *
nodes.back();
118 std::vector<JSONNode *> &list =
childLists.back();
119 if (!
value.is_structured()) {
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()));
139 std::vector<std::unique_ptr<JSONNode>>
nodes;
147 : _storage{&storage}, _value{
value}, _key{std::move(key)}
234 throw std::runtime_error(
"cannot declare \"" + this->
key() +
"\" to be of map - type, already of type " +
248 throw std::runtime_error(
"cannot declare \"" + this->
key() +
"\" to be of seq - type, already of type " +
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>();
289 throw std::runtime_error(
"node \"" +
_key +
"\": implicit string conversion for type " +
value.type_name() +
297 if (!nd.is_number() && !nd.is_boolean()) {
298 throwTypeError(
_key,
"a number", nd);
300 return nd.get<
int>();
305 if (!nd.is_number() && !nd.is_boolean()) {
306 throwTypeError(
_key,
"a number", nd);
308 return nd.get<
double>();
322 if (nd.is_boolean()) {
323 return nd.get<
bool>();
326 if (nd.is_number_unsigned() && nd.get<nlohmann::json::number_unsigned_t>() <= 1) {
327 return nd.get<nlohmann::json::number_unsigned_t>() == 1;
334 return !
_key.empty();
402 return std::unique_ptr<JSONTree>{
new JSONTree{std::make_unique<JSONNode::Storage>(nlohmann::json{})}};
407 return JSONTree::create(std::string{std::istreambuf_iterator<char>{is}, std::istreambuf_iterator<char>{}});
412 return std::unique_ptr<JSONTree>{
new JSONTree{std::make_unique<JSONNode::Storage>(parse(str))}};
425 recorders.erase(std::find(recorders.begin(), recorders.end(),
this));
442 std::vector<std::string> out;
447 for (
auto const &item :
value.items()) {
448 if (
_read.count(item.key()) == 0) {
449 out.push_back(item.key());
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)
Storage(nlohmann::json value)
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)
double val_double() const
void erase(std::string const &key)
Remove a child of a map, if it exists.
bool is_container() const
void writeJSON(std::ostream &os) const
JSONNode & append_child()
JSONNode & operator<<(std::string const &s)
size_t num_children() const
JSONNode(JSONNode const &)=delete
const JSONNode & operator>>(std::string &v) const
bool has_child(std::string const &) 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
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...