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

[csv] Exponential formatted numbers with leading zeros in exponent #365

Closed
jsutes opened this issue May 13, 2022 · 1 comment
Closed

[csv] Exponential formatted numbers with leading zeros in exponent #365

jsutes opened this issue May 13, 2022 · 1 comment

Comments

@jsutes
Copy link

jsutes commented May 13, 2022

Describe the bug

"1.0e-3" gets decoded as a number automatically, but "1.0e-03" gets decoded as a string. Having a leading zero in the exponent is a very common way to write exponential formatted numbers (usually two digits are used to represent the exponent, even if only one digit is needed).

A value like "1.0e-03" should get decoded as a number automatically.

If I explicitly set options.column_types("string,string,float"); then the 1.0e-03 will be read as a number, but that only works if you know how many columns you are expecting. In general, I'd like to be able to make a call like this:

auto data = jsoncons::csv::decode_csv<std::vector<std::vector<double>>>(text, options);

to read any sized matrix of numbers. Currently, this throws an exception if any exponents have a leading zero.

Steps to reproduce the bug

Run the code below and observe the pretty print results of the last two rows. The last row's "rate" value is stored as a string.

Include a small, self-contained example if possible

#include <iomanip>
#include <iostream>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/csv/csv.hpp>

const std::string data = R"(index_id,observation_date,rate
EUR_LIBOR_06M,2015-10-23,0.0000214
EUR_LIBOR_06M,2015-10-26,0.0000143
EUR_LIBOR_06M,2015-10-27,0.0000001
EUR_LIBOR_06M,2015-10-27,1.0e-3
EUR_LIBOR_06M,2015-10-27,1.0e-03
)";

using namespace jsoncons;

int main() {

    csv::csv_options options;
    options.assume_header(true);
    options.column_types("string,string,float");

    // Parse the CSV data into an ojson value
    ojson j = csv::decode_csv<ojson>(data, options);

    // Pretty print
    json_options print_options;
    print_options.float_format(float_chars_format::fixed);
    std::cout << "(1)\n" << pretty_print(j, print_options) << "\n\n";


    // Iterate over the rows
    std::cout << "(2)\n";
    for (const auto& row : j.array_range())
    {
        // Access rated as string and rating as double
        std::cout << row["index_id"].as<std::string>() << ", "
            << row["observation_date"].as<std::string>() << ", "
            << row["rate"].as<double>() << "\n";
    }

	return 0;
}

Output:

(1)
[
    {
        "index_id": "EUR_LIBOR_06M",
        "observation_date": "2015-10-23",
        "rate": 0.0000214
    },
    {
        "index_id": "EUR_LIBOR_06M",
        "observation_date": "2015-10-27",
        "rate": 0.001
    },
    {
        "index_id": "EUR_LIBOR_06M",
        "observation_date": "2015-10-27",
        "rate": "1.0e-03"
    }
]

(2)
EUR_LIBOR_06M, 2015-10-23, 2.14e-05
EUR_LIBOR_06M, 2015-10-27, 0.001
EUR_LIBOR_06M, 2015-10-27, 0.001

What compiler, architecture, and operating system?

  • Compiler: Microsoft Visual Studio Professional 2022 Version 17.1.6 Visual C++ 2022 00476-80000-00000-AA787
  • Architecture (e.g. x86, x64) x64
  • Operating system: Windows 10

What jsoncons library version?
Latest on vcpkg: 0.168.3#1

@danielaparker
Copy link
Owner

Thanks for reporting this issue with CSV file parsing. It should be fixed on master. We'll have a new patch release shortly, and will include it on our next update to vcpkg.

@danielaparker danielaparker changed the title Exponential formatted numbers with leading zeros in exponent [csv] Exponential formatted numbers with leading zeros in exponent May 19, 2022
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