29 #include "absl/strings/string_view.h"
55 void OutputCheck(
size_t needed);
56 void OutputChar(
char c);
83 void JsonWriter::OutputCheck(
size_t needed) {
85 if (free_space >= needed)
return;
88 needed = (needed + 0xff) & ~0xffU;
92 void JsonWriter::OutputChar(
char c) {
98 OutputCheck(
str.size());
102 void JsonWriter::OutputIndent() {
103 static const char spacesstr[] =
114 while (spaces >= (
sizeof(spacesstr) - 1)) {
116 spaces -=
static_cast<unsigned>(
sizeof(spacesstr) - 1);
118 if (spaces == 0)
return;
123 void JsonWriter::ValueEnd() {
135 void JsonWriter::EscapeUtf16(
uint16_t utf16) {
136 static const char hex[] =
"0123456789abcdef";
138 OutputChar(hex[(utf16 >> 12) & 0x0f]);
139 OutputChar(hex[(utf16 >> 8) & 0x0f]);
140 OutputChar(hex[(utf16 >> 4) & 0x0f]);
141 OutputChar(hex[(utf16)&0x0f]);
144 void JsonWriter::EscapeString(
const std::string&
string) {
146 for (
size_t idx = 0;
idx <
string.size(); ++
idx) {
150 }
else if (c >= 32 && c <= 126) {
151 if (c ==
'\\' || c ==
'"') OutputChar(
'\\');
152 OutputChar(
static_cast<char>(c));
153 }
else if (c < 32 || c == 127) {
179 if ((c & 0xe0) == 0xc0) {
182 }
else if ((c & 0xf0) == 0xe0) {
185 }
else if ((c & 0xf8) == 0xf0) {
191 for (i = 0;
i < extra;
i++) {
201 if ((c & 0xc0) != 0x80) {
212 if (((utf32 >= 0xd800) && (utf32 <= 0xdfff)) || (utf32 >= 0x110000)) {
215 if (utf32 >= 0x10000) {
233 EscapeUtf16(
static_cast<uint16_t>(0xd800 | (utf32 >> 10)));
234 EscapeUtf16(
static_cast<uint16_t>(0xdc00 | (utf32 & 0x3ff)));
236 EscapeUtf16(
static_cast<uint16_t>(utf32));
261 void JsonWriter::ObjectKey(
const std::string&
string) {
264 EscapeString(
string);
269 void JsonWriter::ValueRaw(
const std::string&
string) {
272 OutputString(
string);
276 void JsonWriter::ValueString(
const std::string&
string) {
279 EscapeString(
string);
283 void JsonWriter::DumpObject(
const Json::Object&
object) {
285 for (
const auto& p :
object) {
286 ObjectKey(
p.first.data());
294 for (
const auto&
v :
array) {
300 void JsonWriter::DumpValue(
const Json&
value) {
301 switch (
value.type()) {
303 DumpObject(
value.object_value());
306 DumpArray(
value.array_value());
309 ValueString(
value.string_value());
312 ValueRaw(
value.string_value());