15 using json =
struct JsonValue ;
34 std::map<std::string, JsonValue>
data;
60 template <
typename T> T
get() {return ;}
65 throw std::runtime_error(std::string(
"Cannot iterate a non array"));
71 throw std::runtime_error(std::string(
"Cannot iterate a non array"));
77 throw std::runtime_error(std::string(
"Cannot size a non array"));
83 throw std::runtime_error(std::string(
"Cannot slice a non array"));
92 throw std::runtime_error(std::string(
"Cannot get map from a non object"));
110 void expect(
char expected) {
if (
get() != expected)
throw std::runtime_error(std::string(
"Expected '") + expected +
"'");}
127 struct JsonObject::dat
130 dat(std::pair<std::string, JsonValue> p) {
k = p.first ;
v = p.second ; }
146 std::map<std::string,JsonValue>::iterator
it ;
154 bool JsonObject::exist(std::string key) {
auto res =
data.find(key) ;
if (res==
data.end())
return false ;
else return true ; }
159 if (
type !=
Type::Object)
throw std::runtime_error(std::string(
"Trying to access key in a non object"));
164 template<>
bool JsonValue::get<bool>() {
if (
type!=
Type::Bool) std::runtime_error(std::string(
"Incorrect type number"));
return bool_value ; }
167 template<> std::string JsonValue::get<std::string>() {
if (
type!=Type::String) std::runtime_error(std::string(
"Incorrect type string"));
return string_value ;}
168 template<> std::vector<double> JsonValue::get<std::vector<double>>()
170 if (
type!=Type::Array) std::runtime_error(std::string(
"Incorrect type array"));
171 std::vector<double> res ;
172 res.resize(array_value.size()) ;
173 for (
size_t i = 0 ; i<array_value.size() ; i++)
175 res[i] = array_value[i].get<
double>() ;
179 template<> std::vector<int> JsonValue::get<std::vector<int>>()
181 if (
type!=Type::Array) std::runtime_error(std::string(
"Incorrect type array"));
182 std::vector<int> res ;
183 res.resize(array_value.size()) ;
184 for (
size_t i = 0 ; i<array_value.size() ; i++)
186 res[i] = array_value[i].get<
int>() ;
190 template<> std::vector<bool> JsonValue::get<std::vector<bool>>()
192 if (
type!=Type::Array) std::runtime_error(std::string(
"Incorrect type array"));
193 std::vector<bool> 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<
bool>() ;
201 template<> std::vector<std::string> JsonValue::get<std::vector<std::string>>()
203 if (
type!=Type::Array) std::runtime_error(std::string(
"Incorrect type array"));
204 std::vector<std::string> 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<std::string>() ;
212 template<> std::vector<std::vector<double>> JsonValue::get<std::vector<std::vector<double>>>()
214 if (
type!=Type::Array) std::runtime_error(std::string(
"Incorrect type array"));
215 std::vector<std::vector<double>> 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<std::vector<double>>() ;
223 template<> std::map<std::string,std::string> JsonValue::get<std::map<std::string,std::string>>()
225 if (
type!=Type::Object) std::runtime_error(std::string(
"Incorrect type object for mapping"));
226 std::map<std::string,std::string> res ;
227 for (
auto [key, value] : object_value.data)
228 res[
key] =
value.get<std::string>() ;
242 throw std::runtime_error(std::string(
"Cannot check existence in a non object"));
246 std::istream&
operator>>(std::istream& in, JsonValue& value) {
247 std::string content((std::istreambuf_iterator<char>(in)),
248 std::istreambuf_iterator<char>());
249 value.parse(content);
253 std::ostream&
operator<< (std::ostream& out,
const JsonValue &j)
263 for (
auto & v: j.array_value)
269 for (
auto& [key, value] : j.object_value.data)
270 out <<
"\"" <<
key <<
"\": " <<
value <<
", \n" ;
284 throw std::runtime_error(
"Unexpected trailing characters");
291 if (c ==
'"')
return parse_string();
292 if (c ==
'-' || std::isdigit(c))
return parse_number();
293 if (c ==
't')
return parse_true();
294 if (c ==
'f')
return parse_false();
295 if (c ==
'n')
return parse_null();
296 if (c ==
'[')
return parse_array();
297 if (c ==
'{')
return parse_object();
298 throw std::runtime_error(
"Unexpected character in JSON input");
303 expect(
'u'); expect(
'l'); expect(
'l');
309 expect(
'r'); expect(
'u'); expect(
'e');
315 expect(
'a'); expect(
'l'); expect(
's'); expect(
'e');
321 if (peek() ==
'-') ++
pos;
322 while (std::isdigit(peek())) ++
pos;
325 while (std::isdigit(peek())) ++
pos;
327 if (peek() ==
'e' || peek() ==
'E') {
329 if (peek() ==
'+' || peek() ==
'-') ++
pos;
330 while (std::isdigit(peek())) ++
pos;
332 double value = std::stod(text.substr(start,
pos - start));
339 while (peek() !=
'"') {
341 if (c ==
'\0')
throw std::runtime_error(
"Unterminated string");
359 arr.push_back(parse_value());
363 }
else if (peek() ==
']') {
367 throw std::runtime_error(
"Expected ',' or ']'");
385 if (peek() !=
'"')
throw std::runtime_error(
"Expected object key string");
386 std::string
key = parse_string().string_value;
390 obj[
key] = parse_value();
394 }
else if (peek() ==
'}') {
398 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:108
JsonValue parse(std::string txt)
Definition: json_parser.h:103
size_t pos
Definition: json_parser.h:100
void expect(char expected)
Definition: json_parser.h:110
void skip_whitespace()
Definition: json_parser.h:107
char get()
Definition: json_parser.h:109
@ 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:131
JsonValue & value()
Definition: json_parser.h:132
dat(std::pair< std::string, JsonValue > p)
Definition: json_parser.h:130
Definition: json_parser.h:139
iterator(std::map< std::string, JsonValue >::iterator a)
Definition: json_parser.h:140
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:141
iterator & operator++()
Definition: json_parser.h:142
Definition: json_parser.h:39
size_t size()
Definition: json_parser.h:74
enum nddem::JsonValue::Type type
bool is_array()
Definition: json_parser.h:56
JsonObject object_value
Definition: json_parser.h:46
bool is_object()
Definition: json_parser.h:57
JsonValue(double n)
Definition: json_parser.h:49
JsonValue(bool b)
Definition: json_parser.h:50
Type
Definition: json_parser.h:40
JsonValue()
Definition: json_parser.h:48
bool exist(std::string key)
bool is_number()
Definition: json_parser.h:54
static JsonValue parse(std::string &val)
Definition: json_parser.h:121
bool bool_value
Definition: json_parser.h:43
std::string string_value
Definition: json_parser.h:44
auto begin()
Definition: json_parser.h:62
bool is_string()
Definition: json_parser.h:55
T get()
Definition: json_parser.h:60
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:53
JsonValue(std::string s)
Definition: json_parser.h:51
JsonObject & items()
Definition: json_parser.h:89
JsonValue(JsonArray a)
Definition: json_parser.h:52
auto end()
Definition: json_parser.h:68