46 static const picojson::value&
GetValue(
const picojson::object& obj, std::string key) {
49 throw std::logic_error(
"Trying to get the picojson::value from key that does not exist: " + key);
51 return obj.find(key)->second;
55 static const picojson::object&
GetObject(
const picojson::object& obj, std::string key) {
57 throw std::logic_error(
"Trying to get the picojson::object from key that does not exist: " + key);
59 return GetValue(obj, key).get<picojson::object>();
63 static const picojson::array&
GetArray(
const picojson::object& obj, std::string key) {
65 throw std::logic_error(
"Trying to get the picojson::array from key that does not exist: " + key);
67 return GetValue(obj, key).get<picojson::array>();
71 static std::string
GetString(
const picojson::object& obj, std::string key) {
73 throw std::logic_error(
"Trying to get the string from key that does not exist: " + key);
75 return GetValue(obj, key).get<std::string>();
79 static double GetDouble(
const picojson::object& obj, std::string key) {
81 throw std::logic_error(
"Trying to get the double from key that does not exist: " + key);
83 return GetValue(obj, key).get<
double>();
89 throw std::logic_error(
"Trying to get the std::vector<float> from key that does not exist: " + key);
91 std::vector<float> vec;
92 picojson::array arr =
GetArray(obj, key);
93 for (
int i = 0; i < arr.size(); i++) {
94 vec.push_back(arr[i].get<double>());
100 static bool ContainsKey(
const picojson::object& obj, std::string key) {
101 return obj.find(key) != obj.end();
108 picojson::object obj;
115 return picojson::object();
120 return picojson::value(obj);
135 obj[key] = picojson::value(val);
140 obj[key] = picojson::value(num);
146 obj[key] = picojson::value(arr);
152 for (
auto num : vec) {
153 arr.push_back(picojson::value(num));
160 std::string key, std::vector<std::vector<float>> array) {
165 static picojson::value
EncodeArray(
const vector<vector<float>> arr) {
166 vector<picojson::value> result;
167 for(vector<float> subarr : arr) {
168 vector<picojson::value> subarr_encode;
169 for(
float val : subarr) {
170 subarr_encode.push_back(picojson::value(val));
172 result.push_back(picojson::value(subarr_encode));
174 return picojson::value(result);
182 std::cout <<
"Printing Array:" << std::endl << std::endl;
184 for (
auto key : arr) {
185 std::cout <<
"Array item: " << i++ << std::endl;
186 std::cout <<
" " << key << std::endl;
191 static void PrintKeyValues(
const picojson::object& obj, std::string prefix =
" ") {
192 std::cout <<
"PrintKeyValues ---------" << std::endl;
193 for (picojson::object::const_iterator it = obj.begin(); it != obj.end(); it++) {
194 std::cout << prefix << it->first <<
": " << it->second << std::endl;
196 std::cout <<
"End PrintKeyValues ---------" << std::endl;
200 static void Print(
const picojson::object& obj, std::string prefix =
" ") {
201 picojson::value val(obj);
202 std::cout << prefix << val.serialize() << std::endl;
207 std::cout <<
"\n------JSON:------" << std::endl;
210 std::cout <<
"------Key Values:------" << std::endl;
213 std::cout <<
"------Entity Type:------" << std::endl;
215 std::cout <<
" " << type << std::endl;
217 std::cout <<
"------Contains Key:------" << std::endl;
222 std::cout <<
"------Position array:------" << std::endl;
225 for (
int f = 0; f < position.size(); f++) {
226 std::cout <<
" position[" << f <<
"]: " << position[f] << std::endl;
229 std::cout << std::endl;
static picojson::value ConvertPicojsonObjectToValue(picojson::object &obj)
Converts given obj to an equivalent picojson::value.
Definition: json_helper.h:119
static void PrintEntityDetails(const picojson::object &val)
Prints the details of a json object representation of an entity.
Definition: json_helper.h:206
static double GetDouble(const picojson::object &obj, std::string key)
Returns the key from json object obj as a double, will throw an error if key doesn't exist...
Definition: json_helper.h:79
static void PrintKeyValues(const picojson::object &obj, std::string prefix=" ")
Helper method for PrintEntityDetails.
Definition: json_helper.h:191
static void AddStdFloatVectorToJsonObject(picojson::object &obj, std::string key, std::vector< float > vec)
Adds a float vector value named key to a json object.
Definition: json_helper.h:144
static void AddObjectToJsonObject(picojson::object &obj, std::string key, picojson::object &val)
Adds the latter picojson object to the former object as a value under the key 'key'.
Definition: json_helper.h:129
static picojson::array CreateJsonArrayFromVector(std::vector< float > vec)
Creates a json array from a float vector.
Definition: json_helper.h:150
Definition: asubject.cc:3
static bool ContainsKey(const picojson::object &obj, std::string key)
Returns a boolean value of whether the json object obj contains key or not.
Definition: json_helper.h:100
static void Print(const picojson::object &obj, std::string prefix=" ")
Helper method for PrintEntityDetails.
Definition: json_helper.h:200
static std::vector< float > GetStdFloatVector(const picojson::object &obj, std::string key)
Returns the key from json object obj as a float vector, will throw an error if key doesn't exist...
Definition: json_helper.h:87
static const picojson::array & GetArray(const picojson::object &obj, std::string key)
Returns the key from json object obj as a json array, will throw an error if key doesn't exist...
Definition: json_helper.h:63
static void AddStdVectorVectorFloatToJsonObject(picojson::object &obj, std::string key, std::vector< std::vector< float >> array)
Given a picojson object, key, and vector<vector<float>> array, adds the array as the value for key in...
Definition: json_helper.h:159
static const picojson::object & GetObject(const picojson::object &obj, std::string key)
Returns the key from json object obj as a json object, throws error if key doesn't exist...
Definition: json_helper.h:55
static std::string GetString(const picojson::object &obj, std::string key)
Returns the key from json object obj as a string, will throw an error if key doesn't exist...
Definition: json_helper.h:71
static picojson::value EncodeArray(const vector< vector< float >> arr)
Given a vector<vector<float>> array, returns the array as a picojson::value.
Definition: json_helper.h:165
static void AddStringToJsonObject(picojson::object &obj, std::string key, std::string val)
Adds a string value named key to a json object.
Definition: json_helper.h:134
static void AddValueToJsonObject(picojson::object &obj, std::string key, picojson::value val)
Adds a picojson value to the specifiied key in a json object.
Definition: json_helper.h:124
static const picojson::value & GetValue(const picojson::object &obj, std::string key)
Returns the key from json object obj as a json value, throws error if key doesn't exist...
Definition: json_helper.h:47
Definition: json_helper.h:13
static void AddFloatToJsonObject(picojson::object &obj, std::string key, float num)
Adds a float value named key to a json object.
Definition: json_helper.h:139
static picojson::object CreateJsonNotification()
Create a new json object for sending notifications.
Definition: json_helper.h:107
static void PrintArray(const picojson::array &arr)
Helper method for PrintEntityDetails.
Definition: json_helper.h:181
static picojson::object CreateJsonObject()
Returns a new json object.
Definition: json_helper.h:114