From ff6397d1fdf1a30314f0462937f7a2ed217f68e2 Mon Sep 17 00:00:00 2001 From: Vinit Pandit <106718914+MeastroZI@users.noreply.github.com> Date: Wed, 24 Apr 2024 17:04:43 +0530 Subject: [PATCH] Update README.markdown Signed-off-by: Vinit Pandit <106718914+MeastroZI@users.noreply.github.com> --- README.markdown | 82 ++++++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 46 deletions(-) diff --git a/README.markdown b/README.markdown index b59ad0577..7002a18f5 100644 --- a/README.markdown +++ b/README.markdown @@ -6,6 +6,42 @@ standards such as [JSON Schema](http://json-schema.org), [JSON Pointer](https://www.rfc-editor.org/rfc/rfc6901), [JSONL](https://jsonlines.org), and more. +Example + +```cpp +#include +#include +#include +#include + +int main() { + // Creating JSON using parse + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse(R"JSON({ + "name": "John Doe", + "age": 20, + "address": "zxy" + })JSON"); + + // JSON pointer for the name property + const sourcemeta::jsontoolkit::Pointer name_pointer{"name"}; + // A new JSON document for the name + const sourcemeta::jsontoolkit::JSON name_value{"Johnny Doe"}; + // Updating the value of the name using JSON pointer + sourcemeta::jsontoolkit::set(document, name_pointer, name_value); + sourcemeta::jsontoolkit::prettify(document, std::cout); + std::cout<<"\n"; + + // The above program will print the following to standard output: + //output : + // { + // "address": "zxy_with_bar", + // "age": 21, + // "name": "xyz_with_foo" + // } +} +``` + Documentation ------------- @@ -23,50 +59,4 @@ obtain a [commercial license](./LICENSE-COMMERCIAL) that removes such restrictions. Read more about our licensing approach [here](https://www.sourcemeta.com/licensing/). -Example --------------- - -```cpp -#include -#include -#include -#include -int main() -{ - // creating json using parse - sourcemeta::jsontoolkit::JSON _template_ = - sourcemeta::jsontoolkit::parse(R"JSON({ - "name" : "xyz" , - "age" : 20 , - "address" : "zxy" - })JSON"); - - // stringstream for output - std::ostringstream stringvar; - // pointer for the name - const sourcemeta::jsontoolkit::Pointer name_pointer{"name"}; - const sourcemeta::jsontoolkit::Pointer age_pointer{"age"}; - const sourcemeta::jsontoolkit::Pointer address_pointer{"address"}; - // value instance for json - const sourcemeta::jsontoolkit::JSON name_value{"xyz_with_foo"}; - const sourcemeta::jsontoolkit::JSON age_value{20 + 1}; - const sourcemeta::jsontoolkit::JSON address_value{"zxy_with_bar"}; - // updating the value of the name using pointer - sourcemeta::jsontoolkit::set(_template_, name_pointer, name_value); - sourcemeta::jsontoolkit::set(_template_, age_pointer, age_value); - sourcemeta::jsontoolkit::set(_template_, address_pointer, address_value); - sourcemeta::jsontoolkit::prettify(_template_, stringvar); - std::cout << stringvar.str() << std::endl; - - /* - output : - { - "address": "zxy_with_bar", - "age": 21, - "name": "xyz_with_foo" - } - - */ -} -```