mardi 28 août 2018

nlohmann json version 3.2.0, runtime error

I have several json files from a game I am modding that I want to be able to process using an external application.

as far as I can tell this code is within the specifications but I get a runtime error:

terminate called after throwing an instance of 'nlohmann::detail::type_error'
what():  [json.exception.type_error.304] cannot use at() with string

this is the stripped code to reproduce the error:

#include "json.hpp"
using json = nlohmann::json;
using namespace std;

namespace ns
{
class info
{
public:
    std::string id;
};
void to_json(json& j, const info& mi);
void from_json(const json& j, info& mi);
}

int main()
{
    json j ="[{\"id\": \"identifier\"}]";

    ns::info info = j;
    return 0;
}

void ns::to_json(json& j, const ns::info& mi)
{
    j = json{
        {"id",mi.id},
    };
}
void ns::from_json(const json& j, ns::info& mi)
{
    mi.id = j.at("id").get<std::string>();
}

and here is the compiler output:

-------------- Build: Debug in jsontest (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -std=c++11 -fexceptions -g  -c C:\Users\UserOne\Documents\c++\jsontest\main.cpp -o obj\Debug\main.o
mingw32-g++.exe  -o bin\Debug\jsontest.exe obj\Debug\main.o   
Output file is bin\Debug\jsontest.exe with size 2.82 MB
Process terminated with status 0 (0 minute(s), 3 second(s))
0 error(s), 0 warning(s) (0 minute(s), 3 second(s))

Aucun commentaire:

Enregistrer un commentaire