Skip to content

Getting nlohmann::detail::parse_error on JSON generated by nlohmann::json not sure why #1583

@looopTools

Description

@looopTools

Hey, I have a JSON file with a configuration stored, which is just a dump of an object I have previously made with nlohmann::json. The content look like this

{"base_out_path":"/home/tools/minerva_out/.basis/","base_register_path":"/home/tools/minerva_out/.basis_registres/","max_registry_size":1073741824,"out_path":"/home/tools/minerva_out","register_path":"/home/tools/minerva_out/.identifiers"}

and pretty

{
   "base_out_path":"/home/tools/minerva_out/.basis/",
   "base_register_path":"/home/tools/minerva_out/.basis_registres/",
   "max_registry_size":1073741824,
   "out_path":"/home/tools/minerva_out",
   "register_path":"/home/tools/minerva_out/.identifiers"
}```

However, when I try to parse the data, I get this error: 
```bash
terminate called after throwing an instance of 'nlohmann::detail::parse_error'
  what():  [json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal
Aborted (core dumped)

This is how I read the file

    inline nlohmann::json json_reader(const std::string& file_path)
    {
        nlohmann::json json_data;

        std::ifstream input(file_path);

        if(input.fail())
        {
            throw std::runtime_error("Unable to open file " + file_path);
        }
    
        input >> json_data;
        return json_data;
}

This is how I write the file

    inline void json_writer(std::string path, nlohmann::json data)
    {
        std::ofstream json_out(path);
        json_out << data;
        json_out.close();
}

And this is the code where it fails:

    int main(void)
    {
        ...
        nlohmann::json minerva_config = tartarus::readers::json_reader((homedir() + "/.minerva" + "/config.json")); 
         minerva::minerva storage(minerva_config);
         ...
    }
    minerva::minerva(const nlohmann::json& config)
    {

        
        m_base_out_path = config["base_out_path"].get<std::string>();
        m_base_registry_path = config["base_register_path"].get<std::string>();
        m_out_path = config["out_path"].get<std::string>();
        m_registry_path = config["register_path"].get<std::string>();
        size_t registry_size = config["max_registry_size"].get<size_t>();

        m_registry = model::registry(m_registry_path, m_base_registry_path, registry_size);
       ...
    }

I have not had problems with the read and write method before, so any idea as to why it fails?

I use clang 7 as my compiler

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions