Skip to content
/ json4cpp Public

A modern C++23 module-based library for JSON and FSON (Fast JSON) serialization. Built with C++ modules for fast compilation and clean interfaces.

License

Notifications You must be signed in to change notification settings

ruoka/json4cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json4cpp

JSON and FSON Library for C++23

A modern C++23 module-based library for JSON and FSON (Fast JSON) serialization. Built with C++ modules for fast compilation and clean interfaces.

Requirements

  • C++23 compatible compiler (Clang 17+ recommended)
  • LLVM with std.cppm module support

Building

The project uses the C++ Builder (CB) build system. From the project root:

tools/CB.sh <path-to-std.cppm> your-program.c++

Usage

JSON Object Creation

import std;
import xson;

using namespace std::string_literals;
using namespace xson;

auto document = object{};

document["papa"s]["name"s] = "Cool"s;
document["papa"s]["age"s] = 40;
document["papa"s]["married"s] = false;
document["papa"s]["kids"s][1] = { "Name"s, "Tulppu"s };
document["papa"s]["kids"s][2] = { "Name"s, "Elppu"s };
document["papa"s]["kids"s][3] = { "Name"s, "Jalppu"s };

std::clog << json::stringify(document) << std::endl;

JSON Parsing

import std;
import xson;

using namespace std::string_literals;
using namespace xson;
using xson::json::operator <<;
using xson::json::operator >>;

auto ss = std::stringstream{R"(
    {
        "_id" : 2,
        "Name" : "Ruoka",
        "Embedded" : {
            "_id" : 5,
            "Name" : "Tuma"
        },
        "Lucky Numbers" : [
            2,
            22,
            2112
        ]
    }
)"};

std::clog << ss.str() << "\n\n";

auto result = json::parse(ss);

std::clog << std::setw(2) << result << "\n\n";

std::clog << "_id            = " << result["_id"s]               << "\n"
     << "Name           = " << result["Name"s]              << "\n"
          << "Embedded.Name  = " << result["Embedded"s]["Name"s] << "\n"
     << "Lucky Number 1 = " << result["Lucky Numbers"s][0]  << "\n"
     << "Lucky Number 2 = " << result["Lucky Numbers"s][1]  << "\n"
     << "Lucky Number 3 = " << result["Lucky Numbers"s][2]  << "\n\n";

xson::integer_type id = result["_id"s];
xson::string_type name = result["Name"s];
xson::integer_type number = result["Lucky Numbers"s][1];

Module Structure

The library is organized as C++23 modules:

  • xson - Main module exporting core types and functions
  • xson:json - JSON parsing and stringification
  • xson:fson - FSON (Fast JSON) binary serialization
  • xson:object - Core object, array, and builder types
  • xson:fast - Fast encoding/decoding utilities
  • xson:trace - Debug tracing functionality

Features

  • C++23 Modules: Fast compilation with module interfaces
  • JSON Support: Full JSON parsing and stringification
  • FSON Support: Binary serialization format for efficient storage
  • Type-Safe: Strong typing with integer_type, number_type, string_type, etc.
  • Extensible: Support for custom types via timestamp and integer types

About

A modern C++23 module-based library for JSON and FSON (Fast JSON) serialization. Built with C++ modules for fast compilation and clean interfaces.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published