17#include <nlohmann/json.hpp>
40 return *
static_cast<nlohmann::json *
>(
value);
44class SyntaxErrorRecorder :
public nlohmann::json_sax<nlohmann::json> {
46 bool null()
override {
return true; }
47 bool boolean(
bool)
override {
return true; }
48 bool number_integer(number_integer_t)
override {
return true; }
49 bool number_unsigned(number_unsigned_t)
override {
return true; }
50 bool number_float(number_float_t,
const string_t &)
override {
return true; }
51 bool string(string_t &)
override {
return true; }
52 bool binary(binary_t &)
override {
return true; }
53 bool start_object(std::size_t)
override {
return true; }
54 bool key(string_t &)
override {
return true; }
55 bool end_object()
override {
return true; }
56 bool start_array(std::size_t)
override {
return true; }
57 bool end_array()
override {
return true; }
58 bool parse_error(std::size_t,
const std::string &,
const nlohmann::json::exception &
ex)
override
68nlohmann::json parse(std::string
const &
input)
70 nlohmann::json out = nlohmann::json::parse(
input,
nullptr,
false);
71 if (out.is_discarded()) {
72 SyntaxErrorRecorder recorder;
73 nlohmann::json::sax_parse(
input, &recorder);
74 throw std::runtime_error(recorder.message);
80[[noreturn]]
void throwTypeError(std::string
const &key,
const char *expected, nlohmann::json
const &
value)
82 throw std::runtime_error(
"node \"" + key +
"\": expected " + expected +
", but the value is of type " +
88void setContainerType(nlohmann::json &
value, nlohmann::json::value_t
type, std::string
const &key)
93 if (!
value.is_null() && !(
value.is_string() &&
value.get_ref<std::string
const &>().empty())) {
94 throw std::runtime_error(
"cannot declare \"" + key +
"\" to be of " +
95 (
type == nlohmann::json::value_t::object ?
"map" :
"seq") +
" - type, already of type " +
113 return *
nodes.back();
120 std::vector<JSONNode *> &list =
childLists.back();
121 if (!
value.is_structured()) {
124 list.reserve(
value.size());
125 for (
auto it =
value.begin(); it !=
value.end(); ++it) {
126 list.push_back(&
makeNode(it.value(),
value.is_array() ?
"" : it.key()));
141 std::vector<std::unique_ptr<JSONNode>>
nodes;
149 : _storage{&storage}, _value{
value}, _key{std::move(key)}
221 setContainerType(
json(
_value), nlohmann::json::value_t::object,
_key);
227 setContainerType(
json(
_value), nlohmann::json::value_t::array,
_key);
255 switch (
value.type()) {
256 case nlohmann::json::value_t::string:
return value.get<std::string>();
257 case nlohmann::json::value_t::boolean:
return value.get<
bool>() ?
"true" :
"false";
258 case nlohmann::json::value_t::number_integer:
return std::to_string(
value.get<nlohmann::json::number_integer_t>());
259 case nlohmann::json::value_t::number_unsigned:
return std::to_string(
value.get<nlohmann::json::number_unsigned_t>());
260 case nlohmann::json::value_t::number_float: {
263 const double number =
value.get<
double>();
264 std::stringstream ss;
265 for (
int precision = 6; precision <= std::numeric_limits<double>::max_digits10; ++precision) {
267 ss << std::setprecision(precision) << number;
268 if (std::strtod(ss.str().c_str(),
nullptr) == number) {
275 throw std::runtime_error(
"node \"" +
_key +
"\": implicit string conversion for type " +
value.type_name() +
283 if (!nd.is_number() && !nd.is_boolean()) {
284 throwTypeError(
_key,
"a number", nd);
286 return nd.get<
int>();
291 if (!nd.is_number() && !nd.is_boolean()) {
292 throwTypeError(
_key,
"a number", nd);
294 return nd.get<
double>();
308 if (nd.is_boolean()) {
309 return nd.get<
bool>();
312 if (nd.is_number_unsigned() && nd.get<nlohmann::json::number_unsigned_t>() <= 1) {
313 return nd.get<nlohmann::json::number_unsigned_t>() == 1;
377 return std::unique_ptr<JSONTree>{
new JSONTree{std::make_unique<JSONNode::Storage>(nlohmann::json{})}};
382 return JSONTree::create(std::string{std::istreambuf_iterator<char>{is}, std::istreambuf_iterator<char>{}});
387 return std::unique_ptr<JSONTree>{
new JSONTree{std::make_unique<JSONNode::Storage>(parse(str))}};
400 recorders.erase(std::find(recorders.begin(), recorders.end(),
this));
417 std::vector<std::string> out;
422 for (
auto const &item :
value.items()) {
423 if (
_read.count(item.key()) == 0) {
424 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
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
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...