.hpp⽂件_Json⽂件解析(上)
Json⽂件解析(上)
⾃述⽂件
alt=GitHub赞助商 data-canonical-src="img.shields.io/badge/GitHub-Sponsors-ff69b4" v:shapes="_x0000_i1025">设计⽬标
赞助商
积分
CMake的
包装经理
包配置
例⼦
JSON作为⼀流的数据类型
序列化/反序列化
类似STL的访问
从STL容器转换
JSON指针和JSON补丁
JSON合并补丁
隐式转换
python解析json文件到/从任意类型的转换
专门进⾏枚举转换
⼆进制格式(BSON,CBOR,MessagePack和UBJSON)
⽀持的编译器
执照
联系
谢谢
⼆⼿第三⽅⼯具
使⽤JSON for Modern C ++的项⽬
笔记
输入框是什么意思执⾏单元测试
设计⽬标
那⾥有⽆数的JSON库,每个库甚⾄都有其存在的理由。班有以下设计⽬标:
直观的语法。在Python等语⾔中,JSON感觉就像是⼀流的数据类型。使⽤了现代C ++的所有操作符魔术,以在代码中实现相同的感直观的语法
觉。查看以下⽰例,将了解意思。
微不⾜道的整合。整个代码包含⼀个头⽂件json.hpp。没有库,没有⼦项⽬,没有依赖项,没有复杂的构建系统。该类⽤⾹草C ++微不⾜道的整合
11编写。总⽽⾔之,⼀切都不需要调整编译器标志或项⽬设置。
dede底部模板认真测试。项⽬经过严格的单元测试,涵盖了100%的代码,包括所有异常⾏为。此外,使⽤Valgrind和Clang⽹络检查是否有内存泄认真测试
漏。Google OSS-Fuzz还针对所有解析器24/7运⾏模糊测试,到⽬前为⽌,有效执⾏了数⼗亿次测试。为了保持⾼质量,该项⽬遵循核⼼基础设施计划(CII)最佳实践。
其⽅⾯对⽽⾔并不那么重要:
存储效率。每个JSON对象的开销为⼀个指针(联合的最⼤⼤⼩)和⼀个枚举元素(1个字节)。默认归纳使⽤以下C ++数据类型:存储效率
std::string⽤于字符串int64_t,uint64_t或double数字,std::map对象,std::vector数组和bool布尔值。但是,可以根据
basic_json需要对通⽤类进⾏模板化。
速度。当然,那⾥有更快的JSON库。但是,如果⽬标是通过使⽤单个标头添加JSON⽀持来加快开发速度,那么该库就是理想之选。
速度
如果知道如何使⽤std::vector或std::map,那么已经设置好了。
积分
json.hpp是此处single_include/nlohmann或发布的单个必需⽂件。需要添加
#包括 < nlohmann / json.hpp >
//为了⽅便
使⽤ json = nlohmann :: json;
到要处理JSON的⽂件,并设置必要的开关以启⽤C ++ 11(例如,-std=c++11对于GCC和Clang)。
可以进⼀步使⽤file include/nlohmann/json_fwd.hpp进⾏前向声明。json_fwd.hpp的安装(作为cmake安装步骤的⼀部分)可以通过设置来实现-DJSON_MultipleHeaders=ON。
CMake的
也可以nlohmann_json::nlohmann_json在CMake中使⽤接⼝⽬标。此⽬标填充适当的使⽤要
求,INTERFACE_INCLUDE_DIRECTORIES以指向适当的包含⽬录和INTERFACE_COMPILE_FEATURES必要的C ++ 11标志。
外部
要从CMake项⽬中使⽤此库,可以直接从中到,find_package()并使⽤⽣成的程序包配置中的命名空间导⼊⽬标:
#
find_package(nlohmann_json 3.2.0 REQUIRED)
...
add_library(foo ...)
...
target_link_libraries(foo PRIVATE nlohmann_json :: nlohmann_json)
软件包配置⽂件ake可以在安装树中使⽤,也可以在构建树之外直接使⽤。
嵌⼊式的
要将库直接嵌⼊到现有CMake项⽬中,请将整个源代码树放置在⼦⽬录中,然后调⽤add_subdirectory()⽂件:
linux修改文件命令#通常不那么关⼼第三⽅库的测试是
#从⾃⼰的项⽬的代码运⾏。
设置(JSON_BuildTests OFF CACHE INTERNAL “”)
#如果仅在私有源⽂件中包含此第三⽅,则
#在安装主项⽬时不需要安装。
#集(JSON_Install OFF CACHE INTERNAL “”)
#请勿使⽤include(nlohmann_json / ),因为会附带
#会导致构建中断的意外后果。通常
#⿎励(虽然不⼀定有据可查这样)使⽤
#包括(...),⽤于其项⽬的CMake反正拉动。
add_sub⽬录(nlohmann_json)
...
add_library(foo ...)
...
target_link_libraries(foo PRIVATE nlohmann_json :: nlohmann_json)
嵌⼊式(FetchContent)
从CMake v3.11开始, FetchContent可以作为配置类型的依赖项⾃动下载资源库。
例:
包括(FetchContent)
python读写xmlFetchContent_Declare(json
GIT_TAG v3.7.3)
FetchContent_GetProperties(json)
html点击按钮跳转页面如果(NOT json_POPULATED)
FetchContent_Populate(json)
add_subdirectory($ {json_SOURCE_DIR} $ {json_BINARY_DIR} EXCLUDE_FROM_ALL)endif()
target_link_libraries(foo PRIVATE nlohmann_json :: nlohmann_json)
同时⽀持
为了允许项⽬⽀持外部提供的或嵌⼊式JSON库,可以使⽤类似于以下内容的模式:
# Top
project(FOO)
...
option(FOO_USE_EXTERNAL_JSON "Use an external JSON library" OFF)
...
add_subdirectory(thirdparty)
...
add_library(foo ...)
...
# Note that the namespaced target will always be available regardless of the
# import method
target_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json)
#
...
if(FOO_USE_EXTERNAL_JSON)
find_package(nlohmann_json 3.2.0 REQUIRED)
else()
set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(nlohmann_json)
endif()
...
thirdparty/nlohmann_json 然后是此源树的完整副本。
包管理Package Managers
If you are using OS X and Homebrew, just type brew tap nlohmann/json and brew install nlohmann-json and you're set. If you want the bleeding edge rather than the latest release, use brew install nlohmann-json --HEAD.
If you are using the Meson Build System, add this source tree as a meson subproject. You may also use the include.zip published in this project's Releases to reduce the size of the vendored source tree. Alternatively, you can get a wrap file by downloading it from Meson WrapDB, or simply use meson wrap install nlohmann_json. Please see the meson project for any issues regarding the packaging.
The provided meson.build can also be used as an alternative to cmake for installing nlohmann_json system-wide in which case a pkg-config file is installed. To use it, simply have your build system require the nlohmann_json pkg-config dependency. In Meson, it is preferred to use the dependency() object with a subproject fallback, rather than using the subproject directly.
If you are using Conan to manage your dependencies, merely add nlohmann_ to your conanfile's requires, is the release version you want to use. Please file issues here if you experience problems with the packages.
If you are using Spack to manage your dependencies, you can use the nlohmann-json package. Please see the spack project for any issues regarding the packaging.
If you are using hunter on your project for external dependencies, then you can use the nlohmann_json package. Please see the hunter project for any issues regarding the packaging.
If you are using Buckaroo, you can install this library's module with buckaroo add github/buckaroo-pm/nlohmann-json. Please file issues here. There is a demo repo here.
If you are using vcpkg on your project for external dependencies, then you can use the nlohmann-json package. Please see the vcpkg project for any issues regarding the packaging.
If you are using cget, you can install the latest development version with cget install nlohmann/json. A specific version can be installed with cget install nlohmann/json@v3.1.0. Also, the multiple header version can be installed by adding the -
DJSON_MultipleHeaders=ON flag (i.e., cget install nlohmann/json -DJSON_MultipleHeaders=ON).
If you are using CocoaPods, you can use the library by adding pod "nlohmann_json", '~>3.1.2' to your podfile (see an example). Please file issues here.
If you are using NuGet, you can use the package nlohmann.json. Please check this extensive description on how to use the package. Please files issues here.
If you are using conda, you can use the package nlohmann_json from conda-forge executing conda install -c conda-forge nlohmann_json. Please file issues here.
If you are using MSYS2, your can use the mingw-w64-nlohmann-json package, just type pacman -S mingw-w64-i686-nlohmann-json or pacman -S mingw-w64-x86_64-nlohmann-json for installation. Please file issues here if you experience problems with the packages.
If you are using wsjcpp, you can use the command wsjcpp install "github/nlohmann/json:develop" to get the latest version. Note you can change the branch ":develop" to an existing tag or another branch。
包配置
如果使⽤的是裸Makefile,则可以使⽤pkg-config⽣成指向库安装位置的include标志:
pkg-config nlohmann_json --cflags
Meson构建系统的⽤户还可以使⽤系统范围的库,该库可以通过pkg-config以下⽅式到:
json = dependency('nlohmann_json', required: true)
例⼦
除了下⾯的⽰例,可能想查看⽂档,其中每个函数都包含⼀个单独的代码⽰例(例如,检出emplace())。所有⽰例⽂件都可以单独编译和执⾏(例如,⽂件emplace.cpp)。
JSON作为⼀流的数据类型
以下是⼀些⽰例,可让了解如何使⽤该类。
假设要创建JSON对象
{
"pi": 3.141,
"happy": true,
"name": "Niels",
"nothing": null,
"answer": {
"everything": 42
},
"list": [1, 0, 2],
"object": {
"currency": "USD",
"value": 42.99
}
}
使⽤该库,可以编写:
//创建⼀个空结构(空)
json j;
//添加⼀个存储为double的数字(注意j到对象的隐式转换)
j [ “ pi ” ] = 3.141 ;
//添加⼀个存储为bool
j [ “ happy ” ] = true的布尔值;
//添加⼀个存储为std :: string
j [ “ name ” ] = “ Niels ”的字符串;
//通过传递nullptr
j [ “ nothing ” ] = nullptr来添加另⼀个null对象;
//在对象
j [ “ answer ” ] [ “ everything ” ] = 42 内添加⼀个对象;
/
/添加存储为标准::向量(使⽤初始化列表)的阵列
f] [ “列表” ] = { 1, 0, 2 };
//添加另⼀个对象(使⽤成对的初始化列表)
j [ “ object ” ] = {{ “ currency ”, “ USD ” },{ “ value ”, 42.99 }};
//相反,也可以编写(看起来与上⾯的JSON⾮常相似)
json j2 = {
{ “ pi ”,3.141 },
{ “ happy ”,true },
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论