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

Make certain usage patterns more prominent in the README #3557

Merged
merged 4 commits into from
Jul 18, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 64 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
- [Sponsors](#sponsors)
- [Support](#support) ([documentation](https://json.nlohmann.me), [FAQ](https://json.nlohmann.me/home/faq/), [discussions](https://github.com/nlohmann/json/discussions), [API](https://json.nlohmann.me/api/basic_json/), [bug issues](https://github.com/nlohmann/json/issues))
- [Examples](#examples)
- [JSON as first-class data type](#json-as-first-class-data-type)
- [Read JSON from a file](#read-json-from-a-file)
- [Creating `json` objects from JSON literals](#creating-json-objects-from-json-literals)
- [Reading and writing individual object keys](#reading-and-writing-individual-object-keys)
jez marked this conversation as resolved.
Show resolved Hide resolved
- [Serialization / Deserialization](#serialization--deserialization)
- [STL-like access](#stl-like-access)
- [Conversion from STL containers](#conversion-from-stl-containers)
Expand Down Expand Up @@ -97,13 +99,70 @@ There is also a [**docset**](https://github.com/Kapeli/Dash-User-Contributions/t

## Examples

Beside the examples below, you may want to check the [documentation](https://json.nlohmann.me/) where each function contains a separate code example (e.g., check out [`emplace()`](https://json.nlohmann.me/api/basic_json/emplace/)). All [example files](https://github.com/nlohmann/json/tree/develop/docs/examples) can be compiled and executed on their own (e.g., file [emplace.cpp](https://github.com/nlohmann/json/blob/develop/docs/examples/emplace.cpp)).
Here are some examples to give you an idea how to use the class.

### JSON as first-class data type
Beside the examples below, you may want to:

Here are some examples to give you an idea how to use the class.
→ Check the [documentation](https://json.nlohmann.me/)\
→ Browse the [standalone example files](https://github.com/nlohmann/json/tree/develop/docs/examples)

Every API function (documented in the [API Documentation](https://json.nlohmann.me/api/basic_json/)) has a corresponding standalone example file. For example, the [`emplace()`](https://json.nlohmann.me/api/basic_json/emplace/) function has a matching [emplace.cpp](https://github.com/nlohmann/json/blob/develop/docs/examples/emplace.cpp) example file.

### Read JSON from a file

The `json` class provides an API for manipulating a JSON value. To create a `json` object by reading a JSON file:

```cpp
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;

// ...

std::ifstream f("example.json");
json data = json::parse(f);
```

### Creating `json` objects from JSON literals

Assume you want to create hard-code this literal JSON value in a file, as a `json` object:

```json
{
"pi": 3.141,
"happy": true
}
```

There are various options:

```cpp
// Using (raw) string literals and json::parse
json ex1 = json::parse(R"(
{
"pi": 3.141,
"happy": true
}
)");

// Using user-defined (raw) string literals
json ex2 = R"(
{
"pi": 3.141,
"happy": true
}
)"_json;

// Using initializer lists
json ex3 = {
{"happy", true},
{"pi", 3.141},
};
```

### Reading and writing individual object keys
jez marked this conversation as resolved.
Show resolved Hide resolved

Assume you want to create the JSON object
Assume you want to create this JSON object by individually setting each key-value pair:
jez marked this conversation as resolved.
Show resolved Hide resolved

```json
{
Expand Down