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

Can't use ifstream and .clear() #1321

Closed
VignRaph opened this issue Oct 25, 2018 · 3 comments
Closed

Can't use ifstream and .clear() #1321

VignRaph opened this issue Oct 25, 2018 · 3 comments

Comments

@VignRaph
Copy link

My code stops with error:
Exception non gérée à 0x00007FFE1BC6A388 dans Debug.exe : exception Microsoft C++ : nlohmann::detail::parse_error à l'emplacement de mémoire 0x00000089E48FCB38.

My code can be represenseted as following :

#include <iostream>
#include <fstream>
#include "json/single_include/nlohmann/json.hpp"

int main()
{
    std::ifstream file1("1.json"),
        file2("2.json");
    nlohmann::json JsonObject;

    std::cout << JsonObject;         //outputs :   null

    file1 >> JsonObject;               //works fine
    
    //some code

    JsonObject.clear();
    std::cout << JsonObject;       //outputs :   {}

    file2 >> JsonObject;                //error mentionned higher
}

I think it as to do with the fact that .clear() leaves the json object as {} instead of null

I have VisualStudio Community 2017 on Windows 10 and nlohmann json version 3.3.0

@gregmarr
Copy link
Contributor

The clear() function resets the value but keeps the current type. If you're reading new, you should reset it as if it were default constructed:

JsonObject = nlohmann::json{};

Do you get the same error if you reverse the order of reading the files? Have you validated that 2.json is a valid JSON file?

@nlohmann
Copy link
Owner

@gregmarr is right. Here is the documentation of clear.

And if you encounter a parse_error, it's description should contain an indication about the reasons parsing valued, e.g. "unexpected end of file".

@VignRaph
Copy link
Author

Thanks for your response, it worked just fine using :

JsonObject = nlohmann::json{};

and it actually returned parse_error à l'emplacement de mémoire 0x0000000C27AFC9D8 at the same time as the error

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

No branches or pull requests

3 participants