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
28json Parse(std::string_view path_name);
29
35
36
40template<typename T>
41bool Get(const json& j, const std::string& name, T& t)
42{
43 auto iter = j.find(name);
44 if (iter == j.end())
45 return false;
46 try
47 {
48 t = *iter;
49 return true;
50 }
51 catch (nlohmann::detail::exception&)
52 {
53 }
54 return false;
55}
56
60template<typename T>
61bool GetLog(const json& j, const std::string& name, T& t)
62{
63 auto iter = j.find(name);
64 if (iter == j.end())
65 {
66 log_err("json取值失败:{}", name);
67 return false;
68 }
69 try
70 {
71 t = *iter;
72 return true;
73 }
74 catch (nlohmann::detail::exception& e)
75 {
76 log_err("json取值失败({}): {}", name, e.what());
77 }
78 return false;
79}
80
81
82}
83
表示一段内存
nlohmann::json json
Definition: dl_json.h:19
日志系统
#define log_err(...)
Definition: dl_log.h:196
bool GetLog(const json &j, const std::string &name, T &t)
从json返回值,不存在则返回false,打印日志
Definition: dl_json.h:61
json Parse(std::string_view path_name)
从 文件 构建json对象
bool Get(const json &j, const std::string &name, T &t)
从json返回值,不存在则返回false,并且不进行任何操作
Definition: dl_json.h:41
std::span< std::byte > BufferView
Definition: dl_buffer.h:22