Skip to content

NuCpp is a C++ lib use to add some features I want to see.

Notifications You must be signed in to change notification settings

DarkMiMolle/JsonLibCPP-1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NuCpp

NuCpp is a C++ lib use to add some features I want to see.

the lib is located in build/Debug and you need the included file in include/.


Json

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.

exemple:

#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.

About

NuCpp is a C++ lib use to add some features I want to see.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages