Skip to content

get_ref<const number_integer_t &>() fails on integers since 3.12.0 release #4752

@trofi

Description

@trofi

Description

When updating json from 3.11.3 to 3.12.0 in nixpkgs as NixOS/nixpkgs#399385 I noticed a regression in the nix test suite:

  3/194 libutil-tests / nix-util-tests                                      FAIL            0.24s   exit status 1
  4/194 libstore-tests / nix-store-tests                                    FAIL            0.26s   exit status 1

Bisecting json landed at commit 9f60e85 "Fix return value of get_ptr for unsigned integers (#4525)".

I extracted minimal example below that illustrates the problem. Can you help me understand if this API change intended or it's a bug?

Reproduction steps

Minimal example bellow contains exact sample to trigger the failure.

Expected vs. actual results

Minimal example below should run (I think).

Minimal code example

// $ g++ main.cc -o main -std=c++20 && ./main; echo $?
#include <nlohmann/json.hpp>

static const nlohmann::json & ensureType(
    const nlohmann::json & value,
    nlohmann::json::value_type expectedType
    )
{
    if (value.type() != expectedType)
        __builtin_trap();

    return value;
}

static const nlohmann::json::object_t & getObject(const nlohmann::json & value)
{
    return ensureType(value, nlohmann::json::value_t::object).get_ref<const nlohmann::json::object_t &>();
}

static const nlohmann::json & valueAt(
    const nlohmann::json::object_t & map,
    const std::string & key)
{
    if (!map.contains(key))
      __builtin_trap();

    return map.at(key);
}

const nlohmann::json::number_integer_t & getInteger(const nlohmann::json & value)
{
    return ensureType(value, nlohmann::json::value_t::number_integer).get_ref<const nlohmann::json::number_integer_t &>();
}

int main() {
  auto simple = R"({ "int": 0 })"_json;

  return getInteger(valueAt(getObject(simple), "int")) == 0;
}

Error messages

On `json-3.12.0` it fails as:


$ g++ main.cc -o main -std=c++20 && ./main; echo $?
terminate called after throwing an instance of 'nlohmann::json_abi_v3_12_0::detail::type_error'
  what():  [json.exception.type_error.303] incompatible ReferenceType for get_ref, actual type is number
Aborted (core dumped)
134


On `json-3.11.3` it works as:


$ g++ main.cc -o main -std=c++20 && ./main; echo $?
1

Compiler and operating system

x86_64-linux gcc-14.2.0

Library version

3.12.0

Validation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions