Fawkes API  Fawkes Development Version
Service.cpp
1 
2 /****************************************************************************
3  * Service
4  * (auto-generated, do not modify directly)
5  *
6  * Fawkes Backend Info REST API.
7  * Provides backend meta information to the frontend.
8  *
9  * API Contact: Tim Niemueller <niemueller@kbsg.rwth-aachen.de>
10  * API Version: v1beta1
11  * API License: Apache 2.0
12  ****************************************************************************/
13 
14 #include "Service.h"
15 
16 #include <rapidjson/document.h>
17 #include <rapidjson/prettywriter.h>
18 #include <rapidjson/stringbuffer.h>
19 #include <rapidjson/writer.h>
20 
21 #include <sstream>
22 
24 {
25 }
26 
27 Service::Service(const std::string &json)
28 {
29  from_json(json);
30 }
31 
32 Service::Service(const rapidjson::Value &v)
33 {
34  from_json_value(v);
35 }
36 
38 {
39 }
40 
41 std::string
42 Service::to_json(bool pretty) const
43 {
44  rapidjson::Document d;
45 
46  to_json_value(d, d);
47 
48  rapidjson::StringBuffer buffer;
49  if (pretty) {
50  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
51  d.Accept(writer);
52  } else {
53  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
54  d.Accept(writer);
55  }
56 
57  return buffer.GetString();
58 }
59 
60 void
61 Service::to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
62 {
63  rapidjson::Document::AllocatorType &allocator = d.GetAllocator();
64  v.SetObject();
65  // Avoid unused variable warnings
66  (void)allocator;
67 
68  if (name_) {
69  rapidjson::Value v_name;
70  v_name.SetString(*name_, allocator);
71  v.AddMember("name", v_name, allocator);
72  }
73  if (url_) {
74  rapidjson::Value v_url;
75  v_url.SetString(*url_, allocator);
76  v.AddMember("url", v_url, allocator);
77  }
78 }
79 
80 void
81 Service::from_json(const std::string &json)
82 {
83  rapidjson::Document d;
84  d.Parse(json);
85 
86  from_json_value(d);
87 }
88 
89 void
90 Service::from_json_value(const rapidjson::Value &d)
91 {
92  if (d.HasMember("name") && d["name"].IsString()) {
93  name_ = d["name"].GetString();
94  }
95  if (d.HasMember("url") && d["url"].IsString()) {
96  url_ = d["url"].GetString();
97  }
98 }
99 
100 void
101 Service::validate(bool subcall) const
102 {
103  std::vector<std::string> missing;
104  if (!name_)
105  missing.push_back("name");
106  if (!url_)
107  missing.push_back("url");
108 
109  if (!missing.empty()) {
110  if (subcall) {
111  throw missing;
112  } else {
113  std::ostringstream s;
114  s << "Service is missing field" << ((missing.size() > 0) ? "s" : "") << ": ";
115  for (std::vector<std::string>::size_type i = 0; i < missing.size(); ++i) {
116  s << missing[i];
117  if (i < (missing.size() - 1)) {
118  s << ", ";
119  }
120  }
121  throw std::runtime_error(s.str());
122  }
123  }
124 }
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
Definition: Service.cpp:61
virtual void from_json(const std::string &json)
Retrieve data from JSON string.
Definition: Service.cpp:81
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
Definition: Service.cpp:90
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
Definition: Service.cpp:42
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
Definition: Service.cpp:101
Service()
Constructor.
Definition: Service.cpp:23
virtual ~Service()
Destructor.
Definition: Service.cpp:37