Skip to content

Commit 98bd962

Browse files
committed
feat(util):1.12.18, 添加StringTo()对Json的转换函数
1 parent 81d7b78 commit 98bd962

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

modules/util/string_to.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include <tbox/base/defines.h>
2424
#include <tbox/base/log.h>
25+
#include <tbox/base/json.hpp>
26+
2527
#include "string.h"
2628

2729
namespace tbox {
@@ -128,5 +130,17 @@ bool StringTo(const std::string &text, std::string &value)
128130
return true;
129131
}
130132

133+
bool StringTo(const std::string &text, Json &js_value)
134+
{
135+
try {
136+
js_value = Json::parse(text);
137+
return true;
138+
139+
} catch (const std::exception &e) {
140+
LogNotice("can't convert '%s' to json, what: %s", text.c_str(), e.what());
141+
return false;
142+
}
143+
}
144+
131145
}
132146
}

modules/util/string_to.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020

2121
/**
2222
* StringTo() 系列函数用于将字串解析成对应类型的值,如bool, int, double 等
23+
*
2324
* 返回true,表示解析成功,值将存在第二个引用参数里
2425
* 返回false,表示解析失败,第二个引用参数将不受影响
2526
*/
2627
#ifndef TBOX_UTIL_STRING_TO_H_20250626
2728
#define TBOX_UTIL_STRING_TO_H_20250626
2829

2930
#include <string>
31+
#include <tbox/base/json_fwd.h>
3032

3133
namespace tbox {
3234
namespace util {
@@ -51,7 +53,9 @@ bool StringTo(const std::string &text, unsigned long long &value, int base = 10)
5153
bool StringTo(const std::string &text, float &value); //! 解析float值
5254
bool StringTo(const std::string &text, double &value); //! 解析double值
5355

54-
bool StringTo(const std::string &text, std::string &value);
56+
bool StringTo(const std::string &text, std::string &value); //! 等效于 value = text,仅用于风格一致性
57+
58+
bool StringTo(const std::string &text, Json &value); //! 解析成JSON对象
5559

5660
}
5761
}

modules/util/string_to_test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
* of the source tree.
1919
*/
2020
#include <gtest/gtest.h>
21+
2122
#include "string_to.h"
23+
#include <tbox/base/json.hpp>
2224

2325
namespace tbox {
2426
namespace util {
@@ -117,5 +119,19 @@ TEST(StringTo, Double)
117119
EXPECT_FALSE(StringTo("A", value));
118120
}
119121

122+
TEST(StringTo, Json)
123+
{
124+
Json js;
125+
EXPECT_TRUE(StringTo(R"({})", js));
126+
EXPECT_TRUE(StringTo(R"(123)", js));
127+
128+
EXPECT_TRUE(StringTo(R"({"a":123,"b":"test"})", js));
129+
EXPECT_EQ(js["a"].get<int>(), 123);
130+
EXPECT_EQ(js["b"].get<std::string>(), "test");
131+
132+
EXPECT_FALSE(StringTo(R"()", js));
133+
EXPECT_FALSE(StringTo(R"([})", js));
134+
}
135+
120136
}
121137
}

version.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
# TBOX版本号
2222
TBOX_VERSION_MAJOR := 1
2323
TBOX_VERSION_MINOR := 12
24-
TBOX_VERSION_REVISION := 17
24+
TBOX_VERSION_REVISION := 18

0 commit comments

Comments
 (0)