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

[Question] how to use nlohmann/json in c++? #1088

Closed
boddumanohar opened this issue May 14, 2018 · 11 comments
Closed

[Question] how to use nlohmann/json in c++? #1088

boddumanohar opened this issue May 14, 2018 · 11 comments

Comments

@boddumanohar
Copy link

I am working on an application where two process exchange data through shared memory. So instead of using structs to pack the data, I choose to use JSON so that I won't face any issues due to compilers.

My sender side is written in C++14 and the receiver side is written in C

On the reciver side, which is C, how do I read the json data?

I really appreciate the help.

@nlohmann
Copy link
Owner

You want to know how to access a C++ data structure in C?

@boddumanohar
Copy link
Author

Yes. Also which header file should I use?

@nlohmann
Copy link
Owner

The library heavily relies on C++11 features and the STL. I have no idea how C should be able to make sense out of a basic_json value...

@boddumanohar
Copy link
Author

Do you know of any other library that can fulfil my need?

@nlohmann
Copy link
Owner

Sorry, I do not. Maybe check on http://json.org which C libraries are suitable.

@boddumanohar
Copy link
Author

Thanks a ton @nlohmann

@pboettch
Copy link
Contributor

You could, theoretically, write a C-wrapper around this library. Losing basically every flexibility gained by using modern C++. But, with the advantage, that you could share the data-structure (and the same allocated memory) between C and C++ without reparsing JSON several times within the same application.

@boddumanohar
Copy link
Author

My idea is, JSON is plain text based serialized data. So the serialisation will be preserved somewhere within the data structures. I just need to pass the pointer to this location of serialised data. But I still trying to figure out how.

Could please help me understand how nlohmann/json preserves this serialisation?

@boddumanohar boddumanohar reopened this May 16, 2018
@nlohmann
Copy link
Owner

The library has a function dump() that serializes to a std::string. That string is returned and note stored in the library (if this is what you mean with "preserves the serialization").

@boddumanohar
Copy link
Author

boddumanohar commented May 16, 2018 via email

@nlohmann
Copy link
Owner

I guess so.

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

No branches or pull requests

3 participants