16 using json =
struct JsonValue ;
35 std::map<std::string, JsonValue>
data;
61 template <
typename T> T
get() {return ;}
66 throw std::runtime_error(std::string(
"Cannot iterate a non array"));
72 throw std::runtime_error(std::string(
"Cannot iterate a non array"));
78 throw std::runtime_error(std::string(
"Cannot size a non array"));
84 throw std::runtime_error(std::string(
"Cannot slice a non array"));
94 throw std::runtime_error(std::string(
"Cannot get map from a non object"));
113 void expect(
char expected) {
if (
get() != expected)
throw std::runtime_error(std::string(
"Expected '") + expected +
"'");}
125 int replacements = 0;
126 std::regex reg(
"[$][a-zA-Z0-9]+");
127 auto envvar = std::sregex_iterator(in.begin(), in.end(), reg);
128 auto endenvvar = std::sregex_iterator();
129 while (envvar != endenvvar)
131 std::smatch match = *(envvar) ;
132 std::string val =std::string(std::getenv(match.str().substr(1).c_str())) ;
133 in=in.replace(match.position(), match.length(), val) ;
134 envvar = std::sregex_iterator(in.begin(), in.end(), reg);
137 if (replacements>0) std::cout <<
"====Replaced\n" << in ;
149 struct JsonObject::dat
152 dat(std::pair<std::string, JsonValue> p) {
k = p.first ;
v = p.second ; }
168 std::map<std::string,JsonValue>::iterator
it ;
176 bool JsonObject::exist(std::string key) {
auto res =
data.find(key) ;
if (res==
data.end())
return false ;
else return true ; }
181 if (
type !=
Type::Object)
throw std::runtime_error(std::string(
"Trying to access key in a non object"));
186 template<>
bool JsonValue::get<bool>() {
if (
type!=
Type::Bool) std::runtime_error(std::string(
"Incorrect type number"));
return bool_value ; }
189 template<> std::string JsonValue::get<std::string>() {
if (
type!=Type::String) std::runtime_error(std::string(
"Incorrect type string"));
return string_value ;}
190 template<> std::vector<double> JsonValue::get<std::vector<double>>()
192 if (
type!=Type::Array) std::runtime_error(std::string(
"Incorrect type array"));
193 std::vector<double> res ;
194 res.resize(array_value.size()) ;
195 for (
size_t i = 0 ; i<array_value.size() ; i++)
197 res[i] = array_value[i].get<
double>() ;
201 template<> std::vector<int> JsonValue::get<std::vector<int>>()
203 if (
type!=Type::Array) std::runtime_error(std::string(
"Incorrect type array"));
204 std::vector<int> res ;
205 res.resize(array_value.size()) ;
206 for (
size_t i = 0 ; i<array_value.size() ; i++)
208 res[i] = array_value[i].get<
int>() ;
212 template<> std::vector<bool> JsonValue::get<std::vector<bool>>()
214 if (
type!=Type::Array) std::runtime_error(std::string(
"Incorrect type array"));
215 std::vector<bool> res ;
216 res.resize(array_value.size()) ;
217 for (
size_t i = 0 ; i<array_value.size() ; i++)
219 res[i] = array_value[i].get<
bool>() ;
223 template<> std::vector<std::string> JsonValue::get<std::vector<std::string>>()
225 if (
type!=Type::Array) std::runtime_error(std::string(
"Incorrect type array"));
226 std::vector<std::string> res ;
227 res.resize(array_value.size()) ;
228 for (
size_t i = 0 ; i<array_value.size() ; i++)
230 res[i] = array_value[i].get<std::string>() ;
234 template<> std::vector<std::vector<double>> JsonValue::get<std::vector<std::vector<double>>>()
236 if (
type!=Type::Array) std::runtime_error(std::string(
"Incorrect type array"));
237 std::vector<std::vector<double>> res ;
238 res.resize(array_value.size()) ;
239 for (
size_t i = 0 ; i<array_value.size() ; i++)
241 res[i] = array_value[i].get<std::vector<double>>() ;
245 template<> std::map<std::string,std::string> JsonValue::get<std::map<std::string,std::string>>()
247 if (
type!=Type::Object) std::runtime_error(std::string(
"Incorrect type object for mapping"));
248 std::map<std::string,std::string> res ;
249 for (
auto [key, value] : object_value.data)
250 res[
key] =
value.get<std::string>() ;
264 throw std::runtime_error(std::string(
"Cannot check existence in a non object"));
268 std::istream&
operator>>(std::istream& in, JsonValue& value) {
269 std::string content((std::istreambuf_iterator<char>(in)),
270 std::istreambuf_iterator<char>());
275 std::ostream&
operator<< (std::ostream& out,
const JsonValue &j)
285 for (
auto & v: j.array_value)
291 for (
auto& [key, value] : j.object_value.data)
292 out <<
"\"" <<
key <<
"\": " <<
value <<
", \n" ;
306 throw std::runtime_error(
"Unexpected trailing characters");
313 if (c ==
'"')
return parse_string();
314 if (c ==
'-' || std::isdigit(c))
return parse_number();
315 if (c ==
't')
return parse_true();
316 if (c ==
'f')
return parse_false();
317 if (c ==
'n')
return parse_null();
318 if (c ==
'[')
return parse_array();
319 if (c ==
'{')
return parse_object();
320 throw std::runtime_error(
"Unexpected character in JSON input");
325 expect(
'u'); expect(
'l'); expect(
'l');
331 expect(
'r'); expect(
'u'); expect(
'e');
337 expect(
'a'); expect(
'l'); expect(
's'); expect(
'e');
343 if (peek() ==
'-') ++
pos;
344 while (std::isdigit(peek())) ++
pos;
347 while (std::isdigit(peek())) ++
pos;
349 if (peek() ==
'e' || peek() ==
'E') {
351 if (peek() ==
'+' || peek() ==
'-') ++
pos;
352 while (std::isdigit(peek())) ++
pos;
354 double value = std::stod(text.substr(start,
pos - start));
361 while (peek() !=
'"') {
363 if (c ==
'\0')
throw std::runtime_error(
"Unterminated string");
381 arr.push_back(parse_value());
385 }
else if (peek() ==
']') {
389 throw std::runtime_error(
"Expected ',' or ']'");
407 if (peek() !=
'"')
throw std::runtime_error(
"Expected object key string");
408 std::string
key = parse_string().string_value;
412 obj[
key] = parse_value();
416 }
else if (peek() ==
'}') {
420 throw std::runtime_error(
"Expected ',' or '}'");
namespace for Niels Lohmann
Definition: json.hpp:20203
Definition: json_parser.h:20
iterator end()
Definition: json_parser.h:152
JsonValue & at(std::string &s)
Definition: json_parser.h:150
std::map< std::string, JsonValue > data
Definition: json_parser.h:34
JsonValue & operator[](std::string &s)
Definition: json_parser.h:149
JsonValue & at(std::string &s)
bool exist(std::string key)
iterator begin()
Definition: json_parser.h:151
Definition: json_parser.h:98
std::string text
Definition: json_parser.h:99
char peek() const
Definition: json_parser.h:111
JsonValue parse(std::string txt)
Definition: json_parser.h:106
size_t pos
Definition: json_parser.h:100
int process_env(std::string &in)
Definition: json_parser.h:123
void expect(char expected)
Definition: json_parser.h:113
void skip_whitespace()
Definition: json_parser.h:110
char get()
Definition: json_parser.h:112
@ pos
Definition: Typedefs.h:19
type
The type the bitset is encoded with.
Definition: bitset.hpp:44
@ key
the parser read a key of a value in an object
Definition: json_parser.h:11
std::vector< JsonValue > JsonArray
Definition: json_parser.h:17
std::ostream & operator<<(std::ostream &out, const JsonValue &j)
Definition: json_parser.h:253
std::istream & operator>>(std::istream &in, JsonValue &value)
Definition: json_parser.h:246
Definition: json.hpp:5678
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1282
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1181
PUGI_IMPL_FN xpath_string string_value(const xpath_node &na, xpath_allocator *alloc)
Definition: pugixml.cpp:8147
Definition: json_parser.h:128
std::string k
Definition: json_parser.h:134
JsonValue v
Definition: json_parser.h:135
std::string key()
Definition: json_parser.h:153
JsonValue & value()
Definition: json_parser.h:154
dat(std::pair< std::string, JsonValue > p)
Definition: json_parser.h:152
Definition: json_parser.h:139
iterator(std::map< std::string, JsonValue >::iterator a)
Definition: json_parser.h:162
std::map< std::string, JsonValue >::iterator it
Definition: json_parser.h:146
dat operator*()
Definition: json_parser.h:143
bool operator!=(iterator &o)
Definition: json_parser.h:163
iterator & operator++()
Definition: json_parser.h:164
Definition: json_parser.h:39
size_t size()
Definition: json_parser.h:75
enum nddem::JsonValue::Type type
bool is_array()
Definition: json_parser.h:57
JsonObject object_value
Definition: json_parser.h:46
bool is_object()
Definition: json_parser.h:58
JsonValue(double n)
Definition: json_parser.h:50
JsonValue(bool b)
Definition: json_parser.h:51
Type
Definition: json_parser.h:40
JsonValue()
Definition: json_parser.h:49
bool exist(std::string key)
bool is_number()
Definition: json_parser.h:55
static JsonValue parse(std::string &val)
Definition: json_parser.h:142
bool bool_value
Definition: json_parser.h:43
std::string string_value
Definition: json_parser.h:44
auto begin()
Definition: json_parser.h:63
bool is_string()
Definition: json_parser.h:56
T get()
Definition: json_parser.h:61
double number_value
Definition: json_parser.h:42
JsonArray array_value
Definition: json_parser.h:45
JsonValue & operator[](std::string key)
Definition: json_parser.h:157
JsonValue(JsonObject o)
Definition: json_parser.h:54
JsonValue(std::string s)
Definition: json_parser.h:52
JsonObject & items()
Definition: json_parser.h:90
JsonValue(JsonArray a)
Definition: json_parser.h:53
auto end()
Definition: json_parser.h:69