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

How to send json data over the wire? #2185

Closed
shrikky opened this issue Jun 11, 2020 · 3 comments
Closed

How to send json data over the wire? #2185

shrikky opened this issue Jun 11, 2020 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@shrikky
Copy link

shrikky commented Jun 11, 2020

I have a client that is trying to send the following json data :

On the client side
nlohmann::json j2 =
{
{"command", "Target Offset"},
{"args", {"1", "0", "2"}},
};

I create a string str = j2.dump();
I send str over the network to the server

On the server side

I am able to recv the data as string, but how do I convert the string back to json?

nlohmann::json j(read_msg_.body()); this line fails

I have to and from json written

void from_json(const json & j, NetworkPacket& p)
{
p.command = j.value("command", "");

 if (j.contains("args"))
	 p.arguments = j.value("args", p.arguments);

}

void to_json(json & j, const NetworkPacket & p)
{
j = json{ {"command",p.command},
"args",p.arguments };
}

and my NetworkPacket class looks like this

struct NetworkPacket
{
public:
string command;
vector arguments;
};

Please help, how do I send data from client to server over the network.

@nlohmann
Copy link
Owner

You need to call json::parse to create a JSON value back from the string.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 11, 2020
@nlohmann
Copy link
Owner

Do you need further assistance with this issue?

@shrikky
Copy link
Author

shrikky commented Jun 19, 2020

Thank you this worked, I remember trying this at first, maybe I did something mistake. But thanks for helping me out

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

2 participants