Skip to content

Commit

Permalink
Http Server: Allow extending the json interface with a common base class
Browse files Browse the repository at this point in the history
To allow extending the json interface for user defined types a common
jsonable class was added.

This class has a single method to_json.
To add a user define class, inherit the jsonable class and implement the
to_json method.

The formatter was changed to use that.

Signed-off-by: Amnon Heiman <[email protected]>
Signed-off-by: Pekka Enberg <[email protected]>
  • Loading branch information
amnonh authored and Pekka Enberg committed Aug 19, 2014
1 parent 51fedcd commit d7f8c50
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion modules/httpserver/json/formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ string formatter::to_json(const date_time& d)
return res + "\"";
}

string formatter::to_json(const json_base& obj) {
string formatter::to_json(const jsonable& obj) {
return obj.to_json();
}

Expand Down
4 changes: 2 additions & 2 deletions modules/httpserver/json/formatter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace httpserver {

namespace json {

struct json_base;
struct jsonable;

typedef struct tm date_time;

Expand Down Expand Up @@ -105,7 +105,7 @@ public:
* @param obj the date_time to format
* @return the given json object in a json format
*/
static std::string to_json(const json_base& obj);
static std::string to_json(const jsonable& obj);

/**
* return a json formated unsigned long
Expand Down
12 changes: 11 additions & 1 deletion modules/httpserver/json/json_elements.hh
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ public:
std::vector<T> elements;
};

class jsonable {
public:
virtual ~jsonable() = default;
/**
* create a foramted string of the object.
* @return the object formated.
*/
virtual std::string to_json() const = 0;
};

/**
* The base class for all json objects
* It holds a list of all the element in it,
Expand All @@ -145,7 +155,7 @@ public:
* are known in advance and in practice mimic
* reflection
*/
struct json_base {
struct json_base : public jsonable {

virtual ~json_base() = default;

Expand Down

0 comments on commit d7f8c50

Please sign in to comment.