Skip to content

Commit

Permalink
📝 clarified difference between serialization and string value retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Dec 6, 2017
1 parent fa76f2e commit 25d205c
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>();

// 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<std::string>() << '\n';
// output of serialized value
std::cout << j_string << " == " << serialized_string << std::endl;
```

`.dump()` always returns the serialized value, and `.get<std::string>()` returns the originally stored string value.


#### To/from streams (e.g. files, string streams)

You can also use streams to serialize and deserialize:
Expand Down

0 comments on commit 25d205c

Please sign in to comment.