There are cases where a handler that returns a json element needs to
return a successfull empty response. This is common in async operation.
Though it is legit to return an empty string in this situation, it is
cleaner to return an empty response.
This adds a json_void class, that a method that needs to return a value
(i.e. the json_function) can return and the formatter will set the
response to empty.
Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
This patch adds a general assignment operator to the json_list object.
It can accept any data structure that support const range iteration and
that its contained object can be assigned to the json_list object.
Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
This change how the json formatter handle float and adds double support.
float and double conversion will throw exceptions when try to convert
inf or nan.
It also contains tests for both float and double including inf, -inf and
nan for both float and double.
Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
From http://en.cppreference.com/w/cpp/language/constexpr:
A constexpr specifier used in an object declaration implies const.
However, We can not change from
static constexpr const char* TIME_FORMAT = "%a %b %d %I:%M:%S %Z %Y";
to
static constexpr char* TIME_FORMAT = "%a %b %d %I:%M:%S %Z %Y";
The compiler complains:
In file included from json/formatter.cc:22:0:
json/formatter.hh:132:42: error: deprecated conversion from string
constant to ‘char*’ [-Werror=write-strings]
static constexpr char* TIME_FORMAT = "%a %b %d %I:%M:%S %Z %Y";
Since, unlike const, constexpr does not modify a type. It just applies
to an object (or function), and incidentally implies const to the
top-level type.
The code generation generate an enum_wrapper for the enum that defined
in the swagger definition file.
This patch adds more functionality to the enum_wrapper:
It is now possible to cast the enum to other enum if wrapped enum is a
subset of the target enum, this is a reverse functionality of
constructing an enum from another enum.
The wrapper now support comparison operator, begin, end method and the
++ operator, with that the boost::irange is used to return a range
object so the the enum values can be iterate using for range loop.
This is an example of the added methods, After code generation:
template<class T>
operator T() const {
switch(v) {
case my_object_enum_var::VAL1: return T::VAL1;
case my_object_enum_var::VAL2: return T::VAL2;
case my_object_enum_var::VAL3: return T::VAL3;
default: return T::VAL1;
}
}
typedef typename std::underlying_type<my_object_enum_var>::type
pos_type;
enum_var_wrapper& operator++() {
v = static_cast<my_object_enum_var>(static_cast<pos_type>(v) + 1);
return *this;
}
enum_var_wrapper& operator++(int) {
return ++(*this);
}
bool operator==(const enum_var_wrapper& c) const {
return v == c.v;
}
bool operator!=(const enum_var_wrapper& c) const {
return v != c.v;
}
bool operator<=(const enum_var_wrapper& c) const {
return static_cast<pos_type>(v) <= static_cast<pos_type>(c.v);
}
static enum_var_wrapper begin() {
return enum_var_wrapper(my_object_enum_var::VAL1);
}
static enum_var_wrapper end() {
return enum_var_wrapper(my_object_enum_var::NUM_ITEMS);
}
static boost::integer_range<enum_var_wrapper> all_items() {
return boost::irange(begin(), end());
}
This generated code code be found after compilation at:
./build/release/gen/apps/httpd/demo.json.hh
Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
This takes the json2code perl script from the osv and pass it with small
adaptation.
It takes a swagger definition file and creates a hh file from it with
the following code support.
Api opperations are translated to path_description with a name that
determine by their nick name.
Moduls are defined as json object with the same name.
Enums are defined as enum class, a string to enum function is defined
for any query enum parameters.
For enums that are part of a json object a conversion function is
defined so enum of a different type can be assigned to the target enum
as long as it has the same enum values.
Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
This is a migration of the basic json support taken from the
osv/httpserver.
The routes uses the json object to return errors in a json format.
This also adds json_exception which has a constructor from an exception.
Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>