-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Consuming TCP Stream #1080
Comments
Did you find a solution? |
I didn't I accidentally closed this without noticing. What would you recommend? Right now I have a tcp stream coming into a char[ ] buffer |
Could you describe your program a bit more? What is the structure you are parsing? How does it look like when you call the parser? How would further data be added? |
Long story short the server is continually sending JSON over TCP, which does not guarantee everything gets sent as whole. The buffer contains the sent json message parts and appends them to a string (std::string). I'd like the client to be able to parse the json on the string and delete things it has parsed. For example here's the receive code block from the TCP stream. while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0) {
} On a console test program I am able to parse my test strings appropriately. For example:
json j;
std::cin >> j;
parseMe::WiFi w {
j["ssid"].get<std::string>(),
j["power"].get<int>(),
j["channel"].get<int>()
};
std::cout << w.ssid <<" " << w.power << " " << w.channel << std::endl;
namespace parseMe {
struct WiFi {
std::string ssid;
int power;
int channel;
};
} Ignore that last edit I made. |
Are you receiving back-to-back json ("{...} {...} {....}") and you need to pull them apart, are you getting one very-large json ("[a,b,c,........]") and you want to consume "a" before you get the closing ']'? |
Back to back json exactly. Right now what I implemented is a size byte in between the json structures. Read size, buffer up a single json statement, parse, delete from buffer, rinse and repeat. It works well so far but I'm wondering if there's cleaner solutions. |
I actually did this a couple years ago but used the "json11" library:
I don't think nlohmann::json currently does this, but it probably would not be hard to add a form of parse that:
|
I've managed to get it working myself, I'm passing multiple different types of JSON structures and converting them to their arbitrary types. All is well |
@DOGFIVE Could you please share your solution? |
I also faced this problem and found out that there is only one JSON parser suitable for this task: https://github.com/lloyd/yajl It allows partial input and multiple root objects. |
Too old thread, but same issue here today ... @mistal-distal the solution that worked for you was that shared by @jaredgrubb ? |
This is a question not an issue. I saw various issues back from this time last year about streams. Would the stream parser be able to parse/consume a TCP stream's buffer?
For example, it parses to the end of the buffer and the completing JSON isn't queued yet so it can rewind on buffer when new data has been queued.
The text was updated successfully, but these errors were encountered: