Fawkes API Fawkes Development Version
InterfaceData.cpp
1
2/****************************************************************************
3 * InterfaceData
4 * (auto-generated, do not modify directly)
5 *
6 * Fawkes Blackboard REST API.
7 * Access blackboard data through a REST API.
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 "InterfaceData.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
27InterfaceData::InterfaceData(const std::string &json)
28{
29 from_json(json);
30}
31
32InterfaceData::InterfaceData(const rapidjson::Value &v)
33{
35}
36
38{
39}
40
41std::string
42InterfaceData::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
60void
61InterfaceData::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 (kind_) {
69 rapidjson::Value v_kind;
70 v_kind.SetString(*kind_, allocator);
71 v.AddMember("kind", v_kind, allocator);
72 }
73 if (apiVersion_) {
74 rapidjson::Value v_apiVersion;
75 v_apiVersion.SetString(*apiVersion_, allocator);
76 v.AddMember("apiVersion", v_apiVersion, allocator);
77 }
78 if (id_) {
79 rapidjson::Value v_id;
80 v_id.SetString(*id_, allocator);
81 v.AddMember("id", v_id, allocator);
82 }
83 if (type_) {
84 rapidjson::Value v_type;
85 v_type.SetString(*type_, allocator);
86 v.AddMember("type", v_type, allocator);
87 }
88 if (writer_) {
89 rapidjson::Value v_writer;
90 v_writer.SetString(*writer_, allocator);
91 v.AddMember("writer", v_writer, allocator);
92 }
93 rapidjson::Value v_readers(rapidjson::kArrayType);
94 v_readers.Reserve(readers_.size(), allocator);
95 for (const auto &e : readers_) {
96 rapidjson::Value v;
97 v.SetString(e, allocator);
98 v_readers.PushBack(v, allocator);
99 }
100 v.AddMember("readers", v_readers, allocator);
101 if (data_) {
102 v.AddMember("data", *data_, allocator);
103 }
104 if (timestamp_) {
105 rapidjson::Value v_timestamp;
106 v_timestamp.SetString(*timestamp_, allocator);
107 v.AddMember("timestamp", v_timestamp, allocator);
108 }
109}
110
111void
112InterfaceData::from_json(const std::string &json)
113{
114 rapidjson::Document d;
115 d.Parse(json);
116
118}
119
120void
121InterfaceData::from_json_value(const rapidjson::Value &d)
122{
123 if (d.HasMember("kind") && d["kind"].IsString()) {
124 kind_ = d["kind"].GetString();
125 }
126 if (d.HasMember("apiVersion") && d["apiVersion"].IsString()) {
127 apiVersion_ = d["apiVersion"].GetString();
128 }
129 if (d.HasMember("id") && d["id"].IsString()) {
130 id_ = d["id"].GetString();
131 }
132 if (d.HasMember("type") && d["type"].IsString()) {
133 type_ = d["type"].GetString();
134 }
135 if (d.HasMember("writer") && d["writer"].IsString()) {
136 writer_ = d["writer"].GetString();
137 }
138 if (d.HasMember("readers") && d["readers"].IsArray()) {
139 const rapidjson::Value &a = d["readers"];
140 readers_ = std::vector<std::string>{};
141 ;
142 readers_.reserve(a.Size());
143 for (auto &v : a.GetArray()) {
144 readers_.push_back(v.GetString());
145 }
146 }
147 if (d.HasMember("data") && d["data"].IsObject()) {
148 std::shared_ptr<rapidjson::Document> d_data = std::make_shared<rapidjson::Document>();
149 d_data->CopyFrom(d["data"], d_data->GetAllocator());
150 }
151 if (d.HasMember("timestamp") && d["timestamp"].IsString()) {
152 timestamp_ = d["timestamp"].GetString();
153 }
154}
155
156void
157InterfaceData::validate(bool subcall) const
158{
159 std::vector<std::string> missing;
160 if (!kind_)
161 missing.push_back("kind");
162 if (!apiVersion_)
163 missing.push_back("apiVersion");
164 if (!id_)
165 missing.push_back("id");
166 if (!type_)
167 missing.push_back("type");
168 if (!data_)
169 missing.push_back("data");
170 if (!timestamp_)
171 missing.push_back("timestamp");
172
173 if (!missing.empty()) {
174 if (subcall) {
175 throw missing;
176 } else {
177 std::ostringstream s;
178 s << "InterfaceData is missing field" << ((missing.size() > 0) ? "s" : "") << ": ";
179 for (std::vector<std::string>::size_type i = 0; i < missing.size(); ++i) {
180 s << missing[i];
181 if (i < (missing.size() - 1)) {
182 s << ", ";
183 }
184 }
185 throw std::runtime_error(s.str());
186 }
187 }
188}
virtual ~InterfaceData()
Destructor.
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
InterfaceData()
Constructor.
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
std::optional< std::string > writer() const
Get writer value.
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
virtual void from_json(const std::string &json)
Retrieve data from JSON string.