Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.

Fix #38 #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions json11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <limits>
#include <sstream>

namespace json11 {

Expand Down Expand Up @@ -321,6 +323,23 @@ bool Json::operator< (const Json &other) const {
* Parsing
*/

/* strtod_dot(str)
*
* Parse the number according to the JSON grammar, ignoring the current locale assuming that the
* string is already validated to comply with the JSON grammar.
*/
double strtod_dot(const char* str) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a good approach, but I don't see it called anywhere. You probably meant to call it in parse_number().

// It is not correct to use ordinary strtod because it uses the locale and in some locales,
// for example, in ru_RU.UTF-8, the floating-point delimiter is "," but not "." as in JSON
// grammar.
const size_t str_length = strspn(str, "0123456789.eE+-");
std::istringstream string_stream(std::string(str, str_length));
string_stream.imbue(std::locale("C"));
double f = NAN;
string_stream >> f;
return f;
}

/* esc(c)
*
* Format char c suitable for printing in an error message.
Expand Down
13 changes: 13 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ JSON11_TEST_CASE(json11_test) {

}

JSON11_TEST_CASE(json11_test_numbers) {
const std::string numbers =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to override the locale in order to really test what you intend.

R"({"numbers": [0, -0, 1, 42, 0.5, -0.5, 0.5e+1, 0.5E+1, 5.0e-1, 0.5e+12]})";
const std::string expected_numbers =
R"({"numbers": [0, 0, 1, 42, 0.5, -0.5, 5, 5, 0.5, 500000000000]})";

std::string err;
const std::string numbers_json = Json::parse(numbers, err).dump();
std::cout << "numbers_json: " << numbers_json << std::endl;
JSON11_TEST_ASSERT(numbers_json == expected_numbers);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also test round-trip back to string to ensure no locale issues there.


#if JSON11_TEST_STANDALONE_MAIN

static void parse_from_stdin() {
Expand All @@ -277,6 +289,7 @@ int main(int argc, char **argv) {
}

json11_test();
json11_test_numbers();
}

#endif // JSON11_TEST_STANDALONE_MAIN
Expand Down