the lib is located in build/Debug
and you need the included file in include/
.
With NuCpp comes a Json part. It is simple to use.
You need to make a class/struct which inherit from nu::Jsonable
so it will be Jsonable...
But to descibe you class, you need to put the variable which are Json-compatibles in the constructors of your class.
To do so, you can just add:
JsnVar(type, varName);
to your class constructors, or:
ListJsnVar {
JsnVar(types, varName);
}
anywhere in your class and add: JsonInit();
to your class constructors.
After that you can use the method load(string|char*|Json)
to load the Json string to your object.
Use the method stringify()
to get your object as a Json string.
The Json class is a class that make sure that the given string is a valide Json string and allow to access data with the operator[] and the external template function std::get<T>(vatiant)
or std::get_if<T>(&variant)
which are the std::get/get_if for variant.
#include <iostream>
#include <Json.hpp>
class A : public nu::Jsonable {
int val = 42;
string str = "nop";
ListJsnVar {
JsnVar(int, val);
JsnVar(string, str);
}
A() {
JsonInit();
}
};
int main() {
A a;
std::cout << a.stringify() << '\n'; // { "val": 42, "str": "nop" }
a.load(R"({"val": 18, "str": "yep"})");
std::cout << a.stringify() << '\n'; // { "val": 18, "str": "yep" }
return 0;
}
The Json-compatible types are the only type that NuCpp Json handle for now and are:
- bool, char, int, float, string
- Jsonable classes
- std::vector<bool | char | int | float | string>
working to add Jsonable and recursive vectors to std::vector. It will certainly handle more type with time like: std::optional, std::variant but always with: bool, char, int, float, string, Jsonable for base.