Skip to content

Commit

Permalink
Update README: add more examples and reorganise paragraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
st235 committed Jul 2, 2024
1 parent 8ef3477 commit 5a27ae5
Showing 1 changed file with 45 additions and 24 deletions.
69 changes: 45 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,54 @@ See more in [`sample`](./sample/)

## Exploring the API

Everything you may expect is implemented (most likely 😅).
Almost everything you might expect has been implemented. Take a look at this real-life example:

```cpp
if (response.isObject() && response["results"].isArray()) {
const auto& results_array = response["results"].asArray();

for (size_t i = 0; i < results_array.size(); i++) {
const auto& raw_character = results_array[i];

Character c = {
uint32_t(raw_character["id"].asNumber()),
raw_character["name"].asString(),
raw_character["species"].asString(),
raw_character["gender"].asString(),
raw_character["image"].asString()
};

...
}
}
```
Here are a few more examples of the most common use cases for your reference.
### Create `Json` object from a `string`
```cpp
std::string json_text = ...
const auto& json = json::Json::fromJson(json_text);
```

### Declare `Json` object and dump it to back to `string`

```cpp
json::Json json = {
std::make_pair("a", json::Json({ json::Json(true), json::Json("b") })),
std::make_pair("b", json::Json(129.1))
};

std::string json_text = json::Json::toString(json);
```

In this example, the `json object` will be _minified_. If you want to _beautify_ the JSON (i.e., make it human-readable), the library offers a default implementation of `JsonBeautifier`, which you can find in [the samples folder.](./samples/)

### Types

The following paragraphs provide a deep dive into the implementation details of the library.

JSON specification declares 4 types and 3 literals:
- Literals
- `null`
Expand Down Expand Up @@ -64,29 +108,6 @@ Almost all `is` methods (except `isNull`) has corresponding `as` methods to safe
- `asArray` -> `std::vector<Json>`
- `asObject` -> `std::unordered_map<std::string, Json>`

### Create `Json` object from `std::string`

```cpp
#include "json.h"

...

std::string json_text = ...
const auto& json = json::Json::fromJson(json_text);

...
```

### Declare `Json` object

```cpp
json::Json json = {
std::make_pair("a", json::Json({ json::Json(true), json::Json("b") })),
std::make_pair("b", json::Json(129.1))
}
```


## Json Grammar Rules

The specification of JSON format is available at [_the oficial website_](https://www.json.org/json-en.html) or as [_ECMA-404 The JSON Data Interchange Standard_](https://ecma-international.org/publications-and-standards/standards/ecma-404/).
Expand Down

0 comments on commit 5a27ae5

Please sign in to comment.