- Notifications
You must be signed in to change notification settings - Fork0
Another lightweight C++ JSON library, provided simply as a C++ module.
License
arttnba3/jsovon
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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 for
import std
manually, as it's still an experimental feature for some compilers likeclang++
.
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];
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;
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");
- Code: arttnba3arttnba3@outlook.com
- Illustration: Sumi Kirikokirikosumi@gmail.com
MIT
About
Another lightweight C++ JSON library, provided simply as a C++ module.