summaryrefslogtreecommitdiffstats
path: root/library/cpp
diff options
context:
space:
mode:
authorilnurkh <[email protected]>2026-05-22 15:55:04 +0300
committerilnurkh <[email protected]>2026-05-22 18:20:21 +0300
commit44e2a888ba4ec1a9795fda6db9e6ac0c3fe62e3f (patch)
tree875f08f8e16e568eec42ee4e039f33b686a26d5b /library/cpp
parent2e515ea4f384867385aef9d228c5c360ca6baac1 (diff)
json-value: GetValueByPathOrCreate
commit_hash:dbf6bb806bcac71fd67d65ec209838e5f5133710
Diffstat (limited to 'library/cpp')
-rw-r--r--library/cpp/json/writer/json_value.cpp5
-rw-r--r--library/cpp/json/writer/json_value.h1
-rw-r--r--library/cpp/json/writer/json_value_ut.cpp7
3 files changed, 13 insertions, 0 deletions
diff --git a/library/cpp/json/writer/json_value.cpp b/library/cpp/json/writer/json_value.cpp
index 22f2b2c07a2..a5f23170a09 100644
--- a/library/cpp/json/writer/json_value.cpp
+++ b/library/cpp/json/writer/json_value.cpp
@@ -892,6 +892,11 @@ namespace NJson {
const TJsonValue* TJsonValue::GetValueByPath(const TStringBuf key, char delim) const noexcept {
return GetValuePtrByPath<false>(this, key, delim);
}
+ TJsonValue& TJsonValue::GetValueByPathOrCreate(TStringBuf path, char delimiter) noexcept {
+ TJsonValue* const ptr = GetValuePtrByPath<true>(this, path, delimiter);
+ Y_ASSERT(ptr);
+ return *ptr;
+ }
TJsonValue* TJsonValue::GetValueByPath(const TStringBuf key, char delim) noexcept {
return GetValuePtrByPath<false>(this, key, delim);
diff --git a/library/cpp/json/writer/json_value.h b/library/cpp/json/writer/json_value.h
index 2ffbe9ff714..f833b005669 100644
--- a/library/cpp/json/writer/json_value.h
+++ b/library/cpp/json/writer/json_value.h
@@ -100,6 +100,7 @@ namespace NJson {
// returns NULL on failure
const TJsonValue* GetValueByPath(TStringBuf path, char delimiter = '.') const noexcept Y_LIFETIME_BOUND;
TJsonValue* GetValueByPath(TStringBuf path, char delimiter = '.') noexcept Y_LIFETIME_BOUND;
+ TJsonValue& GetValueByPathOrCreate(TStringBuf path, char delimiter = '.') noexcept Y_LIFETIME_BOUND;
void EraseValue(TStringBuf key);
void EraseValue(size_t index);
diff --git a/library/cpp/json/writer/json_value_ut.cpp b/library/cpp/json/writer/json_value_ut.cpp
index e804e541ccf..a7b0df5450b 100644
--- a/library/cpp/json/writer/json_value_ut.cpp
+++ b/library/cpp/json/writer/json_value_ut.cpp
@@ -287,6 +287,13 @@ Y_UNIT_TEST_SUITE(TJsonValueTest) {
UNIT_ASSERT(third.GetValueByPath("t.[2]", result));
UNIT_ASSERT(result.GetStringRobust() == "g");
+ third.GetValueByPathOrCreate("t.[3]") = "7";
+ third.GetValueByPathOrCreate("t.[2]") = "4";
+ UNIT_ASSERT(third.GetValueByPath("t.[2]", result));
+ UNIT_ASSERT(result.GetStringRobust() == "4");
+ UNIT_ASSERT(third.GetValueByPath("t.[3]", result));
+ UNIT_ASSERT(result.GetStringRobust() == "7");
+
UNIT_ASSERT(lhs.SetValueByPath("l/a/c/se", "h", '/'));
UNIT_ASSERT(lhs.GetValueByPath("l/a/c/se", result, '/'));
UNIT_ASSERT(result.GetStringRobust() == "h");