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
22namespace dl::Json
23{
24
25 extern bool g_bJsonLog;
26
30void SetLog(bool open);
31
34{
35 DisableLog() { SetLog(false); }
36 ~DisableLog() { SetLog(true); }
37};
38
43json Parse(std::string_view path_name);
44
50
54template<typename T>
55bool Get(const json& j, const std::string& name, T& t)
56{
57 auto iter = j.find(name);
58 if (iter == j.end())
59 {
60 if(g_bJsonLog)
61 log_err("json取值失败:{}", name);
62 return false;
63 }
64 try
65 {
66 t = *iter;
67 return true;
68 }
69 catch (nlohmann::detail::exception& e)
70 {
71 // 存在但失败,必须报错
72 log_err("json取值失败({}): {}", name, e.what());
73 }
74 return false;
75}
76
79template<typename T>
80bool Get(const json& j, T& t)
81{
82 try
83 {
84 t = j;
85 return true;
86 }
87 catch (nlohmann::detail::exception& e)
88 {
89 // 存在但失败,必须报错
90 log_err("json取值失败: {}", e.what());
91 }
92 return false;
93}
94
98inline std::string Dump(json& j)
99{
100 return j.dump(4);
101}
102
105inline std::string Dump(BufferView buf)
106{
107 json j = Parse(buf);
108 if (j.is_null())
109 return {};
110 return Dump(j);
111}
112
113}
114
表示一段内存
nlohmann::json json
日志系统
#define log_err(...)
json格式
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