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

writing the binary json file? #479

Closed
gearslogy opened this issue Feb 28, 2017 · 2 comments
Closed

writing the binary json file? #479

gearslogy opened this issue Feb 28, 2017 · 2 comments
Labels
aspect: binary formats BSON, CBOR, MessagePack, UBJSON kind: question

Comments

@gearslogy
Copy link

I want to write binary file,and can read it back.
Please give me an example.
thanks

@nlohmann
Copy link
Owner

Do you mean CBOR or MessagePack?

// read CBOR file
std::ifstream input_file("input.cbor", std::ios::binary);
std::vector<uint8_t> input_vector(
    (std::istreambuf_iterator<char>(input_file)),
    std::istreambuf_iterator<char>());
json j = json::from_cbor(input_vector);

// write CBOR file
std::ofstream output_file("output.cbor", std::ios::out | std::ios::binary);
std::vector<uint8_t> output_vector;
json::to_cbor(j, output_vector);
file.write(output_vector.data(), output_vector.size());

@gearslogy
Copy link
Author

Thank you very much!
Your json library very very cool and very friendly to use!

@nlohmann nlohmann added the aspect: binary formats BSON, CBOR, MessagePack, UBJSON label Mar 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aspect: binary formats BSON, CBOR, MessagePack, UBJSON kind: question
Projects
None yet
Development

No branches or pull requests

2 participants