Reranko05 commented on PR #50568:
URL: https://github.com/apache/arrow/pull/50568#issuecomment-5087279280
@kou Sure
`json_writer_internal.h`
```cpp
#pragma once
#include <cstdint>
#include <sstream>
#include <string>
#include <string_view>
#include "arrow/util/visibility.h"
namespace arrow::json {
class ARROW_EXPORT JsonWriter {
public:
JsonWriter() = default;
void StartObject();
void EndObject();
void StartArray();
void EndArray();
void Key(std::string_view key);
void String(std::string_view value);
void RawValue(std::string_view value);
void Bool(bool value);
void Int(int32_t value);
void Int64(int64_t value);
void Uint(uint32_t value);
void Uint64(uint64_t value);
void Double(double value);
void Null();
std::string_view GetString() const;
void Clear();
private:
void MaybeComma();
void WriteQuotedString(std::string_view value);
std::ostringstream output_;
mutable std::string output_cache_;
bool needs_comma_ = false;
};
} // namespace arrow::json
```
`json_writer_internal.cc`
```cpp
#include "arrow/json/json_writer_internal.h"
namespace arrow::json {
void JsonWriter::StartObject() {
MaybeComma();
output_ << '{';
needs_comma_ = false;
}
void JsonWriter::EndObject() {
output_ << '}';
needs_comma_ = true;
}
void JsonWriter::StartArray() {
MaybeComma();
output_ << '[';
needs_comma_ = false;
}
void JsonWriter::EndArray() {
output_ << ']';
needs_comma_ = true;
}
void JsonWriter::Key(std::string_view key) {
MaybeComma();
WriteQuotedString(key);
output_ << ':';
needs_comma_ = false;
}
void JsonWriter::String(std::string_view value) {
MaybeComma();
WriteQuotedString(value);
needs_comma_ = true;
}
void JsonWriter::RawValue(std::string_view value) {
MaybeComma();
output_ << value;
needs_comma_ = true;
}
void JsonWriter::Bool(bool value) {
MaybeComma();
output_ << (value ? "true" : "false");
needs_comma_ = true;
}
void JsonWriter::Int(int32_t value) {
MaybeComma();
output_ << value;
needs_comma_ = true;
}
void JsonWriter::Int64(int64_t value) {
MaybeComma();
output_ << value;
needs_comma_ = true;
}
void JsonWriter::Uint(uint32_t value) {
MaybeComma();
output_ << value;
needs_comma_ = true;
}
void JsonWriter::Uint64(uint64_t value) {
MaybeComma();
output_ << value;
needs_comma_ = true;
}
void JsonWriter::Double(double value) {
MaybeComma();
output_ << value;
needs_comma_ = true;
}
void JsonWriter::Null() {
MaybeComma();
output_ << "null";
needs_comma_ = true;
}
std::string_view JsonWriter::GetString() const {
output_cache_ = output_.str();
return output_cache_;
}
void JsonWriter::Clear() {
output_.str("");
output_.clear();
output_cache_.clear();
needs_comma_ = false;
}
void JsonWriter::MaybeComma() {
if (needs_comma_) {
output_ << ',';
}
}
void JsonWriter::WriteQuotedString(std::string_view value) {
output_ << '"';
for (char c : value) {
switch (c) {
case '"':
output_ << "\\\"";
break;
case '\\':
output_ << "\\\\";
break;
case '\b':
output_ << "\\b";
break;
case '\f':
output_ << "\\f";
break;
case '\n':
output_ << "\\n";
break;
case '\r':
output_ << "\\r";
break;
case '\t':
output_ << "\\t";
break;
default:
output_ << c;
break;
}
}
output_ << '"';
}
} // namespace arrow::json
```
Add temporary benchmark in `json_writer_internal_test.cc`
```cpp
#include <chrono>
#include <iostream>
```
```cpp
TEST(JsonWriter, BenchmarkSerialization) {
JsonWriter writer;
constexpr int kIterations = 1000000;
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < kIterations; ++i) {
writer.Clear();
writer.StartObject();
writer.Key("id");
writer.Int(i);
writer.Key("name");
writer.String("Apache Arrow");
writer.Key("value");
writer.Double(3.1415926535);
writer.Key("valid");
writer.Bool(true);
writer.EndObject();
auto s = writer.GetString();
(void)s;
}
auto end = std::chrono::steady_clock::now();
auto ms =
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
.count();
std::cout << "Time: " << ms << " ms\n";
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]