76. Deserializing data from JSON

For solving this task we will use the nlohmann/json library again. Instead of writing from_json() functions, as was mentioned in the solution to the previous problem, we will take a more explicit approach. The content of a JSON file can be loaded into an nlohmann::json object using the overloaded operator>>. To access the object values, you should use the at() method rather than operator[], because the former throws an exception if the key does not exist (an exception that you can handle), while the latter exhibits undefined behavior. To retrieve an object value as a particular T object, use the get<T>() method. However, this requires the type T to be default constructible.

The deserialize() function shown ...

Get The Modern C++ Challenge now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.