Fawkes API Fawkes Development Version
InterfaceMessageType.cpp
1
2/****************************************************************************
3 * InterfaceMessageType
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 "InterfaceMessageType.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
28{
29 from_json(json);
30}
31
33{
35}
36
38{
39}
40
41std::string
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
61InterfaceMessageType::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 rapidjson::Value v_fields(rapidjson::kArrayType);
74 v_fields.Reserve(fields_.size(), allocator);
75 for (const auto &e : fields_) {
76 rapidjson::Value v(rapidjson::kObjectType);
77 e->to_json_value(d, v);
78 v_fields.PushBack(v, allocator);
79 }
80 v.AddMember("fields", v_fields, allocator);
81}
82
83void
84InterfaceMessageType::from_json(const std::string &json)
85{
86 rapidjson::Document d;
87 d.Parse(json);
88
90}
91
92void
93InterfaceMessageType::from_json_value(const rapidjson::Value &d)
94{
95 if (d.HasMember("name") && d["name"].IsString()) {
96 name_ = d["name"].GetString();
97 }
98 if (d.HasMember("fields") && d["fields"].IsArray()) {
99 const rapidjson::Value &a = d["fields"];
100 fields_ = std::vector<std::shared_ptr<InterfaceFieldType>>{};
101 ;
102 fields_.reserve(a.Size());
103 for (auto &v : a.GetArray()) {
104 std::shared_ptr<InterfaceFieldType> nv{new InterfaceFieldType()};
105 nv->from_json_value(v);
106 fields_.push_back(std::move(nv));
107 }
108 }
109}
110
111void
113{
114 std::vector<std::string> missing;
115 if (!name_)
116 missing.push_back("name");
117 for (size_t i = 0; i < fields_.size(); ++i) {
118 if (!fields_[i]) {
119 missing.push_back("fields[" + std::to_string(i) + "]");
120 } else {
121 try {
122 fields_[i]->validate(true);
123 } catch (std::vector<std::string> &subcall_missing) {
124 for (const auto &s : subcall_missing) {
125 missing.push_back("fields[" + std::to_string(i) + "]." + s);
126 }
127 }
128 }
129 }
130
131 if (!missing.empty()) {
132 if (subcall) {
133 throw missing;
134 } else {
135 std::ostringstream s;
136 s << "InterfaceMessageType is missing field" << ((missing.size() > 0) ? "s" : "") << ": ";
137 for (std::vector<std::string>::size_type i = 0; i < missing.size(); ++i) {
138 s << missing[i];
139 if (i < (missing.size() - 1)) {
140 s << ", ";
141 }
142 }
143 throw std::runtime_error(s.str());
144 }
145 }
146}
InterfaceFieldType representation for JSON transfer.
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
InterfaceMessageType()
Constructor.
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
virtual ~InterfaceMessageType()
Destructor.
virtual void from_json(const std::string &json)
Retrieve data from JSON string.