protobuf/src/google/protobuf/util/internal/json_objectwriter.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include <google/protobuf/util/internal/json_objectwriter.h>
32 
33 #include <cmath>
34 #include <cstdint>
35 #include <limits>
36 
37 #include <google/protobuf/stubs/casts.h>
38 #include <google/protobuf/stubs/logging.h>
39 #include <google/protobuf/stubs/common.h>
40 #include <google/protobuf/util/internal/utility.h>
41 #include <google/protobuf/util/internal/json_escaping.h>
42 #include <google/protobuf/stubs/strutil.h>
43 
44 namespace google {
45 namespace protobuf {
46 namespace util {
47 namespace converter {
48 
49 
51  if (element_ && !element_->is_root()) {
52  GOOGLE_LOG(WARNING) << "JsonObjectWriter was not fully closed.";
53  }
54 }
55 
56 JsonObjectWriter* JsonObjectWriter::StartObject(StringPiece name) {
58  WriteChar('{');
59  PushObject();
60  return this;
61 }
62 
63 JsonObjectWriter* JsonObjectWriter::EndObject() {
64  Pop();
65  WriteChar('}');
66  if (element() && element()->is_root()) NewLine();
67  return this;
68 }
69 
70 JsonObjectWriter* JsonObjectWriter::StartList(StringPiece name) {
72  WriteChar('[');
73  PushArray();
74  return this;
75 }
76 
77 JsonObjectWriter* JsonObjectWriter::EndList() {
78  Pop();
79  WriteChar(']');
80  if (element()->is_root()) NewLine();
81  return this;
82 }
83 
84 JsonObjectWriter* JsonObjectWriter::RenderBool(StringPiece name,
85  bool value) {
86  return RenderSimple(name, value ? "true" : "false");
87 }
88 
89 JsonObjectWriter* JsonObjectWriter::RenderInt32(StringPiece name,
90  int32_t value) {
91  return RenderSimple(name, StrCat(value));
92 }
93 
94 JsonObjectWriter* JsonObjectWriter::RenderUint32(StringPiece name,
95  uint32_t value) {
96  return RenderSimple(name, StrCat(value));
97 }
98 
99 JsonObjectWriter* JsonObjectWriter::RenderInt64(StringPiece name,
100  int64_t value) {
101  WritePrefix(name);
102  WriteChar('"');
104  WriteChar('"');
105  return this;
106 }
107 
108 JsonObjectWriter* JsonObjectWriter::RenderUint64(StringPiece name,
109  uint64_t value) {
110  WritePrefix(name);
111  WriteChar('"');
113  WriteChar('"');
114  return this;
115 }
116 
117 JsonObjectWriter* JsonObjectWriter::RenderDouble(StringPiece name,
118  double value) {
119  if (std::isfinite(value)) {
120  return RenderSimple(name, SimpleDtoa(value));
121  }
122 
123  // Render quoted with NaN/Infinity-aware DoubleAsString.
125 }
126 
127 JsonObjectWriter* JsonObjectWriter::RenderFloat(StringPiece name,
128  float value) {
129  if (std::isfinite(value)) {
130  return RenderSimple(name, SimpleFtoa(value));
131  }
132 
133  // Render quoted with NaN/Infinity-aware FloatAsString.
135 }
136 
137 JsonObjectWriter* JsonObjectWriter::RenderString(StringPiece name,
138  StringPiece value) {
139  WritePrefix(name);
140  WriteChar('"');
142  WriteChar('"');
143  return this;
144 }
145 
146 JsonObjectWriter* JsonObjectWriter::RenderBytes(StringPiece name,
147  StringPiece value) {
148  WritePrefix(name);
149  std::string base64;
150 
153  else
154  Base64Escape(value, &base64);
155 
156  WriteChar('"');
157  // TODO(wpoon): Consider a ByteSink solution that writes the base64 bytes
158  // directly to the stream, rather than first putting them
159  // into a string and then writing them to the stream.
160  stream_->WriteRaw(base64.data(), base64.size());
161  WriteChar('"');
162  return this;
163 }
164 
165 JsonObjectWriter* JsonObjectWriter::RenderNull(StringPiece name) {
166  return RenderSimple(name, "null");
167 }
168 
169 JsonObjectWriter* JsonObjectWriter::RenderNullAsEmpty(StringPiece name) {
170  return RenderSimple(name, "");
171 }
172 
173 void JsonObjectWriter::WritePrefix(StringPiece name) {
174  bool not_first = !element()->is_first();
175  if (not_first) WriteChar(',');
176  if (not_first || !element()->is_root()) NewLine();
177  if (!name.empty() || element()->is_json_object()) {
178  WriteChar('"');
179  if (!name.empty()) {
181  }
182  WriteRawString("\":");
183  if (!indent_string_.empty()) WriteChar(' ');
184  }
185 }
186 
187 } // namespace converter
188 } // namespace util
189 } // namespace protobuf
190 } // namespace google
google::protobuf::util::converter::JsonObjectWriter::RenderBool
JsonObjectWriter * RenderBool(StringPiece name, bool value) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:85
google::protobuf::io::CodedOutputStream::WriteRaw
void WriteRaw(const void *buffer, int size)
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream.h:1103
google::protobuf::value
const Descriptor::ReservedRange value
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1954
google::protobuf::util::converter::JsonObjectWriter::RenderSimple
JsonObjectWriter * RenderSimple(StringPiece name, const std::string &value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.h:171
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf::util::converter::JsonObjectWriter::PushObject
void PushObject()
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.h:184
google::protobuf::util::converter::JsonObjectWriter::RenderFloat
JsonObjectWriter * RenderFloat(StringPiece name, float value) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:128
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
make_cmakelists.converter
converter
Definition: make_cmakelists.py:317
setup.name
name
Definition: setup.py:542
google::protobuf::WebSafeBase64EscapeWithPadding
void WebSafeBase64EscapeWithPadding(StringPiece src, string *dest)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:2250
google::protobuf::util::converter::JsonObjectWriter::EndObject
JsonObjectWriter * EndObject() override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:64
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:1482
google::protobuf::util::converter::JsonObjectWriter::element_
std::unique_ptr< Element > element_
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.h:214
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
google::protobuf::util::converter::JsonObjectWriter::RenderDouble
JsonObjectWriter * RenderDouble(StringPiece name, double value) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:118
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
google::protobuf::util::converter::JsonObjectWriter::RenderUint32
JsonObjectWriter * RenderUint32(StringPiece name, uint32 value) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:95
google::protobuf::util::converter::JsonObjectWriter::Element::is_first
bool is_first()
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.h:130
google::protobuf::util::converter::JsonObjectWriter::Pop
void Pop()
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.h:189
google::protobuf::util::converter::JsonObjectWriter::RenderInt64
JsonObjectWriter * RenderInt64(StringPiece name, int64 value) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:100
google::protobuf::util::converter::JsonObjectWriter::WritePrefix
void WritePrefix(StringPiece name)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:175
google::protobuf::util::converter::JsonObjectWriter::RenderNull
JsonObjectWriter * RenderNull(StringPiece name) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:167
google::protobuf::SimpleFtoa
string SimpleFtoa(float value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:1226
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
google::protobuf::util::converter::JsonObjectWriter::StartObject
JsonObjectWriter * StartObject(StringPiece name) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:57
google::protobuf::util::converter::JsonObjectWriter::~JsonObjectWriter
virtual ~JsonObjectWriter()
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:51
google::protobuf::util::converter::JsonObjectWriter::stream_
io::CodedOutputStream * stream_
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.h:215
google::protobuf::WARNING
static const LogLevel WARNING
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/googletest.h:71
google::protobuf::util::converter::JsonObjectWriter::NewLine
void NewLine()
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.h:197
google::protobuf::util::converter::JsonObjectWriter::WriteRawString
void WriteRawString(StringPiece s)
Definition: protobuf/src/google/protobuf/util/internal/json_objectwriter.h:251
google::protobuf::util::converter::JsonObjectWriter::EndList
JsonObjectWriter * EndList() override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:78
google::protobuf::util::converter::JsonObjectWriter::RenderNullAsEmpty
virtual JsonObjectWriter * RenderNullAsEmpty(StringPiece name)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:171
google::protobuf::util::converter::DoubleAsString
std::string DoubleAsString(double value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:378
google::protobuf::SimpleDtoa
string SimpleDtoa(double value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:1221
google::protobuf::util::converter::JsonObjectWriter::PushArray
void PushArray()
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.h:179
google::protobuf::Base64Escape
int Base64Escape(const unsigned char *src, int szsrc, char *dest, int szdest)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:2206
google::protobuf::util::converter::JsonObjectWriter::RenderUint64
JsonObjectWriter * RenderUint64(StringPiece name, uint64 value) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:109
google::protobuf::util::converter::JsonObjectWriter::StartList
JsonObjectWriter * StartList(StringPiece name) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:71
google::protobuf::util::converter::JsonObjectWriter::RenderBytes
JsonObjectWriter * RenderBytes(StringPiece name, StringPiece value) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:148
google::protobuf::util::converter::JsonEscaping::Escape
static void Escape(strings::ByteSource *input, strings::ByteSink *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_escaping.cc:301
google::protobuf::util::converter::JsonObjectWriter::RenderString
JsonObjectWriter * RenderString(StringPiece name, StringPiece value) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:138
google::protobuf::util::converter::JsonObjectWriter::indent_string_
const std::string indent_string_
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.h:217
google::protobuf::util::converter::JsonObjectWriter::sink_
ByteSinkWrapper sink_
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.h:216
google::protobuf::util::converter::FloatAsString
std::string FloatAsString(float value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:386
isfinite
#define isfinite
Definition: bloaty/third_party/protobuf/conformance/third_party/jsoncpp/jsoncpp.cpp:4014
google::protobuf::util::converter::JsonObjectWriter::use_websafe_base64_for_bytes_
bool use_websafe_base64_for_bytes_
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.h:221
google::protobuf::util::converter::JsonObjectWriter::RenderInt32
JsonObjectWriter * RenderInt32(StringPiece name, int32 value) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.cc:90
google::protobuf::util::converter::JsonObjectWriter::WriteChar
void WriteChar(const char c)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.h:212
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:146
google::protobuf::util::converter::JsonObjectWriter::element
Element * element() override
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/json_objectwriter.h:149
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:12