1 #ifndef DBALLE_CORE_JSON_H 2 #define DBALLE_CORE_JSON_H 5 #include <wreport/var.h> 7 #include <dballe/core/fwd.h> 16 using std::runtime_error::runtime_error;
40 std::vector<State> stack;
46 void jputs(
const char* s);
65 void add_bool(
bool val);
66 void add_int(
int val);
67 void add_double(
double val);
68 void add_cstring(
const char* val);
69 void add_string(
const std::string& val);
71 void add_ostream(
const T& val)
77 void add_number(
const std::string& val);
78 void add_level(
const Level& val);
79 void add_trange(
const Trange& val);
80 void add_datetime(
const Datetime& val);
82 void add_coords(
const Coords& val);
83 void add_ident(
const Ident& val);
85 void add_station(
const Station& s);
87 void add_values(
const Values& values);
88 void add_dbvalues(
const DBValues& values);
91 void add(
const std::string& val) { add_string(val); }
92 void add(
const char* val) { add_cstring(val); }
93 void add(
double val) { add_double(val); }
94 void add(
int val) { add_int(val); }
95 void add(
bool val) { add_bool(val); }
96 void add(
size_t val) { add_ostream(val); }
98 void add(
const Level& val) { add_level(val); }
99 void add(
const Trange& val) { add_trange(val); }
100 void add(
const Datetime& val) { add_datetime(val); }
101 void add(
const DatetimeRange& val) { add_datetimerange(val); }
102 void add(
const Coords& val) { add_coords(val); }
103 void add(
const Ident& val) { add_ident(val); }
105 void add(
const Station& s) { add_station(s); }
106 void add(
const DBStation& s) { add_dbstation(s); }
107 void add(
const Values& v) { add_values(v); }
108 void add(
const DBValues& v) { add_dbvalues(v); }
111 void add(
const char* a, T b)
118 void add_list(
const T& val)
121 for (
const auto& i : val)
135 virtual void on_start_list() = 0;
136 virtual void on_end_list() = 0;
138 virtual void on_start_mapping() = 0;
139 virtual void on_end_mapping() = 0;
141 virtual void on_add_null() = 0;
142 virtual void on_add_bool(
bool val) = 0;
143 virtual void on_add_int(
int val) = 0;
144 virtual void on_add_double(
double val) = 0;
145 virtual void on_add_string(
const std::string& val) = 0;
148 void parse(std::istream& in);
169 Stream(std::istream& in) : in(in) {}
185 if (c >=
'0' and c <=
'9')
186 res = res * 10 + in.get() -
'0';
198 if (in.peek() ==
'-')
201 return -parse_unsigned<T>();
203 return parse_unsigned<T>();
248 void parse_array(std::function<
void()> on_element);
251 void parse_object(std::function<
void(
const std::string& key)> on_value);
258 template<>
inline std::string Stream::parse() {
return parse_string(); }
259 template<>
inline Coords Stream::parse() {
return parse_coords(); }
260 template<>
inline Station Stream::parse() {
return parse_station(); }
261 template<>
inline DBStation Stream::parse() {
return parse_dbstation(); }
262 template<>
inline Ident Stream::parse() {
return parse_ident(); }
263 template<>
inline Level Stream::parse() {
return parse_level(); }
264 template<>
inline Trange Stream::parse() {
return parse_trange(); }
265 template<>
inline Datetime Stream::parse() {
return parse_datetime(); }
Common base types used by most of DB-All.e code.
Coords parse_coords()
Parse a Coords object.
Station information.
Definition: types.h:793
double parse_double()
Parse a double.
void expect_token(const char *token)
Raise a parse error if the stream does not yield this exact token.
T parse_unsigned()
Parse an unsigned integer.
Definition: json.h:179
Coordinates.
Definition: types.h:368
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:686
void skip_spaces()
Consume and discard all spaces at the start of the stream.
Datetime parse_datetime()
Parse a Datetime object.
Collection of DBValue objects, indexed by wreport::Varcode.
Definition: values.h:191
Vertical level or layer.
Definition: types.h:624
A station identifier, that can be any string (including the empty string) or a missing value...
Definition: types.h:747
Range of datetimes.
Definition: types.h:294
JSON sax-like parser.
Definition: json.h:130
DBStation parse_dbstation()
Parse a Coords object.
Station parse_station()
Parse a Coords object.
void val_head()
Append whatever separator is needed (if any) before a new value.
void parse_object(std::function< void(const std::string &key)> on_value)
Parse a JSON object, calling on_value to parse each value.
T parse_signed()
Parse a signed integer.
Definition: json.h:196
Date and time.
Definition: types.h:164
std::string parse_string()
Parse a string from the start of the stream.
JSON serializer.
Definition: json.h:29
Level parse_level()
Parse a Level object.
Element identify_next()
Identify the next element in the stream, without moving the stream position.
Ident parse_ident()
Parse an Ident object.
std::tuple< std::string, bool > parse_number()
Parse a number, without converting it.
void parse_array(std::function< void()> on_element)
Parse a JSON array, calling on_element to parse each element.
DatetimeRange parse_datetimerange()
Parse a DatetimeRange object.
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:176
Trange parse_trange()
Parse a Trange object.
void reset()
Reset the serializer state, to cancel the current output and prepare for a new one.