Json: Cannot get float pointer for value stored as `0`

Created on 7 Mar 2017  路  4Comments  路  Source: nlohmann/json

I am using a web service which doesn't return a floating point all the time. For example, 0 is used to represent 0.0. Because this library needs 0.0 in order to parse it as a float I am having some issues with it. Right now I am unable to get a proper float pointer, simply because the number_float storage is not set.

How would I approach this, since the webservice cannot be changed in order to output 0 as 0.0. I am using lots data that has this, so it would be nice if there was a good solution for this issue.

Any ideas? 馃槃

Maybe it is a good idea to always store ints and float along side each other?

question wontfix

Most helpful comment

I'd say this would be a reasonable thing for the library client to do, but not for the library itself to do. If the client knows that it needs a float there, then it's perfectly reasonable for the client to make it a float.

All 4 comments

I am using the following as a temporary solution:

/// get a pointer to the value (floating-point number)
number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
{
    if (is_number_integer())
    {
        m_type = value_t::number_float;
        m_value.number_float = static_cast<float>(m_value.number_integer);
    }

    return is_number_float() ? &m_value.number_float : nullptr;
}

I understand your problem, but don't know a solution either. Your fix is pragmatic, but I think it's counter-intuitive to change a type with a getter. Maybe someone else has a better idea?

I'd say this would be a reasonable thing for the library client to do, but not for the library itself to do. If the client knows that it needs a float there, then it's perfectly reasonable for the client to make it a float.

I think changing the library in this regard is not helpful.

Was this page helpful?
0 / 5 - 0 ratings