Skip to content
AmirMahmoodPublic

About

Single file Header-only, Compile-Time C++ JSON parser with zero runtime metadata & parsing const

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

ctcJson

Single file Header-only, Compile-Time C++ JSON parser with zero runtime metadata & parsing const.

Usage

  • Requires C++20 or newer standard
  • Simply include ctcjson.hpp
#include <ctcjson/ctcjson.hpp>

The Entry point is ctcjson::parse function. Pass Json string as function argument and desire datatype as template argument, and return value is your constexpr object.

constexpr auto my_array = ctcjson::parse<std::array<int, 2>>(R"([12, -14])");
static_assert(my_array[0] == 12);
static_assert(my_array[1] == -14);

ctcJson supports integral, floating_point, boolean and string types internally. For json Object types, structures with field mapping Model must be defined;

struct Color {
    int code{0};
};

struct Config {
    bool fullscreen;
    std::string_view title;
    std::array<Color, 3> rgb_color;
};

template <>
struct ctcjson::ObjectModel<Color> {
    static consteval auto fields() {
        return std::tuple(field<"color_code">(&Color::code));
    }
};

template <>
struct ctcjson::ObjectModel<Config> {
    static consteval auto fields() {
        return std::tuple(field<"fullscreen">(&Config::fullscreen), field<"title">(&Config::title), field<"rgb">(&Config::rgb_color));
    }
};

constexpr Config my_config = ctcjson::parse<Config>(R"(
    {
        "fullscreen": true,
        "title": "my title",
        "rgb": [
            {"color_code": 100}, {"color_code": 200}, {}
        ]
    }
)");

ctcJson supports enums. it can convert string value in json to enum types. check following example.

enum class Mode { RGB, GRAY };

template <>
struct ctcjson::EnumModel<Mode> {
    static consteval auto values() {
        return std::tuple{enum_entry<"rgb", Mode::RGB>(), enum_entry<"gray", Mode::GRAY>()};
    }
};

struct Color {
    int code;
    Mode mode{Mode::GRAY};
};

template <>
struct ctcjson::ObjectModel<Color> {
    static consteval auto fields() {
        return std::tuple(field<"code">(&Color::code), field<"mode">(&Color::mode));
    }
};

constexpr Color my_color = ctcjson::parse<Color>(R"(
    {
        "code": 0,
        "mode":"rgb"
    }
)");

By default, CtCJson enables CTCJSON_STRICT_JSON_MODE. in this mode, if unknown key be in the input json, the parser throw compile-time error. You can disable this mode by passing -DCTCJSON_STRICT_JSON_MODE=0 definition to compiler.

Also By default, CtCJson use CTCJSON_ALL_FIELDS_OPTIONAL. in this mode, if a value for a member of an ObjectModel was not found in the input JSON, no compile-time error would be generated. so these fields wll be initialized with the default-initialization or user default value. You can disable this mode by passing -DCTCJSON_ALL_FIELDS_OPTIONAL=0 definition to compiler.

Limitations

  • no null support
  • array with multiple datatype not supported
  • array size must be fixed and known at compile time

Road Map

  • Remove tuple usage, to prevent tuple object creation in data sections
  • Escape character support in strings
  • optional or mandatory field
  • Support JSONC and JSON5

License

The project is available under the MIT license.

About

Single file Header-only, Compile-Time C++ JSON parser with zero runtime metadata & parsing const

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages