[core] Convert a null StringRef to an empty JSON string and pin null against null

This commit is contained in:
J. Nick Koston
2026-09-08 03:44:24 +02:00
parent a7bf937001
commit 82d399400e
2 changed files with 9 additions and 1 deletions
+1 -1
View File
@@ -263,7 +263,7 @@ inline double stod(const StringRef &str, size_t *pos = nullptr) {
#ifdef USE_JSON
// NOLINTNEXTLINE(readability-identifier-naming)
inline void convertToJson(const StringRef &src, JsonVariant dst) { dst.set(src.c_str()); }
inline void convertToJson(const StringRef &src, JsonVariant dst) { dst.set(src.empty() ? "" : src.c_str()); }
#endif // USE_JSON
} // namespace esphome
@@ -94,4 +94,12 @@ TEST(StringRefNullEmpty, ComparesAgainstText) {
EXPECT_TRUE(text.starts_with(null_empty));
}
TEST(StringRefNullEmpty, TwoNullViewsAreEqual) {
const StringRef a{nullptr, 0};
const StringRef b{nullptr, 0};
EXPECT_TRUE(a == b);
EXPECT_EQ(a.compare(b), 0);
EXPECT_TRUE(a.starts_with(b));
}
} // namespace esphome::core::testing