Skip to content

Commit

Permalink
Merge pull request #49 from VCTLabs/json-support-double
Browse files Browse the repository at this point in the history
json.hh: get/set for double floating point values
  • Loading branch information
SJLC committed May 5, 2023
2 parents 0b9a87f + 8bf69b0 commit 63e505a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
20 changes: 19 additions & 1 deletion inc/json.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class json_missing_field : public std::runtime_error
class json {
public:
// normally want to take reference on underlying json_object*,
// except for case of initializing from an exising raw json_object*
// except for case of initializing from an existing raw json_object*
// such as those returned by redis_ipc -- those start out with a reference
explicit json(bool is_array = false) {
if (is_array) obj = json_object_new_array();
Expand Down Expand Up @@ -52,6 +52,11 @@ class json {
return !is_same;
}

std::string dump(void) const {
std::string repr = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PRETTY);
return repr;
}

std::string to_string() const {
std::string value;
if (obj) value = json_object_get_string(obj);
Expand All @@ -60,6 +65,12 @@ class json {
return value;
}

double to_double() const {
int value = -1;
if (obj) value = json_object_get_double(obj);
return value;
}

int to_int() const {
int value = -1;
if (obj) value = json_object_get_int(obj);
Expand Down Expand Up @@ -117,6 +128,13 @@ class json {
json_object_object_add(obj, field_name, string_obj);
}

void set_field(const char *field_name, const double &value) {
if (!json_object_is_type(obj, json_type_object))
throw std::runtime_error("Not a hash-type object!");
json_object *double_obj = json_object_new_double(value);
json_object_object_add(obj, field_name, double_obj);
}

void set_field(const char *field_name, const int &value) {
if (!json_object_is_type(obj, json_type_object))
throw std::runtime_error("Not a hash-type object!");
Expand Down
5 changes: 5 additions & 0 deletions test/json_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ int main(int argc, char **argv)
json wooka = snook;
cout << wooka.get_field("betook").get_field("baggins").to_string() << endl;
}
snook.set_field("blueness", 1.5e9);
cout << "Display individual fields..." << endl;
cout << snook.get_field("betook").get_field("baggins").to_string() << endl;
cout << snook.get_field("betook").get_field("bilbo").to_string() << endl;
cout << snook.get_field("blueness").to_string() << endl;
cout << "Display whole thing..." << endl;
cout << snook.dump() << endl;

if (snook.has_field("betook"))
cout << "OK this field exists" << endl;
Expand Down
10 changes: 10 additions & 0 deletions test/json_test.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
9999
Display individual fields...
9999
hungry
1500000000.0
Display whole thing...
{
"betook":{
"baggins":9999,
"bilbo":"hungry"
},
"blueness":1500000000.0
}
OK this field exists
OK this field does not exist
OK these objects match
Expand Down

0 comments on commit 63e505a

Please sign in to comment.