Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decimal separator follows the locale #72

Closed
miloyip opened this issue Jul 13, 2014 · 2 comments
Closed

Decimal separator follows the locale #72

miloyip opened this issue Jul 13, 2014 · 2 comments
Assignees
Milestone

Comments

@miloyip
Copy link
Collaborator

miloyip commented Jul 13, 2014

Upstream https://code.google.com/p/rapidjson/issues/detail?id=84

A locale may output "3,1416" instead of "3.1416" due to locale setting. JSON should always uses . as decimal separator. Need to also check other similar issues such as digit grouping, e.g. "123,456.789" is invalid JSON number.

@miloyip miloyip added this to the v1.0 Beta milestone Jul 13, 2014
@miloyip miloyip added the bug label Jul 13, 2014
@miloyip
Copy link
Collaborator Author

miloyip commented Jul 14, 2014

Currently Writer::Double() simply uses snprintf() to convert a double to string.
snprintf() uses the locale settings set by setlocale().
An idea is to setlocale(LC_NUMERIC, "C") before snprintf() and restore the locale after. However, there is no getlocale() in standard C library for backup.
Another way is to develop a custom dtoa() which neglects locale altogether. This way the code can be potentially optimized .

Some implementations can be referred to:

  1. netlib's dtoa
  2. double-conversion
  3. stringencoders

However, floating-point to string conversion is much complicated than integer, e.g. [1][2].

[1] Burger, Robert G., and R. Kent Dybvig. "Printing floating-point numbers quickly and accurately". ACM SIGPLAN Notices. Vol. 31. No. 5. ACM, 1996.)
[2] Kupriianova, Olga, Christoph Lauter, and Jean-Michel Muller. "Radix conversion for IEEE754-2008 mixed radix floating-point arithmetic". Signals, Systems and Computers, 2013 Asilomar Conference on. IEEE, 2013.

@pah
Copy link
Contributor

pah commented Jul 14, 2014

You can use setlocale for querying the locale as well:

If locale is a null pointer, setlocale queries the current C locale without modifying it.

On the other hand, it may be quite costly to fiddle with the locale in each call to Writer::Double. As a first step, it could be added to the documentation that the user should take care for this in the application code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants