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

In compatible JSON object from nlohmann::json to nohman::json - unexpected end of input; expected '[', '{', or a literal #1603

Closed
looopTools opened this issue May 17, 2019 · 9 comments
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@looopTools
Copy link

looopTools commented May 17, 2019

Hey I am trying to create a configuration using nlohmann::json, but for some reason I get:

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)

I have create a JSON object with nlohmann::json like this

    nlohmann::json minerva_config;

    minerva_config["register_path"] = (USER_HOME + minervafs_root_folder + minervafs_identifier_register); // string
    minerva_config["base_register_path"] = (USER_HOME + minervafs_root_folder + minervafs_registry);       // string  
    minerva_config["base_out_path"] = (USER_HOME + minervafs_root_folder + minervafs_basis_folder);        // string
    minerva_config["out_path"] = (USER_HOME + minervafs_root_folder + "/");                                // string
    minerva_config["max_registry_size"] = 8589934592; // 8 GB of RAM

    tartarus::writers::json_writer(config_file_path, minerva_config);
    minerva_storage = minerva::minerva(minerva_config);      

The json_writer does not fail, what fails is the construct on the next line. Which gives the error presented above. I have tried to remove the writer call but no dice. The code for the constructor is like this:

   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>();
        ...
    }

Where it it interact with the nlohmann::json object. As I am within the same application I wouldn't assume system char encoding or something like that is a problem.
I tried to dump the json object and passed it in a JSON validator, the dump is as follows:

{"base_out_path":"/home/tools/.minervafs/.basis","base_register_path":"/home/tools/.minervafs/.registry","max_registry_size":8589934592,"out_path":"/home/tools/.minervafs/","register_path":"/home/tools/.minervafs/.identifiers"}

Any ideas as to what might be wrong?

@nickaein
Copy link
Contributor

Is this related to #1583?

@looopTools
Copy link
Author

@nickaein no tried that already. The problem in the issue you reference was for writing JSON and then reading it again, and the issue was due to mis-match in character encoding. Which from what I can tell is not the case this time.

@nickaein
Copy link
Contributor

Is the file accessible? Try with its absolute path.

BTW, I don't get any error for this program (input.json contains the JSON dump you have provided):

#include "json.hpp"
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    try
    {
        nlohmann::json j;
        std::ifstream fs("input.json");
        if(!fs)
        {
            std::cout << "Failed to open file" << endl;
            return 0;
        }

        fs >> j;
        std::cout << "Success." << endl;
        std::cout << "Raw content:" << endl;
        std::cout << j.dump(4) << endl;

        std::cout << "Retrieved configs:" << endl;
        std::cout << "\tbase_out_path: " << j["base_out_path"].get<std::string>() << endl;
        std::cout << "\tbase_register_path: " << j["base_register_path"].get<std::string>() << endl;
        std::cout << "\tout_path: " << j["out_path"].get<std::string>() << endl;
        std::cout << "\tregister_path: " << j["register_path"].get<std::string>() << endl;
        std::cout << "\tmax_registry_size: " << j["max_registry_size"].get<size_t>() << endl;
    }
    catch(std::exception& e)
    {
        std::cout << "Exception:" << endl;
        std::cout << e.what() << endl;
        return 0;
    }

    return 0;
}

It might help to debug the error. For me, it outputs the following as expected:

Success.
Raw content:
{
    "base_out_path": "/home/tools/.minervafs/.basis",
    "base_register_path": "/home/tools/.minervafs/.registry",
    "max_registry_size": 8589934592,
    "out_path": "/home/tools/.minervafs/",
    "register_path": "/home/tools/.minervafs/.identifiers"
}
Retrieved configs:
	base_out_path: /home/tools/.minervafs/.basis
	base_register_path: /home/tools/.minervafs/.registry
	out_path: /home/tools/.minervafs/
	register_path: /home/tools/.minervafs/.identifiers
	max_registry_size: 8589934592

@looopTools
Copy link
Author

looopTools commented May 17, 2019

I triede you code above and it works.

Now I tried writing it to disk as well and reading into a new object also gives and error

and again this is not an issue with reading the file. This is an issue when parsing around the object

@looopTools
Copy link
Author

Okay, I have actually gotten a bit further if the first string:

minerva_config["register_path"] = (USER_HOME + minervafs_root_folder + minervafs_identifier_register);

resolves to "/home/tools/.minervafs/.identifiers" then the interperter will throw the error. However if I remove either of the periods, so the string is either: "/home/tools/minervafs/.identifiers" or "/home/tools/.minervafs/identifiers" then the parses passes no problem.

What is this wizardry ???

identifiers

@nickaein
Copy link
Contributor

That's weird. Can you provide a minimal working code that demonstrates this error?

Also, are you using the same file handler for writing and reading the config file? If so, you might need to rewind it (e.g. ifstream::seekg) to start reading from the beginning.

@looopTools
Copy link
Author

@nickaein there is no file invovled this is just for the nlohmann::json object :)

@nickaein
Copy link
Contributor

I see, but the exception in #1603 (comment) usually is thrown when parsing, whether it is parsing the file stream or an string. Are you still getting this exception? For instance, passing an empty string to json::parse() raises the exact same exception.

One other case that this type of exception can be thrown is during the construction of a json_pointer (e.g. this and this). While your strings have the resemblance of JSON Pointers, they are considered as JSON Pointer only if you explicitly ask for it. Also, the error code parse_error.101 in the exception doesn't seems to be related to JSON Pointers.

A minimal code to demonstrate the problem you are having would be very helpful!

@stale
Copy link

stale bot commented Jun 16, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jun 16, 2019
@stale stale bot closed this as completed Jun 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

2 participants