dl
dl_json.h
浏览该文件的文档.
1
12#pragma once
13
14#include <json.hpp>
15
16#include "io/dl_log.h"
17#include "memory/dl_buffer.h"
18
19using json = nlohmann::json;
20
21namespace dl::Json
22{
23
24 extern bool g_bJsonLog;
25
26
30void SetLog(bool open);
31
33{
34 DisableLog() { SetLog(false); }
35 ~DisableLog() { SetLog(true); }
36};
37
42json Parse(std::string_view path_name);
43
49
53template<typename T>
54bool Get(const json& j, const std::string& name, T& t)
55{
56 auto iter = j.find(name);
57 if (iter == j.end())
58 {
59 if(g_bJsonLog)
60 log_err("json取值失败:{}", name);
61 return false;
62 }
63 try
64 {
65 t = *iter;
66 return true;
67 }
68 catch (nlohmann::detail::exception& e)
69 {
70 if (g_bJsonLog)
71 log_err("json取值失败({}): {}", name, e.what());
72 }
73 return false;
74}
75
78inline std::string Dump(json& j)
79{
80 return j.dump(4);
81}
82
85inline std::string Dump(BufferView buf)
86{
87 json j = Parse(buf);
88 if (j.is_null())
89 return {};
90 return Dump(j);
91}
92
93}
94
表示一段内存
nlohmann::json json
日志系统
#define log_err(...)
void SetLog(bool open)
设置是否提示输出日志(请直接使用DisableLog来关闭后,自动恢复)
json Parse(std::string_view path_name)
从 文件 构建json对象
bool g_bJsonLog
bool Get(const json &j, const std::string &name, T &t)
从json返回值,不存在则返回false,打印日志
std::string Dump(json &j)
dump
std::span< std::byte > BufferView