Skip to content

Commit

Permalink
Merge pull request #806 from Expensify/master
Browse files Browse the repository at this point in the history
Update expensify_prod branch
  • Loading branch information
cead22 authored May 29, 2020
2 parents 59cf382 + 007ff47 commit 2a271b8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
7 changes: 6 additions & 1 deletion libstuff/libstuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1191,8 +1191,13 @@ extern const char* _SParseJSONValue(const char* ptr, const char* end, string& va

string SToJSON(const string& value, const bool forceString) {
// Is it an integer?
if (SToStr(SToInt64(value.c_str())) == value)
if (SToStr(SToInt64(value.c_str())) == value) {
return value;
}
// Is it a float?
if (SToStr(SToFloat(value.c_str())) == value) {
return value;
}

// Is it boolean?
if (SIEquals(value, "true"))
Expand Down
3 changes: 2 additions & 1 deletion libstuff/libstuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <cctype>
#include <chrono>
#include <condition_variable>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
Expand Down Expand Up @@ -378,7 +379,7 @@ string SHexStringFromBase32(const string& buffer);
// **NOTE: Use 'ostringstream' because 'stringstream' leaks on VS2005
template <class T> inline string SToStr(const T& t) {
ostringstream ss;
ss << t;
ss << fixed << showpoint << setprecision(6) << t;
return ss.str();
}

Expand Down
8 changes: 7 additions & 1 deletion test/tests/LibStuffTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ struct LibStuff : tpunit::TestFixture {

void testJSON() {
// Floating point value tests
ASSERT_EQUAL(SToJSON("{\"imAFloat\":1.2}"), "{\"imAFloat\":1.2}");
ASSERT_EQUAL(SToJSON("{\"imAFloat\":0.0000}"), "{\"imAFloat\":0.0000}");
ASSERT_EQUAL(SToJSON("{\"imAFloat\":-0.23456789}"), "{\"imAFloat\":-0.23456789}");
ASSERT_EQUAL(SToJSON("{\"imAFloat\":-123456789.23456789}"), "{\"imAFloat\":-123456789.23456789}");
ASSERT_EQUAL(SToJSON("{\"object\":{\"imAFloat\":1.00}}"), "{\"object\":{\"imAFloat\":1.00}}");

STable testFloats;
testFloats["imAFloat"] = (double)0.000;
string returnVal = SComposeJSONObject(testFloats);
ASSERT_EQUAL(returnVal, "{\"imAFloat\":0.000000}");

// Scientific notation tests
ASSERT_EQUAL(SToJSON("{\"science\":1.5e-8}"), "{\"science\":1.5e-8}");
Expand Down

0 comments on commit 2a271b8

Please sign in to comment.