Skip to content
/ lightjson Public

Simple, Light Weight, Header Only, C++11 compliant JSON Library.

License

Notifications You must be signed in to change notification settings

rft0/lightjson

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

LightJSON

LightJSON is a Simple, Light Weight, Header Only, C++11 compliant JSON Library. It is designed for projects which need a minimalistic JSON solution without the overhead of larger libraries.

Usage

Including Header

Just include header file to use it.

#include "jspn.hpp"

If you only want to use reading contents of json without dumping / manipulaitng it you can disable those features. (which may help to reduce binary size)

#define JSON_DISABLE_DUMPING
#include "jspn.hpp"

Parsing JSON

For starters you can pass either a raw pointer, std::string or a std::ifstream to parser.

const char* data = R"("name": "John", "lastName": "Doe", "age": 30, "something": [1, 2.5, {"three": "four"}])";
JSON json = JSONParser::parse(data)

// or

std::ifstream f("test.json");
JSON json = JSONParser::parse(data)

Reading Values

Reading simple fields.

const char* data = R"("name": "John", "lastName": "Doe", "age": 30, "something": [1, 2.5, {"dummy": "dummyvalue"}])";
JSON json = JSONParser::parse(data)

std::string name = json["name"].as<std::string>();       // John
int age = json["age"].as<int>();                         // 30
int someFloat = json["something"][1]                      // 2.5
std::string dummy = json["something"][2]["dummy"]         // dummyvalue

Reading arrays.

const char* data = R"("intArray": [1, 2, 3, 4, 5], "complexArray": ["something", 1, { "dummy": 2 }] )";
JSON json = JSONParser::parse(data);

// Reading an array when all members are same type
std::vector<int> intArray = json["intArray"].as<std::vector<int>>(); // [1, 2, 3, 4, 5]

// Reading an array when it contains members with different types.
std::vector<JSON> complexArray = json["complexArray"].as<std::vector<JSON>>();
complexArray[0].as<std::string>();     // something
complexArray[1].as<int>();             // 1
complexArray[2]["dummy"].as<int>();    // 2

// or we can read value of field "dummy" like this.
JSON anotherRoot = complexArray[2];
anotherRoot["dummy"].as<int>()         // 2

Creating / Changing / Dumping Values

Dumping is disabled if JSON_DISABLE_DUMPING is defined.

JSON root;
root["something"] = "another thing";
root["exampleArray"] = {1, 2, 3};
root["anotherObject]["smt"] = "value"; 

// Dumping json
printf("%s", root.dump(4)); // (first argument means indentation space count, default: 4)

// or we can dump json root directly with using overloaded << operator.
std::cout << root << std::endl;

// another way to create json from root with initializer lists
JSON root = JSON::o({
    {"something", "another thing"},
    {"exampleArray", {1, 2, 3}},
    {"anotherObject", { "smt", "value" }}
});

Releases

No releases published

Packages

No packages published

Languages