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

App crashes when dump method called for non ascii chars. #2126

Closed
fortunecoders opened this issue May 19, 2020 · 6 comments
Closed

App crashes when dump method called for non ascii chars. #2126

fortunecoders opened this issue May 19, 2020 · 6 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@fortunecoders
Copy link

`#include "nlohmann\json.hpp"
#include
#include

using nlohmann::json;

int main()
{
std::string s = "Casa De Los Niños, Inc."; // Sample string with non ascii chars at 15th location 1 based index.

//json second = json::parse(s); -- crashes here.
json second = s; // works

std::string s2 = (second.dump()); //// Crashes here

}
`
Project is setup with MBCS Character set.

@nlohmann
Copy link
Owner

What is the exact error message?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label May 20, 2020
@fortunecoders
Copy link
Author

Unhandled exception at 0x775A2552 in nlohmannsample.exe: Microsoft C++ exception: nlohmann::detail::type_error at memory location 0x010FEEA4. occurred.

Attached is the sample app to try. It is a VS2019 solution

jsonsample.zip

@nlohmann
Copy link
Owner

Can you provide the what() string from the exception and a stack trace?

@fortunecoders
Copy link
Author

Call Stack:
KernelBase.dll!775a2552()
KernelBase.dll![Frames below may be incorrect and/or missing, no symbols loaded for KernelBase.dll]
[External Code]
nlohmannsample.exe!nlohmann::detail::serializer<nlohmann::basic_json<std::map,std::vector,std::string,bool,__int64,unsigned __int64,double,std::allocator,nlohmann::adl_serializer>>::dump_escaped(const std::string & s, const bool ensure_ascii) Line 14169
at d:\jsonsample\nlohmannsample\packages\nlohmann.json.3.7.3\build\native\include\nlohmann\json.hpp(14169)
nlohmannsample.exe!nlohmann::detail::serializer<nlohmann::basic_json<std::map,std::vector,std::string,bool,__int64,unsigned __int64,double,std::allocator,nlohmann::adl_serializer>>::dump(const nlohmann::basic_json<std::map,std::vector,std::string,bool,__int64,unsigned __int64,double,std::allocator,nlohmann::adl_serializer> & val, const bool pretty_print, const bool ensure_ascii, const unsigned int indent_step, const unsigned int current_indent) Line 13980
at d:\jsonsample\nlohmannsample\packages\nlohmann.json.3.7.3\build\native\include\nlohmann\json.hpp(13980)
nlohmannsample.exe!nlohmann::basic_json<std::map,std::vector,std::string,bool,__int64,unsigned __int64,double,std::allocator,nlohmann::adl_serializer>::dump(const int indent, const char indent_char, const bool ensure_ascii, const nlohmann::detail::error_handler_t error_handler) Line 16577
at d:\jsonsample\nlohmannsample\packages\nlohmann.json.3.7.3\build\native\include\nlohmann\json.hpp(16577)
nlohmannsample.exe!main() Line 44
at d:\jsonsample\nlohmannsample\nlohmannsample.cpp(44)
[External Code]

This same thing can be reproduced with the attached sample source code.

@nlohmann
Copy link
Owner

When I compile the code, I get the following warning:

nlohmannsample.cpp:27:36: warning: illegal character encoding in string literal
      [-Winvalid-source-encoding]
    json j_string = "Casa De Los Ni<F1>os, Inc.";
                                   ^~~~
nlohmannsample.cpp:49:23: warning: illegal character encoding in string literal
      [-Winvalid-source-encoding]
    json j_invalid = "<E4>\xA9<FC>";
                      ^~~~
nlohmannsample.cpp:49:28: warning: illegal character encoding in string literal
      [-Winvalid-source-encoding]
    json j_invalid = "<E4>\xA9<FC>";
                              ^~~~

At runtime, I get the following exception:

[json.exception.type_error.316] invalid UTF-8 byte at index 15: 0x6F

See https://github.com/nlohmann/json#character-encoding - this library only supports UTF-8 and will throw an exception when trying to serialize data that is not UTF-8 encoded.

When I save the file as UTF-8, there is no compilation warning and the string "Casa De Los Niños, Inc." is correctly printed.

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed state: needs more info the author of the issue needs to provide more details labels May 20, 2020
@fortunecoders
Copy link
Author

Thanks a lot..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants