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

Parse throwing errors #1149

Closed
AistisT opened this issue Jun 27, 2018 · 8 comments
Closed

Parse throwing errors #1149

AistisT opened this issue Jun 27, 2018 · 8 comments
Labels
platform: visual studio related to MSVC solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@AistisT
Copy link

AistisT commented Jun 27, 2018

So parse seem to work fine, if I load dll from my tester project, everything works fine, if I load exactly same dll with same data through windows services I get lots of errors like "Unhandled exception at 0x75000132 in xxxxxxx.exe: Microsoft C++ exception: nlohmann::detail::parse_error at memory location 0x0293A868. occurred"

This is triggered by

bool parse_error(std::size_t, const std::string&,
				const detail::exception& ex) override
			{
				errored = true;
				if (allow_exceptions)
				{
					// determine the proper exception type from the id
					switch ((ex.id / 100) % 100)
					{
					case 1:
						JSON_THROW(*reinterpret_cast<const detail::parse_error*>(&ex));
					case 2:
						JSON_THROW(*reinterpret_cast<const detail::invalid_iterator*>(&ex));  // LCOV_EXCL_LINE
					case 3:
						JSON_THROW(*reinterpret_cast<const detail::type_error*>(&ex));  // LCOV_EXCL_LINE
					case 4:
						JSON_THROW(*reinterpret_cast<const detail::out_of_range*>(&ex));
					case 5:
						JSON_THROW(*reinterpret_cast<const detail::other_error*>(&ex));  // LCOV_EXCL_LINE
					default:
						assert(false);  // LCOV_EXCL_LINE
					}
				}
				return false;
			}

at line 3624 of json.hpp, I am using hardcoded data defined right in dll at this moment, to rule out any memory issues, but it's not helping. It just dies when it's trying to parse std::wstring.

	static int SendToAgent(std::wstring & data, LPCWSTR LogType)
	{
		
		if (mqttHandlerGlobal.IsAgentOnline())
		{	
			//_asm {int 3}
			auto string = std::wstring(LogType);
			auto datatype = nlohmann::json::parse(string);
			auto parsedData = nlohmann::json::parse(data);
			nlohmann::json messageJson =
			{
				{ "DataType", datatype },
				{ "Data", parsedData },
			};
			printf("Message Sent\n");
			auto message = messageJson.dump();
			return mqttHandlerGlobal.MqttSend(message, *mqttHandlerGlobal.GetTopic());
		}
		return 0;
	}

called by


std::wstring test = L"Test";
SendToAgent(test, L"TestType");

I've really got no clue what's causing it, apart that DLL with parsing code is being loaded by a different project that runs under windows service its exactly the same.

@AistisT
Copy link
Author

AistisT commented Jun 27, 2018

this is with latest dev version

@nlohmann nlohmann added the platform: visual studio related to MSVC label Jun 28, 2018
@nlohmann
Copy link
Owner

When you pass Test as data to SendToAgent and there call json::parse(data), then a parse error should occur, because Test is not valid JSON.

@AistisT
Copy link
Author

AistisT commented Jun 28, 2018

but it works if I run the same in tester loader, and couple days ago you told me that I should parse wstring for it to be accepted by json constructor. Is Parse just for parsing valid json in string format then? What about iterator parse, how does that work? I'm a little confused.

How should I go about constructing a json when my data is in std::wstring

@nlohmann
Copy link
Owner

parse indeed expects a valid JSON text and throws an exception in case of syntax errors.

std::wstring is currently only supported as source for the parser; that is, you can pass a std::wstring to parse. There is currently no support to use std::wstring in any other parts of the library. That is, you can not do something like:

std::wstring my_string = "...";
json j;
j["key"] = my_string;  // <--- this does not work

@AistisT
Copy link
Author

AistisT commented Jun 28, 2018

I swear that worked for me before, but now I can reproduce it.
May I ask what's the reasoning for not supporting wstring? that seems limiting real life usage quite a lot. It should produce wstring json, after all json is just a string that's formatted in a json schema

@nlohmann
Copy link
Owner

The library uses UTF-8 internally. Storing std::wstring would mean a recoding. This is a lot of work and was not approached so far.

@nlohmann
Copy link
Owner

As the original problem is solved, can I close this issue?

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 28, 2018
@AistisT
Copy link
Author

AistisT commented Jun 28, 2018

I've solved it by converting wstring to utf8 string, so in a way its solved I suppose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform: visual studio related to MSVC 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