From 604df6d7adf48f6e4ca6ef7fe7f65f14dd835d54 Mon Sep 17 00:00:00 2001 From: namazso <8676443+namazso@users.noreply.github.com> Date: Tue, 1 Dec 2020 21:40:04 +0100 Subject: [PATCH] add a non-compiled c++ version of example #3 --- examples/example-03-cpp.cpp | 142 ++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 examples/example-03-cpp.cpp diff --git a/examples/example-03-cpp.cpp b/examples/example-03-cpp.cpp new file mode 100644 index 0000000..217f257 --- /dev/null +++ b/examples/example-03-cpp.cpp @@ -0,0 +1,142 @@ + +/* + + + + Licensed under the MIT License . + SPDX-License-Identifier: MIT + Copyright (c) 2016-2018 Rafa Garcia . + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + +*/ + +/* + * In this example the JSON library is used to analyze an object that some + * properties are expected. + */ + +#include +#include +#include +#include +#include +#include "../tiny-json.h" + +class jsonParser : jsonPool_t +{ + static json_t* alloc_fn( jsonPool_t* pool ) + { + const auto list_pool = (jsonParser*)pool; + list_pool->_list.emplace_back(); + return &list_pool->_list.back(); + } + + std::list _list{}; + std::string _str{}; + json_t const* _root{}; + +public: + jsonParser() : jsonPool_t{ &alloc_fn, &alloc_fn } {} + jsonParser( const char* str ) + : jsonPool_t{ &alloc_fn, &alloc_fn } + , _str{ str } + { + _root = json_createWithPool( _str.data(), this ); + } + jsonParser( const jsonParser& ) = delete; + jsonParser( jsonParser&& ) = delete; + jsonParser& operator=( const jsonParser& ) = delete; + jsonParser& operator=( jsonParser&& ) = delete; + + void parse( const char* str ) + { + _str = str; + _list.clear(); + _root = json_createWithPool( _str.data(), this ); + } + + json_t const* root() const { return _root; } +}; + + +/* Parse a json string. */ +int main() { + const char* str = "{\n" + "\t\"firstName\": \"Bidhan\",\n" + "\t\"lastName\": \"Chatterjee\",\n" + "\t\"age\": 40,\n" + "\t\"address\": {\n" + "\t\t\"streetAddress\": \"144 J B Hazra Road\",\n" + "\t\t\"city\": \"Burdwan\",\n" + "\t\t\"state\": \"Paschimbanga\",\n" + "\t\t\"postalCode\": \"713102\"\n" + "\t},\n" + "\t\"phoneList\": [\n" + "\t\t{ \"type\": \"personal\", \"number\": \"09832209761\" },\n" + "\t\t{ \"type\": \"fax\", \"number\": \"91-342-2567692\" }\n" + "\t]\n" + "}\n"; + puts( str ); + jsonParser parser { str }; + json_t const *json = parser.root(); + if ( !json ) { + puts("Error json create."); + return EXIT_FAILURE; + } + + json_t const* firstName = json_getProperty( json, "firstName" ); + if ( !firstName || JSON_TEXT != json_getType( firstName ) ) { + puts("Error, the first name property is not found."); + return EXIT_FAILURE; + } + char const* firstNameVal = json_getValue( firstName ); + printf( "Fist Name: %s.\n", firstNameVal ); + + char const* lastName = json_getPropertyValue( json, "lastName" ); + if ( !lastName ) { + puts("Error, the last name property is not found."); + return EXIT_FAILURE; + } + printf( "Last Name: %s.\n", lastName ); + + json_t const* age = json_getProperty( json, "age" ); + if ( !age || JSON_INTEGER != json_getType( age ) ) { + puts("Error, the age property is not found."); + return EXIT_FAILURE; + } + int const ageVal = (int)json_getInteger( age ); + printf( "Age: %d.\n", ageVal ); + + json_t const* phoneList = json_getProperty( json, "phoneList" ); + if ( !phoneList || JSON_ARRAY != json_getType( phoneList ) ) { + puts("Error, the phone list property is not found."); + return EXIT_FAILURE; + } + + json_t const* phone; + for( phone = json_getChild( phoneList ); phone != 0; phone = json_getSibling( phone ) ) { + if ( JSON_OBJ == json_getType( phone ) ) { + char const* phoneNumber = json_getPropertyValue( phone, "number" ); + if ( phoneNumber ) printf( "Number: %s.\n", phoneNumber ); + } + } + + return EXIT_SUCCESS; +}