Skip to content

NaN to json and back #515

Closed
Closed

Description

@martukas

double val = std::numeric_limits::quiet_NaN();
DBG << "val=" << val;
json j = val;
DBG << "j=" << j.dump();
val = j;
DBG << "val=" << val;

goes like this:

val=nan
j=null
terminate called after throwing an instance of 'std::domain_error'
what(): type must be number, but is null

Could this not be behave nicer?

Activity

  1. nlohmann commented on Mar 17, 2017

    @nlohmann
    Owner

    With the code in the develop branch, this works:

    int main()
    {
        double val = std::numeric_limits<double>::quiet_NaN();
        std::cout << "val=" << val << std::endl;
        json j = val;
        std::cout << "j=" << j.dump() << std::endl;
        val = j;
        std::cout << "val=" << val << std::endl;
    }

    output:

    val=nan
    j=null
    val=nan
    

    Note the develop branch is unreleased and is subject to changes until version 3.0.0. This behavior of NaN was only introduced recently.

  2. nlohmann commented on Mar 22, 2017

    @nlohmann
    Owner

    @martukas Did you try the develop branch? Could you comment on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions