This is an automated email from the ASF dual-hosted git repository.
alexey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git
The following commit(s) were added to refs/heads/master by this push:
new acc5c4998 [util] add EasyJson::ToString(const rapidjson::Value&)
acc5c4998 is described below
commit acc5c4998a69ef2ec2b751b6a6954af2c9942917
Author: Alexey Serbin <[email protected]>
AuthorDate: Wed Oct 30 21:57:53 2024 -0700
[util] add EasyJson::ToString(const rapidjson::Value&)
This patch makes EasyJson::ToString() available as a static method
along with a new test scenario to cover the newly introduced
functionality. The new utility method is useful in various
JSON-parsing code and is necessary for follow-up changelists.
Change-Id: I1dc9260282435641eddc9d8ec02ebf9f0f140796
Reviewed-on: http://gerrit.cloudera.org:8080/22007
Tested-by: Marton Greber <[email protected]>
Reviewed-by: Marton Greber <[email protected]>
---
src/kudu/util/easy_json-test.cc | 26 ++++++++++++++++++++++++++
src/kudu/util/easy_json.cc | 12 ++++++++----
src/kudu/util/easy_json.h | 3 +++
3 files changed, 37 insertions(+), 4 deletions(-)
diff --git a/src/kudu/util/easy_json-test.cc b/src/kudu/util/easy_json-test.cc
index 70745126a..1289fdb06 100644
--- a/src/kudu/util/easy_json-test.cc
+++ b/src/kudu/util/easy_json-test.cc
@@ -103,4 +103,30 @@ TEST_F(EasyJsonTest, TestAllocatorLifetime) {
ASSERT_EQ(child.value()["child_attr"].GetInt(), 1);
}
+TEST_F(EasyJsonTest, ToString) {
+ {
+ EasyJson ej;
+ ASSERT_EQ("null", EasyJson::ToString(ej.value()));
+ ASSERT_EQ(EasyJson::ToString(ej.value()), ej.ToString());
+ }
+ {
+ EasyJson ej;
+ ej.SetObject();
+ ASSERT_EQ("{}", EasyJson::ToString(ej.value()));
+ ASSERT_EQ(EasyJson::ToString(ej.value()), ej.ToString());
+ }
+ {
+ EasyJson root;
+ EasyJson child = root["child"];
+
+ child["f0"] = 1;
+ child["f1"] = 0.5;
+ child["f2"] = "a";
+ EasyJson arr = child.Set("arr", EasyJson::kArray);
+ ASSERT_EQ("{\"child\":{\"f0\":1,\"f1\":0.5,\"f2\":\"a\",\"arr\":[]}}",
+ EasyJson::ToString(root.value()));
+ ASSERT_EQ(EasyJson::ToString(root.value()), root.ToString());
+ }
+}
+
} // namespace kudu
diff --git a/src/kudu/util/easy_json.cc b/src/kudu/util/easy_json.cc
index 42d032477..c2059be77 100644
--- a/src/kudu/util/easy_json.cc
+++ b/src/kudu/util/easy_json.cc
@@ -34,6 +34,13 @@ using std::string;
namespace kudu {
+string EasyJson::ToString(const Value& value) {
+ rapidjson::StringBuffer buffer;
+ rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
+ value.Accept(writer);
+ return buffer.GetString();
+}
+
EasyJson::EasyJson() : alloc_(new EasyJsonAllocator), value_(&alloc_->value())
{}
EasyJson::EasyJson(EasyJson::ComplexTypeInitializer type)
@@ -197,10 +204,7 @@ template<> EasyJson
EasyJson::PushBack(EasyJson::ComplexTypeInitializer val) {
}
string EasyJson::ToString() const {
- rapidjson::StringBuffer buffer;
- rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
- value_->Accept(writer);
- return buffer.GetString();
+ return ToString(*value_);
}
EasyJson::EasyJson(Value* value, scoped_refptr<EasyJsonAllocator> alloc)
diff --git a/src/kudu/util/easy_json.h b/src/kudu/util/easy_json.h
index bd0365aaa..5796e7344 100644
--- a/src/kudu/util/easy_json.h
+++ b/src/kudu/util/easy_json.h
@@ -59,6 +59,9 @@ class EasyJson {
kArray
};
+ // Returns a string representation of the specified RapidJson value object.
+ static std::string ToString(const rapidjson::Value& value);
+
EasyJson();
// Initializes the EasyJson object with the given type.
explicit EasyJson(ComplexTypeInitializer type);