Skip to content

Commit

Permalink
Update generic-json.md
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenberry committed Dec 12, 2024
1 parent 2aa695a commit 6346f64
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/generic-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ glz::read_json(json, buffer);
assert(json[0].get<double>() == 5.0);
assert(json[1].get<std::string>() == "Hello World");
assert(json[2]["pi"].get<double>() == 3.14);
assert(json[2]["pi"].as<int>() == 3);
```
```c++
Expand All @@ -31,6 +32,10 @@ glz::write_json(json, buffer);
expect(buffer == R"({"answer":{"everything":42},"happy":true,"list":[1,0,2],"name":"Stephen","object":{"currency":"USD","value":42.99},"pi":3.141})");
```

## get() vs as()

`glz::json_t` is a variant underneath that stores all numbers in `double`. The `get()` method mimics a `std::get` call for a variant, which rejects conversions. If you want to access a number as an `int`, then call `json.as<int>()`, which will cast the `double` to an `int`.

## Type Checking json_t

`glz::json_t` has member functions to check the JSON type:
Expand Down

0 comments on commit 6346f64

Please sign in to comment.