Back to Site
Loading...
Searching...
No Matches
json.h File Reference

A small, self-contained JSON value type for librats. More...

#include "librats/util/rats_export.h"
#include <cstdint>
#include <initializer_list>
#include <iosfwd>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
Include dependency graph for json.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  librats::JsonError
 Thrown by the throwing parse path and by type-mismatched accessors. More...
 
class  librats::Json
 
class  librats::Json::Object
 Insertion-ordered string->Json map with O(1) average lookup. More...
 
class  librats::Json::Iterator< Const >
 
class  librats::Json::ItemsProxy< Const >
 A key/value view supporting structured bindings: for (auto&& [key, val] : obj.items()) { ... } For arrays, key() is the decimal index. More...
 

Namespaces

namespace  librats
 

Functions

std::istream & librats::operator>> (std::istream &is, Json &j)
 
std::ostream & librats::operator<< (std::ostream &os, const Json &j)
 

Detailed Description

A small, self-contained JSON value type for librats.

librats::Json is a dynamically-typed JSON value with a deliberately nlohmann-flavoured surface so existing call sites read naturally:

Json j; // null j["name"] = "rats"; // becomes an object j["count"] = 42; // signed integer j["tags"] = Json::array(); // empty array j["tags"].push_back("p2p");

Json cfg = { // initializer-list "object" detection, {"version", 1}, // exactly like nlohmann: a list whose {"nodes", {{"id", "ab"}}}, // every element is a [string, value] pair }; // is treated as an object.

std::string text = j.dump(); // compact std::string nice = j.dump(2); // pretty, 2-space indent Json back = Json::parse(text); // throws on malformed input Json safe = Json::parse(text, nullptr, false); // never throws; see is_discarded()

Design notes:

  • Numbers keep their kind (signed / unsigned / floating) so integers round-trip exactly and large 64-bit values are not silently truncated.
  • Objects preserve insertion order while offering O(1) average key lookup.
  • Heavy payloads (string / array / object) live behind a pointer, so a Json value is small (a tag plus one word) and cheap to move.
  • The parser is iterative-friendly recursive descent with a depth guard, so adversarial deeply-nested input fails cleanly instead of smashing the stack.

Definition in file json.h.