|
|
|||
File indexing completed on 2026-11-07 09:44:12
0001 // This file is part of the ACTS project. 0002 // 0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project 0004 // 0005 // This Source Code Form is subject to the terms of the Mozilla Public 0006 // License, v. 2.0. If a copy of the MPL was not distributed with this 0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/. 0008 0009 #pragma once 0010 0011 #include <cstddef> 0012 #include <filesystem> 0013 #include <span> 0014 #include <vector> 0015 0016 #include <nlohmann/json.hpp> 0017 0018 namespace Acts::detail { 0019 0020 /// How a JSON document is encoded on disk. 0021 enum class JsonEncoding { 0022 /// Human readable JSON text 0023 Text, 0024 /// Binary CBOR, as produced by `nlohmann::json::to_cbor` 0025 Cbor, 0026 }; 0027 0028 /// Compression applied on top of the encoded payload. 0029 enum class JsonCompression { 0030 /// The encoded payload is stored verbatim 0031 None, 0032 /// The encoded payload is wrapped in a single zstd frame 0033 Zstd, 0034 }; 0035 0036 /// The on-disk format of a JSON document: how it is encoded, and whether it 0037 /// is compressed. The two are independent, so all four combinations are 0038 /// valid. 0039 struct JsonFileFormat { 0040 /// Payload encoding 0041 JsonEncoding encoding{JsonEncoding::Text}; 0042 /// Payload compression 0043 JsonCompression compression{JsonCompression::None}; 0044 }; 0045 0046 /// Whether this build can read and write zstd compressed payloads. 0047 /// 0048 /// zstd is an optional dependency: when it is not found at configure time, 0049 /// writing compressed output and reading a compressed file both throw. 0050 /// 0051 /// @return true if zstd support was compiled in 0052 bool zstdSupported(); 0053 0054 /// Deduce the on-disk format from a file name. 0055 /// 0056 /// The file name is the only specification of the output format, which keeps 0057 /// it honest about its own contents: 0058 /// - `.json` text, uncompressed 0059 /// - `.cbor` CBOR, uncompressed 0060 /// - `.json.zst` text, zstd compressed 0061 /// - `.cbor.zst` CBOR, zstd compressed 0062 /// 0063 /// @param path the file name to inspect 0064 /// @throw std::invalid_argument if the extension is not one of the above 0065 /// @return the deduced format 0066 JsonFileFormat jsonFormatFromPath(const std::filesystem::path& path); 0067 0068 /// Encode a JSON payload into its on-disk representation. 0069 /// 0070 /// @param payload the JSON document to encode 0071 /// @param format the target encoding and compression 0072 /// @param indentation indentation for text output, ignored for CBOR 0073 /// @param compressionLevel zstd level, ignored when not compressing 0074 /// @return the encoded bytes 0075 std::vector<std::byte> encodeJson(const nlohmann::json& payload, 0076 const JsonFileFormat& format, 0077 unsigned indentation, int compressionLevel); 0078 0079 /// Decode a JSON payload, detecting encoding and compression from content. 0080 /// 0081 /// Compression is detected from the zstd frame magic, and the encoding from 0082 /// the leading byte of the (decompressed) payload, so the caller does not 0083 /// need to know or declare the format. The file name is used only to make 0084 /// error messages point at the offending file. 0085 /// 0086 /// @param data the encoded bytes 0087 /// @param origin file the data came from, used for diagnostics only 0088 /// @return the decoded JSON document 0089 nlohmann::json decodeJson(std::span<const std::byte> data, 0090 const std::filesystem::path& origin = {}); 0091 0092 /// Write a JSON payload to a file, in the format its name implies. 0093 /// 0094 /// @param path destination, whose extension selects the format 0095 /// @param payload the JSON document to write 0096 /// @param indentation indentation for text output, ignored for CBOR 0097 /// @param compressionLevel zstd level, ignored when not compressing 0098 void writeJsonFile(const std::filesystem::path& path, 0099 const nlohmann::json& payload, unsigned indentation, 0100 int compressionLevel); 0101 0102 /// Read a JSON payload from a file, whatever format it is in. 0103 /// 0104 /// @param path the file to read 0105 /// @return the decoded JSON document 0106 nlohmann::json readJsonFile(const std::filesystem::path& path); 0107 0108 } // namespace Acts::detail
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|