Giter Club home page Giter Club logo

Comments (8)

inikulshin avatar inikulshin commented on May 26, 2024 1

Imho, number parsing is better to be redesigned.

  1. Reader should accept all JSON numbers, by simple "regex" described here http://www.json.org. E.g. 1000-digit number is ok.
  2. template value.as_number<TNumber> should call static minijson::stonum<TNumber>
  3. 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.
  4. User always can add his specialization for stonum, e.g. stonum<MyBigNumber>

P.S. Thanks for this great project!

from minijson_reader.

giacomodrago avatar giacomodrago commented on May 26, 2024

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.

dlebansais avatar dlebansais commented on May 26, 2024

from minijson_reader.

giacomodrago avatar giacomodrago commented on May 26, 2024

Thanks, I'll rework that as well. Feel free to share some code if you think it's worth sharing.

from minijson_reader.

inikulshin avatar inikulshin commented on May 26, 2024

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.

giacomodrago avatar giacomodrago commented on May 26, 2024

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.

giacomodrago avatar giacomodrago commented on May 26, 2024

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.

giacomodrago avatar giacomodrago commented on May 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.