Skip to content

Commit b0098a0

Browse files
committed
feat(util): 实现了tbox::util::json::Load()与tbox::util::json::LoadDeeply()无异常函数
1 parent 6dad077 commit b0098a0

File tree

8 files changed

+160
-2
lines changed

8 files changed

+160
-2
lines changed

modules/base/catch_throw.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace tbox {
3535
*/
3636
bool CatchThrow(const std::function<void()> &func,
3737
bool print_backtrace = false,
38-
bool abort_process = false);
38+
bool abort_process = false) noexcept;
3939

4040
/// 同 CatchThrow,但不打印日志、堆栈,也不退出进程
4141
/**
@@ -44,7 +44,7 @@ bool CatchThrow(const std::function<void()> &func,
4444
* \return false 运行过程中没有出现异常
4545
* \return true 运行过程中捕获到了异常
4646
*/
47-
bool CatchThrowQuietly(const std::function<void()> &func);
47+
bool CatchThrowQuietly(const std::function<void()> &func) noexcept;
4848

4949
}
5050

modules/util/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ TEST_CPP_SRC_FILES = \
8181
base64_test.cpp \
8282
checksum_test.cpp \
8383
crc_test.cpp \
84+
json_deep_loader_test.cpp \
8485
execute_cmd_test.cpp \
8586
timestamp_test.cpp \
8687
buffer_test.cpp \

modules/util/json.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,19 @@ Json Load(const std::string &filename)
183183
return js;
184184
}
185185

186+
bool Load(const std::string &filename, Json &js) noexcept
187+
{
188+
try {
189+
js = Load(filename);
190+
return true;
191+
192+
} catch (const std::exception &e) {
193+
LogWarn("load json fail, catch: %s", e.what());
194+
}
195+
196+
return false;
197+
}
198+
186199
/**
187200
* 通过数[],{},"的方式找JSON字串的结束位置
188201
*/

modules/util/json.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ struct ParseJsonFileError : public std::runtime_error {
8787
*/
8888
Json Load(const std::string &filename);
8989

90+
/// 不抛异常的,只关注成功与否,使用简单
91+
bool Load(const std::string &filename, Json &js) noexcept;
92+
9093
/// 从字串中找到JSON的结束位置
9194
/**
9295
* \param str_ptr JSON字串地址

modules/util/json_deep_loader.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "json_deep_loader.h"
2121
#include <fstream>
2222

23+
#include <tbox/base/log.h>
2324
#include <tbox/base/json.hpp>
2425
#include "json.h"
2526
#include "string.h"
@@ -113,6 +114,18 @@ Json LoadDeeply(const std::string &filename) {
113114
return DeepLoader().load(filename);
114115
}
115116

117+
bool LoadDeeply(const std::string &filename, Json &js) noexcept {
118+
try {
119+
js = DeepLoader().load(filename);
120+
return true;
121+
122+
} catch (const std::exception &e) {
123+
LogWarn("load json deeply fail, catch: %s", e.what());
124+
}
125+
126+
return false;
127+
}
128+
116129
}
117130
}
118131
}

modules/util/json_deep_loader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ class DeepLoader {
124124
*/
125125
Json LoadDeeply(const std::string &filename);
126126

127+
/// 不抛异常的,只关注成功与否,使用简单
128+
bool LoadDeeply(const std::string &filename, Json &js) noexcept;
129+
127130
}
128131
}
129132
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* .============.
3+
* // M A K E / \
4+
* // C++ DEV / \
5+
* // E A S Y / \/ \
6+
* ++ ----------. \/\ .
7+
* \\ \ \ /\ /
8+
* \\ \ \ /
9+
* \\ \ \ /
10+
* -============'
11+
*
12+
* Copyright (c) 2025 Hevake and contributors, all rights reserved.
13+
*
14+
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
15+
* Use of this source code is governed by MIT license that can be found
16+
* in the LICENSE file in the root of the source tree. All contributing
17+
* project authors may be found in the CONTRIBUTORS.md file in the root
18+
* of the source tree.
19+
*/
20+
#include <gtest/gtest.h>
21+
#include <tbox/base/json.hpp>
22+
#include "json_deep_loader.h"
23+
#include "fs.h"
24+
#include "json.h"
25+
26+
namespace tbox {
27+
namespace util {
28+
namespace json {
29+
30+
TEST(JsonDeepLoader, Load) {
31+
const char *filepath = "/tmp/tbox-json-deep-loader-test.json";
32+
const char *json_text = \
33+
R"(
34+
{
35+
"int": 123,
36+
"string": "hello",
37+
"array": [1, 2, 3]
38+
}
39+
)";
40+
Json js;
41+
42+
fs::WriteStringToTextFile(filepath, json_text);
43+
EXPECT_NO_THROW({ js = LoadDeeply(filepath); } );
44+
45+
int i_value;
46+
GetField(js, "int", i_value);
47+
EXPECT_EQ(i_value, 123);
48+
49+
fs::RemoveFile(filepath);
50+
EXPECT_ANY_THROW({ js = Load(filepath); } );
51+
}
52+
53+
TEST(JsonDeepLoader, LoadNoThrow) {
54+
const char *filepath = "/tmp/tbox-json-deep-loader-test.json";
55+
const char *json_text = \
56+
R"(
57+
{
58+
"int": 123,
59+
"string": "hello",
60+
"array": [1, 2, 3]
61+
}
62+
)";
63+
Json js;
64+
65+
fs::WriteStringToTextFile(filepath, json_text);
66+
EXPECT_TRUE(LoadDeeply(filepath, js));
67+
68+
int i_value;
69+
GetField(js, "int", i_value);
70+
EXPECT_EQ(i_value, 123);
71+
72+
fs::RemoveFile(filepath);
73+
EXPECT_FALSE(Load(filepath, js));
74+
}
75+
76+
}
77+
}
78+
}

