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 emit a JSON object? #27

Closed
haitaoxin opened this issue Jun 21, 2015 · 8 comments
Closed

How to emit a JSON object? #27

haitaoxin opened this issue Jun 21, 2015 · 8 comments

Comments

@haitaoxin
Copy link

Hello,
I noticed that in \test\sio_test.cpp, nlohmann mentioned that JSON will not compile on Windows:

ifndef _WIN32

include "json.hpp" //nlohmann::json cannot build in MSVC

endif

Does it mean that I cann't emit a JSON object from Win32 client? Here is my object
{
name: 'Joe',
age: 25,
car: {
photo: ,
year: 2005
}
}
If I can, could you give a simple example?

Thanks a lot!

@melode11
Copy link
Contributor

This macro is just for unit-test.
However there's indeed no direct interfaces receiving JSON object because this lib is not going to be depended on any JSON libraries at interface level.
The correct way is to convert your json object/array to object_message or array_message.

@haitaoxin
Copy link
Author

melode11, thank you for the answer. However, I still don't know how to send an object_message. The constructor of this class takes empty input, and it only has get_map() method, so "set_map()", so how can I input a set of key:value pair?

I spent some time to debug the code, still could not find a workaround to send a JSON object (instead of a string in quotations). Could you please give an example of how to do that?

@melode11
Copy link
Contributor

get_map gives the internal map reference, you can set any string - message key-value pair into it.
or just

object_map_ptr->get_map() = your_map;

@melode11
Copy link
Contributor

suppose you got a json:

{
   name:'Bob',
   friends:['Tom','Jim']
}

you need to concrete a object_message with below structure:

object_message:
           |__name(string): bob(string_message)
           |__friends(string):array_message:Tom(string_message), Jim(string_message)

Do I explained clearly?

@haitaoxin
Copy link
Author

Yes, I followed your suggestion and it works. Thanks again!

@gallgall
Copy link

Please post a clearly example about this. I really really need it. Thanks a lot!

@zhyq0520
Copy link

@melode11
Hi, how will it be able to communicate with sock.io(js version) in this way?

@liwuking
Copy link

@haitaoxin can you post a clearly example. I really really need it. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants