-
-
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
Question: parsing void *
#655
Comments
Are you sure that the memory pointed to by the void pointer is really a sequence of characters of the proper length? |
I suspect that the |
Can you try |
Give me an hour and I will post here |
(sry about that... meetings...) I have tried the above but instead of using
This is the feature I propose |
Sorry, the second parameter has to be another pointer. This would be an example: #include <iostream>
#include "json.hpp"
using json = nlohmann::json;
int main() {
const char* text = "{\"foo\": 12}";
void *input = (void*)text;
json j = json::parse((char*)input, ((char*)input) + strlen((char*)input));
std::cout << j << std::endl;
} |
Perfect! I completely missed that ...
Works nicely - I think this can close. Thanks a lot for your help! |
Thanks for reporting back! |
Sorry for commenting in a closed issue. May I know where in the API is documented this signature used above?: json::parse((char*)begin, (char*)end); This is exactly what I was looking for (I receive a continuos stream via TCP and I do know the JSON string message boundaries, so I know the
BTW I don't know what "C-style arrays can be used with std::begin()/std::end()" means. |
This would be a special case of an iterator range, which is not documented. :-/ For a #include "json.hpp"
#include <iostream>
using json = nlohmann::json;
int main() {
char input[] = {'t', 'r', 'u', 'e'};
json j = json::parse(std::begin(input), std::end(input));
std::cout << j << std::endl;
} Output: I shall update the documentation together with #1405. |
Thanks. As a side note, I think that parsing from a stream or a specific range in a char array should not be a "special case". At least in my experience this is a very common usage :) |
I don't mean it is a special case at per se - it's rather a special case for an iterator range... |
ah ok :) |
Hi all,
Quick question: I am using
librdkafka
to receive messages whose payload is JSON string. The library returns the payload in a non-null terminatedvoid*
and provides amessage->len()
to indicate how many characters/bytes are valid. Is it possible to call something like:json::parse(void*, const size_t&)
?I see that the parser supports
char*
type but it overruns the string's length causing an overflow ...The solutions I see are:
json::parse(void*, const size_t&)
signature maybe by extending/overloadingparse(T(&) array[N] ...)
}
character and stopAt the moment I am using
strncpy
to populate a pre-allocated buffer and terminate it properly before passing it toparse
. This is an extra copy operation which could be avoided...Many thanks for this awesome library - let me know what you think,
Andreas
The text was updated successfully, but these errors were encountered: