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 <sstream>
22 
24 {
25 }
26 
27 DomainObject::DomainObject(const std::string &json)
28 {
29  from_json(json);
30 }
31 
32 DomainObject::DomainObject(const rapidjson::Value &v)
33 {
34  from_json_value(v);
35 }
36 
38 {
39 }
40 
41 std::string
42 DomainObject::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 DomainObject::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 (name_) {
79  rapidjson::Value v_name;
80  v_name.SetString(*name_, allocator);
81  v.AddMember("name", v_name, 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 }
89 
90 void
91 DomainObject::from_json(const std::string &json)
92 {
93  rapidjson::Document d;
94  d.Parse(json);
95 
96  from_json_value(d);
97 }
98 
99 void
100 DomainObject::from_json_value(const rapidjson::Value &d)
101 {
102  if (d.HasMember("kind") && d["kind"].IsString()) {
103  kind_ = d["kind"].GetString();
104  }
105  if (d.HasMember("apiVersion") && d["apiVersion"].IsString()) {
106  apiVersion_ = d["apiVersion"].GetString();
107  }
108  if (d.HasMember("name") && d["name"].IsString()) {
109  name_ = d["name"].GetString();
110  }
111  if (d.HasMember("type") && d["type"].IsString()) {
112  type_ = d["type"].GetString();
113  }
114 }
115 
116 void
117 DomainObject::validate(bool subcall) const
118 {
119  std::vector<std::string> missing;
120  if (!kind_)
121  missing.push_back("kind");
122  if (!apiVersion_)
123  missing.push_back("apiVersion");
124  if (!name_)
125  missing.push_back("name");
126  if (!type_)
127  missing.push_back("type");
128 
129  if (!missing.empty()) {
130  if (subcall) {
131  throw missing;
132  } else {
133  std::ostringstream s;
134  s << "DomainObject is missing field" << ((missing.size() > 0) ? "s" : "") << ": ";
135  for (std::vector<std::string>::size_type i = 0; i < missing.size(); ++i) {
136  s << missing[i];
137  if (i < (missing.size() - 1)) {
138  s << ", ";
139  }
140  }
141  throw std::runtime_error(s.str());
142  }
143  }
144 }
DomainObject::from_json_value
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
Definition: DomainObject.cpp:100
DomainObject::validate
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
Definition: DomainObject.cpp:117
DomainObject::to_json_value
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
Definition: DomainObject.cpp:61
DomainObject::to_json
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
Definition: DomainObject.cpp:42
DomainObject::DomainObject
DomainObject()
Constructor.
Definition: DomainObject.cpp:23
DomainObject::~DomainObject
virtual ~DomainObject()
Destructor.
Definition: DomainObject.cpp:37
DomainObject::from_json
virtual void from_json(const std::string &json)
Retrieve data from JSON string.
Definition: DomainObject.cpp:91