core-adapters 0.1.1
Third-party adapters for snicholls-core: nanobind helpers, nlohmann/json hooks, the snicholls_core Python package
Loading...
Searching...
No Matches
uuid_json.hpp File Reference

nlohmann/json hooks for snicholls::uuid: the canonical lowercase 8-4-4-4-12 string. More...

#include <optional>
#include <string>
#include <snicholls/core_adapters/nlohmann/json.hpp>
#include <snicholls/core/uuid.hpp>

Go to the source code of this file.

Functions

void snicholls::to_json (nlohmann::json &j, const uuid &u)
 Writes u as its canonical lowercase string.
 
void snicholls::from_json (const nlohmann::json &j, uuid &u)
 Reads a canonical 8-4-4-4-12 string (either case).
 

Detailed Description

nlohmann/json hooks for snicholls::uuid: the canonical lowercase 8-4-4-4-12 string.

Overview
nlohmann::json j = id; writes "f81d4fae-7dec-11d0-a765-00a0c91e6bf6" (snicholls::to_text, lowercase). j.get<snicholls::uuid>() reads the canonical form in either case (snicholls::parse_uuid with default options); braces, urn:uuid: and the 32-digit form are refused, as is a JSON number or byte array.
const auto id = "0190A5C4-7E1B-7C3A-9F12-3B4C5D6E7F80"_uuid;
const nlohmann::json j = {{"order", id}}; // written lowercase
std::printf("%s\n", j.dump().c_str());
const auto back = j.at("order").get<snicholls::uuid>();
Relationships
Guarantees and contracts
  • Round trip for every 128-bit value; to_json throws only std::bad_alloc.
Errors
snicholls::json_error: not a string, or not a canonical UUID.
Performance
Not benchmarked: to_text / parse_uuid plus one std::string.
Tests and demos
  • Tests: tests/nlohmann/uuid_json_test.cpp.
  • Demo: demos/nlohmann/uuid_json_demo.cpp.
Dependencies
CMake target snicholls::core_json.
Known issues and gotchas
  • None known.
Status
Experimental, pre-1.0.

Definition in file uuid_json.hpp.