Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Another lightweight C++ JSON library, provided simply as a C++ module.

License

NotificationsYou must be signed in to change notification settings

arttnba3/jsovon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JsOvOn

Another lightweight C++ JSON library, provided simply as aC++ module.

license

Usage

Import the Library

To use theJsOvOn library, all we only need to do is to"do the import":

import jsovon;

Don't forget to add the source code ofJsOvOn to your project and link theJsOvOn library into yourCMakeLists.txt.

You can refer tosrc/test.cpp andsrc/CMakeLists.txt to see how to import and use this library.

Currently you may need to enable CMake's support forimport std manually, as it's still an experimental feature for some compilers likeclang++.

Use classJson

Simply, we can create theJson object for basic data type like this:

jsovon::Json test;/* number*/test =123;unsignedint a = test;test = -456;int b = test;test =789.1011F;float c = test;test =1213.1415;double d = test;/* string*/test ="1617181920";std::string e = test;/* bool*/test =true;bool f = test;/* null*/test = jsovon::null;if (test == jsovon::null) {/* true here*/    std::cout <<"Hello world!" << std::endl;}

For common json object type, it can be used like this:

jsovon::Json object;object = {    {"key1",123 },    {"key2", -456 },    {"key3",789.1011F },    {"key4",1213.1415 },    {"key5","1617181920" },    {"key6",true },    {"key7", jsovon::null }};object["key8"] = object;unsignedint a = object["key1"];int b = object["key2"];float c = object["key3"];double d = object["key4"];std::string e = object["key5"];e = (std::string) object["key5"];/* note that to re-assign a string val from Json object, use casting explicitly*/bool f = object["key6"];jsovon::Json test = object["key7"];

For using the array type, just do it as follow:

jsovon::Json array;/* number*/array.append(123);unsignedint a = array[0];array.append(-456);int b = array[1];array.append(789.1011F);float c = array[2];array.append(1213.1415);double d = array[3];/* string*/array.append("1617181920");std::string e = array[4];e = (std::string) array[4];/* note that to re-assign a string val from Json array, use casting explicitly*//* bool*/array.append(true);bool f = array[5];/* null*/array.append(jsovon::null);jsovon::Json g = array[6];

Print classJson into JSON format

All we need to do is to call thestr() method, or you can print it directly into a stream:

jsovon::Json object = {    {"key1",123 },    {"key2", -456 },    {"key3",789.1011F },    {"key4",1213.1415 },    {"key5","1617181920" },    {"key6",true },    {"key7", jsovon::null }};std::cout << object << std::endl;std::cout << object.str() << std::endl;

Parse the JSON text into classJson

To parse a string into aJson class, just simply do as follow:

auto json_str =R"(    {        "key1" : 123,        "key2" : -456,        "key3" : 789.101,        "key4" : 1213.14,        "key5" : "1617181920",        "key6" : true,        "key7" : null    })";jsovon::Json test = jsovon::ParseJsonText(json_str);

Alternatively, you can also choose to parse a json file as follow:

jsovon::Json test = ParseJsonFile("test.json");

Author

License

MIT

About

Another lightweight C++ JSON library, provided simply as a C++ module.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp