Skip to content

Commit 6346f64

Browse files
committed
Update generic-json.md
1 parent 2aa695a commit 6346f64

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

docs/generic-json.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ glz::read_json(json, buffer);
1111
assert(json[0].get<double>() == 5.0);
1212
assert(json[1].get<std::string>() == "Hello World");
1313
assert(json[2]["pi"].get<double>() == 3.14);
14+
assert(json[2]["pi"].as<int>() == 3);
1415
```
1516
1617
```c++
@@ -31,6 +32,10 @@ glz::write_json(json, buffer);
3132
expect(buffer == R"({"answer":{"everything":42},"happy":true,"list":[1,0,2],"name":"Stephen","object":{"currency":"USD","value":42.99},"pi":3.141})");
3233
```
3334

35+
## get() vs as()
36+
37+
`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`.
38+
3439
## Type Checking json_t
3540

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

0 commit comments

Comments
 (0)