Fawkes API  Fawkes Development Version
DomainObject.cpp
1 
2 /****************************************************************************
3  * DomainObject
4  * (auto-generated, do not modify directly)
5  *
6  * CLIPS Executive REST API.
7  * Enables access to goals, plans, and all items in the domain model.
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 "DomainObject.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 <numeric>
22 #include <sstream>
23 
25 {
26 }
27 
28 DomainObject::DomainObject(const std::string &json)
29 {
30  from_json(json);
31 }
32 
33 DomainObject::DomainObject(const rapidjson::Value &v)
34 {
35  from_json_value(v);
36 }
37 
39 {
40 }
41 
42 std::string
43 DomainObject::to_json(bool pretty) const
44 {
45  rapidjson::Document d;
46 
47  to_json_value(d, d);
48 
49  rapidjson::StringBuffer buffer;
50  if (pretty) {
51  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
52  d.Accept(writer);
53  } else {
54  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
55  d.Accept(writer);
56  }
57 
58  return buffer.GetString();
59 }
60 
61 void
62 DomainObject::to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
63 {
64  rapidjson::Document::AllocatorType &allocator = d.GetAllocator();
65  v.SetObject();
66  // Avoid unused variable warnings
67  (void)allocator;
68 
69  if (kind_) {
70  rapidjson::Value v_kind;
71  v_kind.SetString(*kind_, allocator);
72  v.AddMember("kind", v_kind, allocator);
73  }
74  if (apiVersion_) {
75  rapidjson::Value v_apiVersion;
76  v_apiVersion.SetString(*apiVersion_, allocator);
77  v.AddMember("apiVersion", v_apiVersion, allocator);
78  }
79  if (name_) {
80  rapidjson::Value v_name;
81  v_name.SetString(*name_, allocator);
82  v.AddMember("name", v_name, allocator);
83  }
84  if (type_) {
85  rapidjson::Value v_type;
86  v_type.SetString(*type_, allocator);
87  v.AddMember("type", v_type, allocator);
88  }
89 }
90 
91 void
92 DomainObject::from_json(const std::string &json)
93 {
94  rapidjson::Document d;
95  d.Parse(json);
96 
97  from_json_value(d);
98 }
99 
100 void
101 DomainObject::from_json_value(const rapidjson::Value &d)
102 {
103  if (d.HasMember("kind") && d["kind"].IsString()) {
104  kind_ = d["kind"].GetString();
105  }
106  if (d.HasMember("apiVersion") && d["apiVersion"].IsString()) {
107  apiVersion_ = d["apiVersion"].GetString();
108  }
109  if (d.HasMember("name") && d["name"].IsString()) {
110  name_ = d["name"].GetString();
111  }
112  if (d.HasMember("type") && d["type"].IsString()) {
113  type_ = d["type"].GetString();
114  }
115 }
116 
117 void
118 DomainObject::validate(bool subcall) const
119 {
120  std::vector<std::string> missing;
121  if (!kind_) {
122  missing.push_back("kind");
123  }
124  if (!apiVersion_) {
125  missing.push_back("apiVersion");
126  }
127  if (!name_) {
128  missing.push_back("name");
129  }
130  if (!type_) {
131  missing.push_back("type");
132  }
133 
134  if (!missing.empty()) {
135  if (subcall) {
136  throw missing;
137  } else {
138  std::string s =
139  std::accumulate(std::next(missing.begin()),
140  missing.end(),
141  missing.front(),
142  [](std::string &s, const std::string &n) { return s + ", " + n; });
143  throw std::runtime_error("DomainObject is missing " + s);
144  }
145  }
146 }
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
DomainObject()
Constructor.
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
virtual ~DomainObject()
Destructor.
virtual void from_json(const std::string &json)
Retrieve data from JSON string.
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.