Nlohmann Json“ array”和“ array_t”之间的区别

问题描述

nlohmann array_t 与Nlohmann json array 有何不同?实际如何使用 array_t ?每个文档的文档如下。

array

从给定的初始化列表创建一个JSON数组值。也就是说,给定值列表a,b,c,将创建JSON值[a,b,c]。如果初始化列表为空,则创建空数组[]。

示例

nlohmann::json j_nonempty_init_list = json::array({1,2,3,4});
std::cout << j_nonempty_init_list;
// [1,4]

array_t

要在C ++中存储对象,将通过下面介绍的模板参数定义类型。

  • 模板参数
    • ArrayType 容器类型以存储数组(例如std :: vector或std :: list)
    • AllocatorType 分配器,用于数组(例如std :: allocator)

解决方法

<body>是一个静态函数,它将初始化程序列表转换为内部类型为json::array的{​​{1}}值。

json的定义如下:

value_t::array

其中array_tusing array_t = ArrayType<basic_json,AllocatorType<basic_json>>; 的模板模板参数:

ArrayType

最后,basic_json只是默认实例:

template<template<typename U,typename V,typename... Args> class ObjectType =
         std::map,template<typename U,typename... Args> class ArrayType = std::vector,class StringType = std::string,class BooleanType = bool,class NumberIntegerType = std::int64_t,class NumberUnsignedType = std::uint64_t,class NumberFloatType = double,template<typename U> class AllocatorType = std::allocator,template<typename T,typename SFINAE = void> class JSONSerializer =
         adl_serializer,class BinaryType = std::vector<std::uint8_t>>
class basic_json;

因此,nlohmann::json最后是using json = basic_json<>; 的类型别名。
如果您在json::array_t上创建自己的typedef,则可以覆盖传递给std::vector<json>的类型。