Skip to content

Commit cc12d35

Browse files
committed
update to retrieve data
1 parent 56c1791 commit cc12d35

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

example/main.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ int main(int argc, char **argv) {
1010
person["age"] = 21;
1111
person.insert("name", "Adit");
1212
person["height"] = 186.5;
13+
person["alive"] = true;
1314

1415
object languages;
1516
languages.push_back("German");
@@ -18,8 +19,16 @@ int main(int argc, char **argv) {
1819

1920
person["languages"] = languages;
2021
person["languages"].push_back("French");
22+
logger::info(person.dumps(2));
2123

22-
std::cout << person << std::endl << std::endl;
24+
std::string name = person["name"];
25+
logger::info(name);
26+
27+
int age = person["age"];
28+
logger::info(std::to_string(age));
29+
30+
bool alive = person["alive"];
31+
logger::info(alive ? "Alive" : "Dead");
2332

2433
parser jsonParser;
2534
object obj = jsonParser.loads(person.dumps(2));

include/json/object.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ class object {
8181

8282
// Output functions
8383
friend std::ostream &operator<<(std::ostream &os, object &obj);
84+
85+
operator std::nullptr_t() const;
86+
// operator bool() const;
87+
operator double() const;
88+
operator std::string() const;
8489
};
8590

8691
} // namespace json

src/object.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ void json::object::erase(const std::string key) {
170170
this->_map.erase(key);
171171
return;
172172
}
173-
std::map<std::string, json::object>::iterator json::object::find(const std::string key){
173+
std::map<std::string, json::object>::iterator
174+
json::object::find(const std::string key) {
174175
this->assertIsMap();
175176
return this->_map.find(key);
176177
}
@@ -350,4 +351,44 @@ std::ostream &operator<<(std::ostream &os, object &obj) {
350351
return os;
351352
}
352353

354+
object::operator std::nullptr_t() const {
355+
if (this->_type.get() != type::null) {
356+
logger::error("This object is not a null",
357+
"json::object::operator std::nullptr_t()");
358+
}
359+
360+
return nullptr;
361+
}
362+
// object::operator bool() const {
363+
// if (this->_type.get() != type::boolean) {
364+
// logger::error("This object is not a boolean",
365+
// "json::object::operator bool()");
366+
// }
367+
368+
// return std::get<bool>(this->_value);
369+
// }
370+
object::operator double() const {
371+
std::cout << this->_type.get() << std::endl;
372+
if (this->_type.get() != type::number && this->_type.get() != type::integer &&
373+
this->_type.get() != type::boolean) {
374+
logger::error("This object is not a number",
375+
"json::object::operator double()");
376+
}
377+
378+
if (this->_type.get() == type::integer)
379+
return static_cast<double>(std::get<long long int>(this->_value));
380+
else if (this->_type.get() == type::boolean)
381+
return static_cast<double>(std::get<bool>(this->_value));
382+
else
383+
return std::get<double>(this->_value);
384+
}
385+
object::operator std::string() const {
386+
if (this->_type.get() != type::string) {
387+
logger::error("This object is not a string",
388+
"json::object::operator std::string()");
389+
}
390+
391+
return std::get<std::string>(this->_value);
392+
}
393+
353394
} // namespace json

0 commit comments

Comments
 (0)