From 6346f64b38d69bba46c3c610f713758001e0969d Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Thu, 12 Dec 2024 09:24:13 -0600 Subject: [PATCH] Update generic-json.md --- docs/generic-json.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/generic-json.md b/docs/generic-json.md index 2701773da4..aecbd31f49 100644 --- a/docs/generic-json.md +++ b/docs/generic-json.md @@ -11,6 +11,7 @@ glz::read_json(json, buffer); assert(json[0].get() == 5.0); assert(json[1].get() == "Hello World"); assert(json[2]["pi"].get() == 3.14); +assert(json[2]["pi"].as() == 3); ``` ```c++ @@ -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()`, which will cast the `double` to an `int`. + ## Type Checking json_t `glz::json_t` has member functions to check the JSON type: