


A modern C++20 JSON library with type-safe manipulation, zero-copy navigation, and intuitive path-based access
Overview
nfx-json is a modern C++20 library for working with JSON documents. It provides type-safe value access, zero-copy document navigation with JSON Pointer and dot notation paths, STL-compatible iterators for arrays and objects, and efficient parsing and generation - all optimized for performance across multiple platforms and compilers.
Key Features
📦 Core JSON Components
- Builder: High-performance streaming JSON construction with fluent method chaining and SIMD optimizations
- Document: Generic JSON document abstraction with type-safe value access and manipulation
- PathView: Zero-copy document traversal with JSON Pointer and dot notation path iteration
🔒 Type-Safe Value Access
- Template-based get<T>() and set<T>() methods with compile-time type checking
- Support for primitive types (integers, floats, booleans, strings)
- C++20 concepts for type constraints (Primitive, Value, Container)
- Returns std::optional<T> for safe value retrieval
- Direct access to Array and Object containers
🛤️ Flexible Path Navigation
- JSON Pointer (RFC 6901): /user/name, /items/0/id
- Dot Notation: user.name, items[0].id
- Auto-detection based on path syntax (paths starting with / use JSON Pointer)
- Create intermediate objects/arrays automatically with createPath option
- Navigate deeply nested structures safely
📊 Real-World Applications
- Configuration management (app settings, environment configs)
- API request/response handling (REST, GraphQL)
- Data persistence and caching
- Inter-process communication (IPC)
- Log processing and analysis
- Database document storage (NoSQL, MongoDB-style)
- Game save states and player data
- Message queue payloads (Kafka, RabbitMQ)
🔄 STL-Compatible Iteration
- Range-for loops over arrays: for (const auto& item : array) { ... }
- Range-for loops over objects: for (const auto& [key, value] : object) { ... }
- ObjectIterator with .key() and .value() accessors
- PathView for iterating all document paths with depth tracking
⚡ Performance Optimized
- SIMD-accelerated JSON serialization (SSE2 string escaping)
- Zero-copy document navigation with JSON Pointers
- Fast parsing with std::from_chars (no locale overhead)
- Direct buffer writing for JSON generation (no intermediate DOM)
- Pre-allocated buffers with smart memory management
- Compile-time type detection and optimization
🌍 Cross-Platform Support
- Linux, Windows
- GCC 14+, Clang 18+, MSVC 2022+
- Thread-safe operations
- Consistent behavior across platforms
Quick Start
Requirements
- C++20 compatible compiler:
- GCC 14+ (14.2.0 tested)
- Clang 18+ (19.1.7 tested)
- MSVC 2022+ (19.44+ tested)
- CMake 3.20 or higher
CMake Integration
# --- Library build types ---
option(NFX_JSON_BUILD_STATIC "Build static library" ON )
option(NFX_JSON_BUILD_SHARED "Build shared library" OFF)
# --- Build components ---
option(NFX_JSON_BUILD_TESTS "Build tests" OFF)
option(NFX_JSON_BUILD_SAMPLES "Build samples" OFF)
option(NFX_JSON_BUILD_BENCHMARKS "Build benchmarks" OFF)
option(NFX_JSON_BUILD_DOCUMENTATION "Build Doxygen documentation" OFF)
# --- Performance options ---
option(NFX_JSON_ENABLE_SIMD "Enable native CPU optimizations" ON )
# --- Installation & packaging ---
option(NFX_JSON_INSTALL_PROJECT "Install project" OFF)
option(NFX_JSON_PACKAGE_SOURCE "Enable source package generation" OFF)
Using in Your Project
Option 1: Using FetchContent (Recommended)
include(FetchContent)
FetchContent_Declare(
nfx-json
GIT_REPOSITORY https://github.com/nfx-libs/nfx-json.git
GIT_TAG main # or use specific version tag like "1.0.0"
)
FetchContent_MakeAvailable(nfx-json)
# Link with static library
target_link_libraries(your_target PRIVATE nfx-json::static)
Option 2: As a Git Submodule
# Add as submodule
git submodule add https://github.com/nfx-libs/nfx-json.git third-party/nfx-json
# In your CMakeLists.txt
add_subdirectory(third-party/nfx-json)
target_link_libraries(your_target PRIVATE nfx-json::static)
Option 3: Using find_package (After Installation)
find_package(nfx-json REQUIRED)
target_link_libraries(your_target PRIVATE nfx-json::static)
Building
Build Commands:
# Clone the repository
git clone https://github.com/nfx-libs/nfx-json.git
cd nfx-json
# Create build directory
mkdir build && cd build
# Configure with CMake
cmake .. -DCMAKE_BUILD_TYPE=Release
# Build the library
cmake --build . --config Release --parallel
# Run tests (optional)
ctest -C Release --output-on-failure
# Run benchmarks (optional)
./bin/Release/BM_JsonAccess
Documentation
nfx-json includes API documentation generated with Doxygen.
📚 Online Documentation
The complete API documentation is available online at: https://nfx-libs.github.io/nfx-json
Building Documentation Locally
# Configure with documentation enabled
cmake .. -DCMAKE_BUILD_TYPE=Release -DNFX_JSON_BUILD_DOCUMENTATION=ON
# Build the documentation
cmake --build . --target nfx-json-documentation
Requirements
- Doxygen - Documentation generation tool
- Graphviz Dot (optional) - For generating class diagrams
Accessing Local Documentation
After building, open ./build/doc/html/index.html in your web browser.
Usage Examples
Builder - High-Performance JSON Construction
using namespace nfx::json;
.
write(
"name",
"Alice Johnson")
.
write(
"email",
"alice@example.com")
.
write(
"username",
"alice_dev")
.
write(
"firstName",
"Alice")
.
write(
"lastName",
"Johnson")
std::string prettyJson = prettyBuilder.
toString();
High-performance JSON builder for programmatic construction.
High-performance JSON builder with incremental construction API.
Builder & writeEndArray()
End writing a JSON array.
std::string toString()
Get the constructed JSON string.
Builder & writeKey(std::string_view key)
Write a property key before a nested object or array.
Builder & writeStartObject()
Start writing a JSON object.
Builder & write(std::string_view key, std::nullptr_t value)
Write a null value property.
Builder & writeStartArray()
Start writing a JSON array.
Builder & writeEndObject()
End writing a JSON object.
Document - JSON Manipulation
using namespace nfx::json;
doc.
set<std::string>(
"/name",
"John Doe");
doc.
set<
int>(
"/age", 30);
doc.
set<std::string>(
"/email",
"john.doe@example.com");
auto name = doc.
get<std::string>(
"/name");
auto age = doc.
get<
int>(
"/age");
doc.
set<std::string>(
"/address/city",
"New York");
doc.
set<std::string>(
"/address/zip",
"10001");
doc.
set<std::string>(
"/hobbies/0",
"reading");
doc.
set<std::string>(
"/hobbies/1",
"gaming");
doc.
set<std::string>(
"/hobbies/2",
"coding");
JSON Document type with low-level and high-level APIs.
Low-level JSON value storage type.
std::optional< T > get(std::string_view path) const
Get typed value at specified path.
std::string toString(int indent=0, size_t bufferSize=0) const
Convert to JSON string.
void set(std::string key, Document value)
Insert or assign object member.
Array Iteration with Range-For
using namespace nfx::json;
"items": [
{"id": 1, "name": "Item A"},
{"id": 2, "name": "Item B"},
{"id": 3, "name": "Item C"}
]
})");
if (!docOpt)
{
return 1;
}
auto itemsOpt = docOpt->get<
Array>(
"items");
if (itemsOpt)
{
for (const auto& item : itemsOpt.value())
{
auto id = item.get<int64_t>("id");
auto name = item.get<std::string>("name");
if (id && name)
{
std::cout << "ID: " << *id << ", Name: " << *name << std::endl;
}
}
}
std::vector< Document > Array
Array for JSON arrays.
static std::optional< Document > fromString(std::string_view jsonStr)
Create Document from JSON string.
Object Iteration with Range-For
using namespace nfx::json;
"config": {
"timeout": 30,
"retries": 3,
"debug": true
}
})");
if (!docOpt)
{
return 1;
}
auto configOpt = docOpt->get<
Object>(
"config");
if (configOpt)
{
for (const auto& [key, value] : configOpt.value())
{
std::cout << "Key: " << key << ", Value: " << value.toString() << std::endl;
}
}
std::vector< std::pair< std::string, Document > > Object
Ordered map for JSON objects (preserves insertion order).
PathView - Document Traversal
using namespace nfx::json;
"user": {
"name": "Alice",
"age": 30,
"address": {
"city": "Seattle",
"zip": "98101"
}
},
"tags": ["developer", "manager"]
})");
if (!docOpt)
{
return 1;
}
{
std::cout << entry.path << " (depth: " << entry.depth << ")" << std::endl;
}
{
std::cout << entry.path << std::endl;
}
auto leaves = paths.leaves();
for (const auto& entry : leaves)
{
std::cout << entry.path << " = " << entry.value().toString() << std::endl;
}
Path iterator for traversing all paths in a JSON document.
@ DotNotation
Dot notation format (e.g., "user.addresses[0].city").
Complete Example
#include <iostream>
using namespace nfx::json;
int main()
{
"server": {
"host": "localhost",
"port": 8080,
"ssl": true
},
"users": [
{"name": "Alice", "role": "admin"},
{"name": "Bob", "role": "user"}
]
})");
if (!docOpt) {
std::cerr << "Failed to parse JSON\n";
return 1;
}
auto host = config.
get<std::string>(
"server.host");
auto port = config.
get<int64_t>(
"/server/port");
std::cout << "Server: " << host.value_or("unknown")
<< ":" << port.value_or(0) << "\n";
if (
auto users = config.
get<
Array>(
"users")) {
for (const auto& user : *users) {
auto name = user.get<std::string>("name");
auto role = user.get<std::string>("role");
std::cout << "User: " << *name << " (" << *role << ")\n";
}
}
config.
set<int64_t>(
"/server/port", 9090);
config.
set<std::string>(
"/server/environment",
"production");
std::cout <<
"\nUpdated config:\n" << config.
toString(2) <<
"\n";
return 0;
}
Sample Output:
Server: localhost:8080
User: Alice (admin)
User: Bob (user)
Updated config:
{
"server": {
"host": "localhost",
"port": 9090,
"ssl": true,
"environment": "production"
},
"users": [
{
"name": "Alice",
"role": "admin"
},
{
"name": "Bob",
"role": "user"
}
]
}
Project Structure
nfx-json/
├── benchmark/ # Performance benchmarks with Google Benchmark
├── cmake/ # CMake modules and configuration
├── include/nfx/ # Public headers
├── samples/ # Example usage and demonstrations
├── src/ # Implementation files
└── test/ # Unit tests with GoogleTest
Performance
For detailed performance metrics and benchmarks, see the benchmark documentation.
Roadmap
See TODO.md for upcoming features and project roadmap.
Changelog
See the CHANGELOG.md for a detailed history of changes, new features, and bug fixes.
License
This project is licensed under the MIT License.
Dependencies
Core Dependencies
Development Dependencies
- GoogleTest: Testing framework (BSD 3-Clause License) - Development only
- Google Benchmark: Performance benchmarking framework (Apache 2.0 License) - Development only
All dependencies are automatically fetched via CMake FetchContent when building the library, tests, or benchmarks.
Updated on February 15, 2026