From 25d205c16d9f6b1be883eaff97d8bf0ff4a22339 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 6 Dec 2017 21:42:36 +0100 Subject: [PATCH] :memo: clarified difference between serialization and string value retrieval #853 --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 60be52987..6e13bfdc3 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,29 @@ std::cout << j.dump(4) << std::endl; // } ``` +Note the difference between serialization and assignment: + +```cpp +// store a string in a JSON value +json j_string = "this is a string"; + +// retrieve the string value (implicit JSON to std::string conversion) +std::string cpp_string = j_string; +// retrieve the string value (explicit JSON to std::string conversion) +auto cpp_string2 = j_string.get(); + +// retrieve the serialized value (explicit JSON serialization) +std::string serialized_string = j_string.dump(); + +// output of original string +std::cout << cpp_string << " == " << cpp_string2 << " == " << j_string.get() << '\n'; +// output of serialized value +std::cout << j_string << " == " << serialized_string << std::endl; +``` + +`.dump()` always returns the serialized value, and `.get()` returns the originally stored string value. + + #### To/from streams (e.g. files, string streams) You can also use streams to serialize and deserialize: