0

I am trying to iterate over an array in a json file to retrieve an entry with an ID. The value that I want to compare it to is not a string (the json default type), but an uint64_t. For testing purposes, I wrote this simplified example:

#include <iostream>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main ()
{
    std::string file = "test.json";
    std::ifstream ifs(file);
    json j = json::parse(ifs);

    std::string s = "10";
    int i = 10;

    for (auto it : j["inputs"]) {
        std::cout << "value: " << it["id"] << std::endl;

        if (it["id"] == s) {
            std::cout << "matching" << std::endl;
        }

        if (it["id"].get<int>() == i) {
            std::cout << "also matching numeric" << std::endl;
        }
    }
}

As you can see, I tried using ints first, because that is probably a more commonly used type. However, I am unable to cast the value I get from the ID-field to any other type. I get the following output:

value: "10"
matching
terminate called after throwing an instance of 'std::domain_error'
  what():  type must be number, but is string
Abgebrochen (Speicherabzug geschrieben)

I would like to know what I have to do to correctly cast entries of the json to an arbitrary data type. Thanks!

The test json:

{
   "inputs": [
      {
         "type": "camera",
         "id": "10"
      },
      {
         "type": "lidar",
         "stream_id": "20"
      }
   ],
   "outputs": [
   ]
}
DocDriven
  • 3,726
  • 6
  • 24
  • 53
  • What does the JSON library's documentation describes as the reason for this exception? – Sam Varshavchik Jun 01 '21 at 17:03
  • @SamVarshavchik I was unable to find sufficient information on this error. I used an example provided by the lib author as a template, which I expected to be working. – DocDriven Jun 01 '21 at 17:17
  • 4
    It looks like a string to me. It's got quotes around it and everything. Are you _sure_ it's a `uint64_t`? – Tim Randall Jun 01 '21 at 17:18
  • @TimRandall Do you mean the json value? It is indeed a string, but I need to compare it to a numeric data type. I thought I could convert the string to uint64 explicitly with get<...>() – DocDriven Jun 01 '21 at 17:28

1 Answers1

0

What version are you using? Because I got next output on latest version:

value: 10
also matching numeric
value: null
terminate called after throwing an instance of 'nlohmann::detail::type_error'
  what():  [json.exception.type_error.302] type must be number, but is null
Aborted (core dumped)
  • Also latest version correct work with `it["id"] == i` – Maxim Eryomenko Jun 01 '21 at 17:49
  • sorry, in my case, i remove quotes around id's value, but if return quotes, got `what(): [json.exception.type_error.302] type must be number, but is string`. I agree with @Tim Randall – Maxim Eryomenko Jun 01 '21 at 18:10
  • So based on all of your comments, I conclude that it is not intended to convert a "json-string" (that being a value encased in double quotes in the file) to a numeric value, as it is assumed that numeric values are already saved without quotes. If that is the case, my question is answered. Is this correct? (also aimed @TimRandall) – DocDriven Jun 01 '21 at 19:16
  • I test your case with [this patch](https://gist.github.com/moeryomenko/68d659bc1537fdf9e11259277e9fc522), it's work very well – Maxim Eryomenko Jun 02 '21 at 10:56