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

"type must be string, but is " std::string(j.type_name() #1206

Closed
weirdbeardgame opened this issue Aug 21, 2018 · 21 comments
Closed

"type must be string, but is " std::string(j.type_name() #1206

weirdbeardgame opened this issue Aug 21, 2018 · 21 comments

Comments

@weirdbeardgame
Copy link

  • What is the issue you have? JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

Create a JSON file with multiple objects and attempt to iterate with ` assert(baddie.is_array());

for (auto& json : jsonStream)
{
      name = json["Name"].get<std::string>();
   }`
  • What is the expected behavior?
    The file is properly read and parsed.

  • And what is the actual behavior instead?
    it gives the error above and an exception Unhandled exception at 0x00007FFF278EB098 in rpg.exe: Microsoft C++ exception: nlohmann::detail::type_error at memory location 0x000000268ECFF168. occurred

Visual Studio 2017

  • Did you use a released version of the library or the version from the develop branch?
    From release.

Also this is for an entirely different project the solution for the question I posted worked perfectly.

@gregmarr
Copy link
Contributor

Can you show your JSON file and all the code from read to the exception?

@theodelrieu
Copy link
Contributor

I assume that baddie is a data member, is it default initialized in the header file?

If not, then default constructed json values are null, which causes the assertion.

@gregmarr
Copy link
Contributor

@kenshen112 There is no json library code in there.

@weirdbeardgame
Copy link
Author

@gregmarr The createEnemy and init function is the JSON libaray code

@theodelrieu By initalized do you mean set to NULL or the JSON library equivilent? IE. JSON J = NULL or nullptr.

@gregmarr
Copy link
Contributor

Ah, I see, the baddie is a json object that is defined in the class definition in another file.
Looks like your problem is that you're accessing "Attack" and "HP" without checking that they exist, and they don't exist for the second object. You either need to check that they exist, or use the value() function that takes a default value.

@weirdbeardgame
Copy link
Author

One last question it's giving me a Syntax error where there should be none "syntax error - unexpected end of input; expected '[', '{', or a literal" right after the red Dragon in my JSON file I've tried editing it in various ways to make it read correctly.

@gregmarr
Copy link
Contributor

I would start by removing all whitespace from the file. You might have an invisible control character somewhere.

@weirdbeardgame
Copy link
Author

weirdbeardgame commented Aug 21, 2018

Didn't work (Quick Edit) Turns out it is reading the entire file, I tried printing out the Name variable and all three appear so the error must be referring to the end of the file.

@nlohmann
Copy link
Owner

@kenshen112 I am confused. Did #1206 (comment) solve your issue or is there anything left open?

@weirdbeardgame
Copy link
Author

@nlohmann It did but it brought another issue that I have yet to solve that i asked shortly after that

@nlohmann
Copy link
Owner

You mean the parse error? Could you share the JSON text and the code with which you parse it?

@nlohmann
Copy link
Owner

What is the exact error message?

@weirdbeardgame
Copy link
Author

JSON_THROW(parse_error::create(101, m_lexer.get_position(), error_msg));

error_msg | "syntax error - unexpected end of input; expected '[', '{', or a literal" | std::basic_string<char,std::char_traits<char>,std::allocator<char> >

That said I can see the name var printing out in the console perfectly for all three and no matter the adjustments I make to the file the error never vanishes not that it'd matter the file itself is valid

@nlohmann
Copy link
Owner

The exception should contain a position of the error.

@weirdbeardgame
Copy link
Author

Unhandled exception at 0x00007FFD8F3EB098 in rpg.exe: Microsoft C++ exception: nlohmann::detail::parse_error at memory location 0x000000F1CB6FE968. occurred

@nlohmann
Copy link
Owner

Can you parse the file in like this:

std::ifstream f("yourfile.json);
json j = json::parse(f);

?

@weirdbeardgame
Copy link
Author

It gave me the same error but this time it didn't print the name data like it was before

@nlohmann
Copy link
Owner

Are you sure you opened the file correctly? And all parse errors contain the position like

[json.exception.parse_error.101] parse error at 8: syntax error - unexpected ']'; expected '[', '{', or a literal
exception id: 101

If you have a parse error at 0 or parse error at 1 then this means that even reading the first byte fails.

@weirdbeardgame
Copy link
Author

Got it figured out, had to change the design of the parsing function it was indeed the while loop that was messing it all up thanks for the help!

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

4 participants