Json: Read `long long int` data as a number.

Created on 14 Nov 2018  路  2Comments  路  Source: nlohmann/json

64-bit integers (long long int) are available on most platforms and frequently used in machine learning data sets as sample IDs, but they are currently read as a string and require parsing.

invalid

Most helpful comment

The library uses std::int64_t and std::uint64_t internally to store integers. There should not be any issue with long long int- in particular, the library will not store numbers as strings. Maybe I misunderstood you.

Here is some example code:

#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

int main()
{
    long long int foo = 123123123123;

    // store a number without parsing
    json j = foo;

    // output JSON value
    std::cout << j << std::endl;

    // roundtrip
    long long int j_foo = j;
    assert(j_foo == foo);

    // parsing creates the same value
    assert(json::parse("123123123123") == j);
}

All 2 comments

The library uses std::int64_t and std::uint64_t internally to store integers. There should not be any issue with long long int- in particular, the library will not store numbers as strings. Maybe I misunderstood you.

Here is some example code:

#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

int main()
{
    long long int foo = 123123123123;

    // store a number without parsing
    json j = foo;

    // output JSON value
    std::cout << j << std::endl;

    // roundtrip
    long long int j_foo = j;
    assert(j_foo == foo);

    // parsing creates the same value
    assert(json::parse("123123123123") == j);
}

You are right. I just noticed that this datum was in fact a string not a long long int. Sorry for a false alarm.

Was this page helpful?
0 / 5 - 0 ratings