Comments (8)
Imho, number parsing is better to be redesigned.
- Reader should accept all JSON numbers, by simple "regex" described here http://www.json.org. E.g. 1000-digit number is ok.
- template
value.as_number<TNumber>
should callstatic minijson::stonum<TNumber>
stonum<TNumber>
should be implemented for all C++ integer/floating point types, by relevant parsing and maybe throwing (e.g. "out of range"). I don't think parsed values caching is required - every value usually converted only once.- User always can add his specialization for
stonum
, e.g.stonum<MyBigNumber>
P.S. Thanks for this great project!
from minijson_reader.
Apologies for this oversight. This is really sad and embarrassing.
strtod
and alike really belong to a different era, where it was appropriate to change the behaviour of an entire class of functions by messing with some global mutable state. The errno
dance for dealing with error conditions is pretty awful as well.
Let me ponder over a possible solution.
from minijson_reader.
from minijson_reader.
Thanks, I'll rework that as well. Feel free to share some code if you think it's worth sharing.
from minijson_reader.
For original problem, locale decimal point seems to be available as
std::use_facet<std::numpunct<char>>(std::locale()).decimal_point()
.
If it differ from '.', strtod
should be applied on value's buffer copy, with '.' replaced by right char.
from minijson_reader.
There seems to be a light at the end of the tunnel.
The really good news is that starting with C++17, the C++ programming language gained std::from_chars
, an actually usable function to parse numbers without having to deal with the oddities and the idiosyncrasies of the old C standard library, including the terrible locale-dependent behavior trap.
The good and bad news is that GCC added full support for that function with version 11, which was released in 2021. It's good because we can finally parse floating point numbers properly without having to use streams (ew!) or some external library, it's bad because people with older versions of GCC are out of luck.
Since the current version of minijson_reader
is dangerously buggy when it comes to parsing doubles, and I do not want to keep on using strtod
with a weird (and probably buggy) adapter performing a locale dance, nor do I want to pull in some dependency, I think I'll bite the bullet and use std::from_chars
. Those who cannot upgrade their compiler will have to stay on an old tag (and make sure they don't change their locale, sadly) or use a better json library (lol).
from minijson_reader.
OK @inikulshin @dlebansais @michaelkourlas, my idea for the new minijson::value
class is fully implemented in the rework_number_parsing
branch (which became a massive refactor from ancient C++03 to modern C++). The best place to start is the README
(which on that branch is up-to-date with the changes).
Thanks to C++17 and especially its new number-parsing functions, minijson::value
finally looks vaguely usable to me. 😅 Let me know what you think.
User always can add his specialization for stonum, e.g. stonum
Let's add this on a second pass.
from minijson_reader.
User always can add his specialization for stonum, e.g. stonum
Let's add this on a second pass.
Filed as #24
from minijson_reader.
Related Issues (14)
- Small compilation errors HOT 4
- minijson_reader truncates strings containing a Unicode null character (\u0000) HOT 2
- Travis CI tests not running any more
- Rework dispatch HOT 3
- Determine if toplevel content is object or array HOT 10
- Allow user-provided specializations to convert values
- parse_object issue HOT 2
- minijson_reader does not reject unescaped control characters in strings HOT 2
- C++17 features HOT 1
- build errors on OSX
- License change request HOT 3
- Unable to read arrays HOT 1
- minijson_reader does not distinguish between an unparsable value and a value parsable as zero HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from minijson_reader.