nlohmann json C++ 解析

学习材料:nlohmann json

json官方

源码解析

源码

要学习并理解这份代码,可以按照以下步骤进行,逐步梳理代码的逻辑:

基本步骤:

  1. 配置宏:

    • 理解用于配置的宏定义,这些宏控制库的不同特性和行为。
    • 例如,#if JSON_DIAGNOSTICS 用于控制诊断特性的启用。
  2. 核心类 basic_json:

    • 分析 basic_json 类,它是整个库的核心,表示一个 JSON 值。
    • 理解类中的成员变量(如 value_t 枚举和 json_value 联合体)和它们的作用。
    • 阅读并理解类的构造函数、析构函数、拷贝和移动构造函数及赋值运算符。
    • 学习类中的成员函数,例如 operator[]getsetis 等。
  3. 序列化和反序列化:

    • 理解用于将 JSON 值转换为字符串和从字符串转换回 JSON 值的序列化和反序列化逻辑。
    • 例如,json_serializer 类中的 serializedeserialize 方法。
  4. 异常处理:

    • 了解用于处理错误的自定义异常类,例如 json_exception
    • 理解这些异常类如何与标准的异常处理机制集成,并提供有用的错误信息。

basic_json

The basic_json class is the core of the library, encapsulating all JSON types. Its design includes:

Type Management: An internal enum to manage the type of the JSON value.
Storage: A union to store different types efficiently.
Constructors and Destructors: To handle different JSON types.
Operators: Overloaded operators for ease of use.

// basic_json 类用于表示一个 JSON 值
template<typename BasicJsonType>
class basic_json
{
   
private:
    json_value m_value; // 存储 JSON 值的联合体
    value_t m_type = value_t::null; // 当前 JSON 值的类型

public:
    // 构造函数
    basic_json() noexcept;
    basic_json(value_t t) noexcept;

    // 析构函数
    ~basic_json() noexcept;

    // 拷贝和移动构造函数及赋值运算符
    basic_json(const basic_json& other);
    basic_json(basic_json&& other) noexcept;

    basic_json& operator=(const basic_json& other);
    basic_json& operator=(basic_json&& other) noexcept;

    // 成员函数
    // 示例:重载运算符 []
    reference operator[](size_type idx)
    {
   
        // 检查类型并返回数组元素
        if (JSON_UNLIKELY(!is_array()))
        {
   
            throw type_error::create(305, "cannot use operator[] with " + std::string(type_name()));
        }

        return m_value.array->operator[](idx);
    }
};

类型管理

JSON type value_t type used type
object object pointer to @ref object_t
array array pointer to @ref array_t
string string pointer to @ref string_t
boolean boolean @ref boolean_t
number number_integer @ref number_integer_t
number number_unsigned @ref number_unsigned_t
number number_float @ref number_float_t
binary binary pointer to @ref binary_t
null null no value is stored

定义一个枚举来表示 JSON 值的类型

enum class value_t : std::uint8_t
{
   
    null,             ///< null value
    object,           ///< object (unordered set of name/value pairs)
    array,            ///< array (ordered collection of values)
    string,           ///< string value
    boolean,          ///< boolean value
    number_integer,   ///< number value (signed integer)
    number_unsigned,  ///< number value (unsigned integer)
    number_float,     ///< number value (floating-point)
    binary,           ///< binary array (ordered collection of bytes)
    discarded         ///< discarded by the parser callback function
};

C++11枚举类——enum class

联合体用于高效存储不同类型的 JSON 值

    using string_t = StringType;
    using boolean_t = BooleanType;
    using number_integer_t = NumberIntegerType;
    using number_unsigned_t = NumberUnsignedType;
    using number_float_t = NumberFloatType;
    using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
    using object_comparator_t = detail::actual_object_comparator_t<basic_json>;
    
union json_value
{
   
    std::nullptr_t null;
    object_t* object;
    array_t* array;
    string_t* string;
    boolean_t boolean;
    number_integer_t number_integer;
    number_unsigned_t number_unsigned;
    number_float_t number_float;
    binary_t* binary;
    //构造函数
	json_value() = default;
     json_value(boolean_t v) noexcept : boolean(v) {
   }
     json_valu

你可能感兴趣的:(C++,json,c++,开发语言)