跳转至

ev-json-byte-span-parse-test-003

字段 值
项目 nlohmann/json
固定 commit 55f93686c015
路径 tests/src/unit-regression2.cpp
行段 L747-L756
特性 std::byte
许可证 MIT
代码来源 项目源码
级别 manual
审核 zyuhao7 @ 2026-10-24
测试代码 是
发现方式 regex

用法

用 std::as_bytes 构造字节视图并解析

为什么典型

展示二进制缓冲区转 std::byte 视图后直接作为解析输入的标准形态,不经过字符串拷贝。

摘录

#ifdef JSON_HAS_CPP_20
#ifndef _LIBCPP_VERSION // see https://github.com/nlohmann/json/issues/4490
#if __has_include(<span>)
    SECTION("issue #2546 - parsing containers of std::byte")
    {
        const char DATA[] = R"("Hello, world!")"; // NOLINT(misc-const-correctness,cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
        const auto s = std::as_bytes(std::span(DATA));
        const json j = json::parse(s);
        CHECK(j.dump() == "\"Hello, world!\"");
    }

备注

测试证据;条件编译覆盖 C++20 与 span 可用性。

查看固定 commit 源码 · 返回证据索引