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

Serializing vector #1085

Closed
yuvalyo opened this issue May 12, 2018 · 10 comments
Closed

Serializing vector #1085

yuvalyo opened this issue May 12, 2018 · 10 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@yuvalyo
Copy link

yuvalyo commented May 12, 2018

at this moment i'm working on a little project and i'm trying to serialize a vector

the current code is:

std::string JsonResponsePacketSerializer::serializeResponse(HighscoreResponse response)
{
	json j;
	j["status"] = response.status;
	j["highscores"] = {};
	for (auto iter = response.highscores.begin(); iter != response.highscores.end(); ++iter)
	{
		j["highscores"].push_back(*iter);
	}


	return j.dump();
}

as you can see in the struct the "highscores" is a vector
but i'm gettting all kind of erros such as:

  1. could not find to_json() method in T's namespace
    2)forcing MSVC stacktrace to show which T we're talking about.

thanks for your help 👍

@nlohmann
Copy link
Owner

Please have a look at this section of the README: https://github.com/nlohmann/json/blob/develop/README.md#arbitrary-types-conversions

@yuvalyo
Copy link
Author

yuvalyo commented May 12, 2018

so you say i just need to do like:

json j;
j["highscores"] = response.highscores;

?

@nlohmann
Copy link
Owner

You need to provide a to_json function.

@yuvalyo
Copy link
Author

yuvalyo commented May 12, 2018

std::string JsonResponsePacketSerializer::serializeResponse(HighscoreResponse response)
{
	json j = json{ { "status", response.status },{ "highscores", response.highscores }};
	return j.dump();
}

still does not work

@nlohmann
Copy link
Owner

Can you provide the code of your to_json function?

@yuvalyo
Copy link
Author

yuvalyo commented May 12, 2018

void to_json(json& j, const HighscoreResponse& response)
{
	j = json{ { "status", response.status },{ "highscores", response.highscores }};
}

this is my to_json function but i need to know just hot to serialize the vector because from what i understood you can only convert normal types and not user_defined

@theodelrieu
Copy link
Contributor

Is the to_json function in the same namespace than HighscoreResponse?

If so, it means that some member of HighscoreResponse is a user defined type which also need a to_json function

@yuvalyo
Copy link
Author

yuvalyo commented May 12, 2018

i just need to know how do you convert a std::vector to json

@nlohmann
Copy link
Owner

Given std::vector<Foo>, then it is sufficient to provide a to_json function for type Foo - the library then knows how to process a vector of Foo.

@nlohmann
Copy link
Owner

Do you need further assistance on this?

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label May 27, 2018
@nlohmann nlohmann closed this as completed Jun 3, 2018
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