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

accept and parse function not work well with a pure number string #3029

Closed
WYHuestc opened this issue Sep 25, 2021 · 2 comments
Closed

accept and parse function not work well with a pure number string #3029

WYHuestc opened this issue Sep 25, 2021 · 2 comments
Labels
solution: invalid the issue is not related to the library

Comments

@WYHuestc
Copy link

WYHuestc commented Sep 25, 2021

When parse and accept a string, if the string is pure number string like "123"
accept will return true(it looks a error result)
parse will not throw a exception(I think it is wrong, It should throw a expection)
after parse we use this object to emplace it throw a expection json.exception.type_error.311

Example code:

using namespace nlohmann;
int main(){
    auto j = json::array();
    std::string my_input = "123456";
    if (json::accept(my_input)){
        std::cout << "it is a json" << std::endl;
    } else {
        std::cout << "it is not a json" << std::endl;
    }
    try
    {
        j = json::parse(my_input, nullptr, false);
        std::cout << "is_discarded: " << (j.is_discarded() ? "true" : "false") << std::endl;
        std::cout << "j.dump(): " << j.dump() << endl;
    }
    catch (json::exception& ex)
    {
        std::cout << "parse error reason: " << ex.what() << std::endl;
    }
    try {
        j.emplace("123","23");
    } catch (json::exception& ex) {
        std::cout << "emplace error reason: " << ex.what() << std::endl;
    }
    return 0;
}

The result output is :
it is a json
is_discarded: false
j.dump(): 123456
emplace error reason: [json.exception.type_error.311] cannot use emplace() with number

nlohmann::json version : 3.10.2
gcc version:
Apple clang version 13.0.0 (clang-1300.0.29.3)
Target: x86_64-apple-darwin20.6.0
Thread model: posix

@nlohmann
Copy link
Owner

This sounds reasonable:

@nlohmann nlohmann added solution: invalid the issue is not related to the library and removed kind: bug labels Sep 25, 2021
@WYHuestc
Copy link
Author

Thx for help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

2 participants