modules/util/json_test.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <gtest/gtest.h>
2121
#include <tbox/base/json.hpp>
2222
#include "json.h"
23+
#include "fs.h"
2324

2425
namespace tbox {
2526
namespace util {
@@ -159,6 +160,52 @@ TEST(Json, HasField)
159160
EXPECT_FALSE(HasArrayField(js, "string"));
160161
}
161162

163+
TEST(Json, Load) {
164+
const char *filepath = "/tmp/tbox-json-test.json";
165+
const char *json_text = \
166+
R"(
167+
{
168+
"int": 123,
169+
"string": "hello",
170+
"array": [1, 2, 3]
171+
}
172+
)";
173+
Json js;
174+
175+
fs::WriteStringToTextFile(filepath, json_text);
176+
EXPECT_NO_THROW({ js = Load(filepath); } );
177+
178+
int i_value;
179+
GetField(js, "int", i_value);
180+
EXPECT_EQ(i_value, 123);
181+
182+
fs::RemoveFile(filepath);
183+
EXPECT_ANY_THROW({ js = Load(filepath); } );
184+
}
185+
186+
TEST(Json, LoadNoThrow) {
187+
const char *filepath = "/tmp/tbox-json-test.json";
188+
const char *json_text = \
189+
R"(
190+
{
191+
"int": 123,
192+
"string": "hello",
193+
"array": [1, 2, 3]
194+
}
195+
)";
196+
Json js;
197+
198+
fs::WriteStringToTextFile(filepath, json_text);
199+
EXPECT_TRUE(Load(filepath, js));
200+
201+
int i_value;
202+
GetField(js, "int", i_value);
203+
EXPECT_EQ(i_value, 123);
204+
205+
fs::RemoveFile(filepath);
206+
EXPECT_FALSE(Load(filepath, js));
207+
}
208+
162209
TEST(Json, FindEndPos) {
163210
EXPECT_EQ(FindEndPos("", 0), 0);
164211
EXPECT_EQ(FindEndPos(R"({})", 2), 2); //! {}

0 commit comments

Comments
 (0)