Kick off your book project in 2 hours, get started with GhostAI in 2 hours, or do both! Free live workshops, on Zoom. You’ll leave with a real book project and a clear plan to keep going. Saturday, June 27, 2026.

Leanpub Header

Skip to main content

How to Build a Typed JSON Configuration Library in Modern C++

From JSON Parsing to Compile-Time Reflection: Build a Production-Ready Header-Only Configuration Library with C++23

The instructor has published 75% of this course.Last updated on 2026-06-21

Ever wondered how configuration libraries work under the hood?

Most C++ courses teach you how to use libraries. But in this course, you'll build one—from scratch.

You'll discover:

  • Why header-only libraries are the gold standard for C++ distribution
  • How compile-time reflection with CRTP eliminates repetitive mapping code
  • Why dot-notation makes nested JSON access elegant and intuitive
  • How to provide crystal-clear error messages that save hours of debugging
  • And how to build a production-ready library that you can actually use in your projects

No black boxes. No magic. Just C++23 at its best.

By the end, you won't just know how to use a configuration library—you'll know exactly how it works, why it's designed that way, and how you can build your own.

Ready to build your own typed JSON configuration library in modern C++?

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 course credit with a Learner Membership

PDF
EPUB
WEB
About

About

About the Course

Building a Typed JSON Configuration Library in Modern C++

In this hands-on course, you'll build a production-ready configuration library from scratch—not just to use it, but to understand every line of code, every design decision, and every C++23 feature that makes it possible.

What You'll Learn

By the end of this course, you'll be able to:

  • Design and implement header-only C++ libraries with clean, intuitive APIs
  • Use compile-time reflection with CRTP to automatically map JSON to C++ types
  • Implement template metaprogramming patterns like std::apply, fold expressions, and concepts
  • Build production-quality C++ code with comprehensive error handling and testing
  • Master C++23 features including string_view, concepts, ranges, and structured bindings

Course Structure

This course is organized into 4 modules over approximately 150 minutes:

  • Module 1 (30 min): Project setup and C++23 foundations—CMake, FetchContent, header-only library structure, C++23 essentials
  • Module 2 (40 min): Core configuration class—three-query API pattern (get, try_get, has), JSON parsing with nlohmann/json, error handling
  • Module 3 (35 min): Dot-notation and error handling—path traversal algorithm, nested key access, meaningful error messages with field context
  • Module 4 (45 min): Compile-time reflection and capstone—CRTP pattern, Field descriptors, HasFields concept, from_json/to_json, nested reflectables, and building a sample application

Who Is This For?

This course is designed for intermediate C++ developers who:

  • Have 1-2 years of C++ experience
  • Want to understand how libraries are built from the inside out
  • Are interested in modern C++23 features and template metaprogramming
  • Want to improve their library design and error handling skills

