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

could not find to_json() method in T's namespace #1140

Closed
AistisT opened this issue Jun 20, 2018 · 7 comments
Closed

could not find to_json() method in T's namespace #1140

AistisT opened this issue Jun 20, 2018 · 7 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@AistisT
Copy link

AistisT commented Jun 20, 2018

nlohmann::json subscriber;	
subscriber["Name"] = &callerName;
subscriber["SubscribeTopic"] = topicToSubscribe;
subscriber["CallBackTopic"] = callBackTopic;
	

and

nlohmann::json subscriber = {
   	{ "Name", &callerName },
   	{ "SubscribeTopic", &topicToSubscribe },
   	{ "CallBackTopic", &callBackTopic },
   };

Both cause the error

Vs 2017, latest release

I seem to be unable to create json object, I've read through documentation like 20 times and there is nothing else that has been mentioned about it.

@nlohmann
Copy link
Owner

The error message indicates that you try to convert a type to JSON that the library does not know. In that case, it searches for a to_json method on the type's namespace, see https://github.com/nlohmann/json#arbitrary-types-conversions.

In your case, the problem seems to be line

subscriber["Name"] = &callerName;

What is the type of &callerName?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Jun 20, 2018
@AistisT
Copy link
Author

AistisT commented Jun 20, 2018

std::string pointer, I realized that I have been trying to access it by reference, that was causing the issue, cheers for pointing my attention to it!

@nlohmann
Copy link
Owner

Cool! Does it work now?

@AistisT
Copy link
Author

AistisT commented Jun 20, 2018

It does work now, but how should I deal with Unicode characters? Is it possible to use std::wstring ? I would really like to avoid having to get another library and trying to figure how to encode strings in utf-8

@nlohmann nlohmann removed the state: needs more info the author of the issue needs to provide more details label Jun 20, 2018
@AistisT
Copy link
Author

AistisT commented Jun 20, 2018

Nvm seems c++11 introduced easy way to encode strings to utf-8

std::string msg = u8" महसुस Aistis message €";

Feel free to close this now! 👍

@OvermindDL1
Copy link

Nvm seems c++11 introduced easy way to encode strings to utf-8

That still depends on the compiler encoding the source text properly however.

@nlohmann
Copy link
Owner

Indeed the library only supports UTF-8, though you may parse from std::wstring (in the latest develop version). But @OvermindDL1 is right - your compiler must make sure that string literals are actually encoded with UTF-8.

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

No branches or pull requests

3 participants