Skip to content

Commit

Permalink
Merge pull request #50 from VCTLabs/json-support-double
Browse files Browse the repository at this point in the history
json.hh: fix bug in to_double()
  • Loading branch information
SJLC committed May 5, 2023
2 parents 63e505a + fb0afbc commit 0450757
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion inc/json.hh
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class json {
}

double to_double() const {
int value = -1;
double value = -1;
if (obj) value = json_object_get_double(obj);
return value;
}
Expand Down
8 changes: 8 additions & 0 deletions test/json_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ int main(int argc, char **argv)
cout << "Display whole thing..." << endl;
cout << snook.dump() << endl;

double blue = snook.get_field("blueness").to_double();
double blue_ratio = blue / 1.5e9;
double blue_error = 1.0 - blue_ratio;
if (blue_error < 1e-4 && blue_error > -1e-4)
cout << "OK double value is close enough" << endl;
else
cout << "ERROR double value seems fishy" << endl;

if (snook.has_field("betook"))
cout << "OK this field exists" << endl;
else
Expand Down
1 change: 1 addition & 0 deletions test/json_test.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Display whole thing...
},
"blueness":1500000000.0
}
OK double value is close enough
OK this field exists
OK this field does not exist
OK these objects match
Expand Down

0 comments on commit 0450757

Please sign in to comment.