Project 7: JSON-RPC Server

A server implementing JSON-RPC 2.0 over TCP—clients send JSON requests with method names and parameters, server executes the method and returns JSON results. Include error handling per the spec.

Quick Reference

Attribute Value
Primary Language C++
Alternative Languages Rust, Go, Python
Difficulty Level 2: Intermediate
Time Estimate 1 week
Knowledge Area RPC, JSON Serialization, Protocol Design
Tooling Microservice backend
Prerequisites Project 2, familiarity with JSON

What You Will Build

A server implementing JSON-RPC 2.0 over TCP—clients send JSON requests with method names and parameters, server executes the method and returns JSON results. Include error handling per the spec.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Message framing → maps to length-prefix or delimiter-based framing
  • JSON parsing and generation → maps to using nlohmann/json or similar
  • Method dispatch → maps to runtime function lookup
  • Error codes and responses → maps to JSON-RPC 2.0 error format

Key Concepts

  • JSON-RPC 2.0 Specification: https://www.jsonrpc.org/specification
  • Message Framing: “Designing Data-Intensive Applications” Chapter 4 - Kleppmann
  • nlohmann/json Library: https://github.com/nlohmann/json
  • RPC Patterns: “Patterns of Enterprise Application Architecture” - Fowler

Real-World Outcome

$ ./jsonrpc_server 9000
JSON-RPC server listening on port 9000
Registered methods: add, subtract, multiply, divide, echo

# Client sends (with 4-byte length prefix):
{"jsonrpc": "2.0", "method": "add", "params": [1, 2], "id": 1}

# Server responds:
{"jsonrpc": "2.0", "result": 3, "id": 1}

# Error case:
{"jsonrpc": "2.0", "method": "divide", "params": [1, 0], "id": 2}

# Response:
{"jsonrpc": "2.0", "error": {"code": -32000, "message": "Division by zero"}, "id": 2}

# Unknown method:
{"jsonrpc": "2.0", "method": "unknown", "params": [], "id": 3}

# Response:
{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": 3}

Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: LEARN_CPP_NETWORK_PROGRAMMING.md
  • “Designing Data-Intensive Applications” by Martin Kleppmann