Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
looopTools opened this issue Apr 25, 2019 · 2 comments

Comments

@looopTools
Copy link

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

@nickaein
Copy link
Contributor

This is similar to #1484. Can you do the tests discussed in the comments (#1484 (comment), #1484 (comment), #1484 (comment))? You already have done some of them. The rest might help us to understand what is causing this problem.

@looopTools
Copy link
Author

Sorry for slow response, haven't had internet.
I seems to be due to encoding of the file. The system for some reason used ISO-SOMETHING. I have swapped the system encoding to UTF-8 and it works.

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

No branches or pull requests

2 participants