Prerequisites

  • Basic C++ knowledge (classes, templates, STL)
  • Interest in library design and C++ metaprogramming
  • C++23 compiler (Apple Clang 15+, GCC 13+, or MSVC 2022)
  • CMake 3.27+ (we'll show you how to install it)

What You'll Get

By the end of this course, you'll have:

  • A fully functional header-only configuration library that you built yourself
  • Deep understanding of C++23 features and when to use them
  • Practical experience with template metaprogramming and compile-time reflection
  • Production-ready code that you can use in your own projects
  • The confidence to design and implement your own C++ libraries

How We'll Learn

  • Build the library step by step
  • 5-10 minute lessons: Bite-sized, focused content
  • Code walkthroughs: Every line explained with clear reasoning
  • Quizzes: 3-5 questions per module to reinforce learning
  • Capstone project: Build a sample application using your library

Share this course

Instructor

About the Instructor

Eduard Ghergu, Ph.D.

For over 27 years, I’ve helped teams transform their codebases from legacy liabilities to scalable, maintainable systems through clean architecture, domain-driven design, and hands-on mentorship. My books and courses distill decades of real-world experience into actionable insights—no fluff, just proven patterns.

Material

Course Material

  • C++23 Configuration Library: Build Your Own Header-Only JSON Parser

  • Course Introduction

  • Why Build Your Own?

  • What You’ll Learn

  • Course Structure

  • Who Is This For?

  • Prerequisites

  • How to Use This Course

  • What You’ll Get

  • Module 1 - Project Setup & C++23 Foundations

  • Lessons

  • Duration

  • Learning Objectives

  • Quiz

  • Module 1 - Lesson 1

  • Why C++23?

  • Installation Steps

  • Step 1: Install CLion IDE

  • Step 2: Verify Your Setup

  • Common Pitfalls

  • Summary Checklist

  • Transition

  • Module 1 - Lesson 2

  • Project Structure for Header-Only Libraries

  • Why This Structure?

  • Setting Up the Project

  • Step 1: Create the Directory Structure

  • Step 2: Create the Main Header File

  • Step 3: Create the Root CMakeLists.txt

  • Step 4: Create the Tests CMakeLists.txt

  • Step 5: Create a Sample Test

  • Step 6: Create an Example

  • Verifying the Setup

  • Common Pitfalls

  • Summary Checklist

  • Transition

  • Module 1 - Lesson 3

  • Why Header-Only?

  • Our Project Structure

  • The `include/` Directory

  • The `tests/` Directory

  • The `CMakeLists.txt` File

  • The `compile_commands.json` File

  • Header-Only Design Patterns

  • 1. Single Header Per Component

  • 2. Traditional Include Guards (Preferred)

  • 3. Header-Only Template Classes

  • What We Won’t Have

  • Real-World Examples

  • Summary Checklist

  • Transition

  • Module 1 - Lesson 4

  • Why C++23?

  • 1. std::string_view — Efficient String Handling

  • 2. C++20 Concepts — Type Constraints

  • 3. Range-based for — Modern Iteration

  • Lambda Expressions — Quick Primer

  • 4. Structured Bindings — Destructuring

  • 5. if with Initializer — Scope Control

  • Summary Checklist

  • Transition

  • Module 1 - Lesson 5

  • What is CMake?

  • Our Minimal CMakeLists.txt

  • 1. Project Declaration

  • 2. C++ Standard Settings

  • 3. Interface Library

  • 4. Include Directories

  • Building the Project

  • Using the Library

  • Common Pitfalls

  • Summary Checklist

  • References

  • Transition

  • Module 1 - Lesson 6

  • What is FetchContent?

  • Fetching nlohmann/json

  • Fetching GoogleTest

  • Linking Dependencies

  • Complete FetchContent Setup

  • Why Git Tags?

  • Common Pitfalls

  • Building with Dependencies

  • Summary Checklist

  • References

  • Transition

  • Module 1 - Quiz

  • Quiz

    3 attempts allowed

  • Module 2: Core Configuration Class

  • Lessons

  • Duration

  • Learning Objectives

  • Quiz

  • Module 2 - Lesson 1

  • The Three-Query API Pattern

  • 1. get() — Get with Default

  • 2. try_get() — Get as Optional

  • 3. has() — Check Existence

  • Design Trade-offs

  • Why Not Just One Method?

  • Our Solution: Three Methods

  • Interface Design Principles

  • 1. Clear Intent

  • 2. Exception Safety

  • 3. Template Flexibility

  • Example Usage Patterns

  • Pattern 1: Required with Fallback

  • Pattern 2: Optional with Check

  • Pattern 3: Existence Check

  • Pattern 4: Chained Access

  • Summary Checklist

  • Transition

  • Module 2 - Lesson 2

  • Why nlohmann/json?

  • Basic JSON Usage

  • Parsing JSON

  • Accessing Values

  • Type Conversions

  • Implementing the Constructor

  • Step-by-Step Breakdown

  • CMake Integration

  • Lazy Loading Pattern

  • Common Pitfalls

  • Summary Checklist

  • Transition

  • Module 2 - Lesson 3

  • Current Implementation: Eager Loading

  • Alternative: Lazy Loading

  • Why Eager Loading for Config Files?

  • 1. Config Files Are Small

  • 2. Error at Construction

  • 3. Simpler Implementation

  • 4. Config Files Don’t Change

  • When Lazy Loading Makes Sense

  • Our Implementation: Eager Loading

  • Performance Considerations

  • Memory Usage

  • Parse Time

  • Summary Checklist

  • Transition

  • Module 2 - Lesson 4

  • The find_key() Helper

  • The get() Method Signature

  • Default Value Magic: T{}

  • Full Implementation

  • Step-by-Step Breakdown

  • Template Type Deduction

  • Explicit Template Specification

  • Deduced from Default Value

  • Using Default Constructor

  • Supported Types

  • Primitive Types

  • Container Types

  • Custom Types (later)

  • Why Template?

  • Without Templates (C-style)

  • With Templates

  • Common Pitfalls

  • Summary Checklist

  • References

  • Transition

  • Module 2 - Lesson 5

  • The Default Value Pattern

  • Pattern 1: Default in Function Signature

  • Pattern 2: Default in Return

  • Default Value Initialization

  • Aggregate Initialization {}

  • Explicit Defaults

  • Default Value Types

  • Primitive Types

  • String Types

  • Container Types

  • Custom Types

  • Default Value Semantics

  • Missing Key → Default Value

  • Present Key → Actual Value

  • Type Mismatch → Exception

  • Default Value Best Practices

  • 1. Always Provide a Default

  • 2. Use Meaningful Defaults

  • 3. Document Your Defaults

  • Default Value vs Optional

  • When to Use Default Value

  • When to Use Optional

  • Summary Checklist

  • Transition

  • Module 2 - Lesson 6

  • The try_get() Method Signature

  • std::optional — What Is It?

  • Key Methods

  • Full Implementation

  • Step-by-Step Breakdown

  • Usage Examples

  • Example 1: Simple Optional Check

  • Example 2: Using value_or() for Default

  • Example 3: Pattern Matching

  • Example 4: Chaining with has()

  • try_get() with Default

  • try_get() — Explicit Optionality

  • get() with Default — Implicit Optionality

  • When to Use Each

  • Use try_get() When:

  • Use get() with Default When:

  • Common Pitfalls

  • Summary Checklist

  • Transition

  • Module 2 - Lesson 7

  • The has() Method Signature

  • Implementation

  • How has() Works

  • Leaf Node Check

  • Basic Existence Check

  • Empty Key Guard

  • Usage Examples

  • Example 1: Basic Existence Check

  • Example 2: Before Typed Object Access

  • Example 3: Multiple Checks

  • Example 4: Conditional Configuration

  • has() vs try_get() vs get()

  • has() — Check Existence Only

  • try_get() — Check + Access as Optional

  • get() — Get Value with Default

  • Performance Considerations

  • has() Performance

  • Better: Single Call with try_get()

  • When to Use Each

  • Use has() When:

  • Use try_get() When:

  • Use get() When:

  • has() Edge Cases

  • Empty Key

  • Nested Key (Intermediate Node)

  • Non-Object at Intermediate Node

  • Summary Checklist

  • Transition

  • Module 2 - Lesson 8

  • Error Handling Strategy

  • Error 1: File Not Found

  • Implementation

  • Error Message

  • Best Practices

  • Error 2: JSON Parse Error

  • Implementation

  • Error Message

  • Best Practices

  • Error 3: Type Conversion Error

  • Implementation

  • Error Message

  • Best Practices

  • Type Mismatch Error Messages

  • Example

  • Error Handling in try_get()

  • Implementation

  • Behavior

  • Error Handling in has()

  • Implementation

  • Behavior

  • Error Message Design Principles

  • 1. Include Context

  • 2. Be Specific

  • 3. Include Key Names

  • 4. Use std::runtime_error

  • Try-Catch Pattern

  • For the Users of the Library

  • For Library Implementers

  • Common Pitfalls

  • Summary Checklist

  • Transition

  • Module 2 - Quiz

  • Quiz

    3 attempts allowed

  • Module 3: Dot-Notation and Error Handling

  • Lessons

  • Duration

  • Learning Objectives

  • Quiz

  • Module 3 - Lesson 1

  • Why Dot-Notation?

  • The Problem

  • Alternative Approaches

  • 1. Nested Object Access (JavaScript-style)

  • 2. Array-Based Keys

  • 3. Path Objects

  • 4. Dot-Notation (Our Choice)

  • Design Trade-offs

  • Pros of Dot-Notation

  • Cons of Dot-Notation

  • How Dot-Notation Works

  • The Algorithm

  • Implementation Strategy

  • Step-by-Step Walkthrough

  • Edge Cases

  • 1. Empty Key

  • 2. No Dots (Flat Key)

  • 3. Trailing Dot

  • 4. Multiple Consecutive Dots

  • Why std::string_view?

  • Common Pitfalls

  • Summary Checklist

  • Transition

  • Module 3 - Lesson 2

  • Algorithm Overview

  • Step-by-Step Implementation

  • Step 1: Empty Key Guard

  • Step 2: Initialize Traversal

  • Step 3: Loop Through Segments

  • Step 4: Find Next Dot

  • Step 5: Extract Segment

  • Step 6: Validate Current Node

  • Step 7: Find Segment in Object

  • Step 8: Move to Child Node

  • Step 9: Update Position

  • Step 10: Return Result

  • Complete Implementation

  • Walkthrough Examples

  • Example 1: Simple Nested Key

  • Example 2: Deep Nesting

  • Example 3: Missing Key

  • Example 4: Non-Object Intermediate

  • Performance Analysis

  • Time Complexity: O(n)

  • Space Complexity: O(1)

  • Common Pitfalls

  • Summary Checklist

  • Transition

  • Module 3 - Lesson 3

  • How get() Uses find_key()

  • The get() Method

  • Walkthrough Examples

  • Example 1: Simple Nested Access

  • Example 2: Missing Nested Key

  • Example 3: Type Mismatch

  • try_get() Implementation

  • has() Implementation

  • Deep Nesting Support

  • Example: 5-Level Nesting

  • Example: Partial Path Match

  • Error Message Context

  • Why Include Full Key Path?

  • Performance Considerations

  • No Extra Copies

  • Single Pass

  • Common Pitfalls

  • Summary Checklist

  • Transition

  • Module 3 - Lesson 4

  • The Question

  • Why No Limit?

  • Iterative, Not Recursive

  • Memory Usage: O(1)

  • Walkthrough Examples

  • Example 1: 5-Level Nesting

  • Example 2: 10-Level Nesting

  • Example 3: 100-Level Nesting (Theoretical)

  • Performance Comparison

  • Time Complexity: O(n)

  • Space Complexity: O(1)

  • Why This Matters

  • Real-World Use Cases

  • Configuration Management

  • Comparison with Alternatives

  • Array-Based Access (Hypothetical)

  • Path Objects (Hypothetical)

  • Dot-Notation Wins

  • Edge Cases

  • Empty Segments

  • Trailing Dots

  • Consecutive Dots

  • Memory Profile

  • Stack Usage (per call)

  • Heap Usage

  • Scalability Analysis

  • Maximum Practical Depth

  • When to Worry

  • Common Pitfalls

  • Summary Checklist

  • Transition

  • Module 3 - Lesson 5

  • Why Context Matters

  • Without Context (Bad)

  • With Context (Good)

  • Error Message Components

  • 1. Source Identifier

  • 2. Action Description

  • 3. Key Path

  • 4. Requested Type

  • 5. Original Error

  • Implementation

  • Type Conversion Error

  • Error Message Construction

  • Walkthrough Examples

  • Example 1: Simple Key

  • Example 2: Nested Key

  • Example 3: Deeply Nested

  • try_get() Error Handling

  • Best Practices for Error Messages

  • 1. Include Key Name

  • 2. Preserve Original Error

  • 3. Use Consistent Format

  • 4. Be Specific

  • Error Message Examples

  • Missing File

  • Parse Error

  • Type Conversion

  • Missing Key (with get)

  • Missing Key (with try_get)

  • Debugging Workflow

  • Step 1: Read the Error Message

  • Step 2: Identify the Key

  • Step 3: Check the Value

  • Step 4: Fix the Value

  • Common Pitfalls

  • Summary Checklist

  • Transition

  • Module 3 - Lesson 6

  • Why Parse Error Handling Matters

  • Without Proper Handling (Bad)

  • With Proper Handling (Good)

  • nlohmann::json Parse Errors

  • Error Type

  • Error Information

  • Example Error

  • Implementation

  • load() Method with Error Handling

  • Error Message Construction

  • Walkthrough Examples

  • Example 1: Syntax Error

  • Example 2: Trailing Comma

  • Example 3: Missing Quote

  • Error Message Format

  • Consistent Pattern

  • Why This Format?

  • Benefits

  • try_get() Error Handling

  • get() Error Handling

  • Error Handling Flow

  • Common Parse Errors

  • 1. Syntax Errors

  • 2. Trailing Commas

  • 3. Invalid Strings

  • 4. Invalid Numbers

  • 5. Invalid Booleans

  • Best Practices

  • 1. Always Include File Path

  • 2. Preserve Original Error

  • 3. Use std::runtime_error

  • 4. Be Specific

  • Common Pitfalls

  • Summary Checklist

  • Transition

  • Module 3 - Lesson 7

  • Why File Error Handling Matters

  • Without Proper Handling (Bad)

  • With Proper Handling (Good)

  • File Opening Errors

  • Common Scenarios

  • Implementation

  • load() Method with File Error Handling

  • Error Message Construction

  • Walkthrough Examples

  • Example 1: File Doesn’t Exist

  • Example 2: Wrong Working Directory

  • Example 3: Permission Denied

  • Example 4: Directory Instead of File

  • Error Message Format

  • Consistent Pattern

  • Why This Format?

  • Benefits

  • File Error vs Parse Error

  • File Error (load() - first check)

  • Parse Error (load() - after file open)

  • Error Handling Flow

  • Default Configuration Path

  • Default Path

  • Constructor with Default

  • Usage Examples

  • Best Practices

  • 1. Always Check File Open

  • 2. Include Full Path

  • 3. Use std::runtime_error

  • 4. Be Specific

  • Common Pitfalls

  • Testing File Errors

  • Test File Not Found

  • Test Default Path

  • Test Permission Denied

  • Summary Checklist

  • Transition

  • Module 3 - Quiz

  • Quiz

    3 attempts allowed

  • Module 4 - Compile-Time Reflection

  • Lessons

  • Duration

  • Learning Objectives

  • Capstone Project

  • Quiz

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub