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 
32 
33 #include <math.h>
34 
39 
43 
44 namespace google {
45 namespace protobuf {
46 namespace util {
47 namespace converter {
48 
49 using strings::ArrayByteSource;
50 ;
51 
53  if (element_ && !element_->is_root()) {
54  GOOGLE_LOG(WARNING) << "JsonObjectWriter was not fully closed.";
55  }
56 }
57 
60  WriteChar('{');
61  PushObject();
62  return this;
63 }
64 
66  Pop();
67  WriteChar('}');
68  if (element() && element()->is_root()) NewLine();
69  return this;
70 }
71 
74  WriteChar('[');
75  PushArray();
76  return this;
77 }
78 
80  Pop();
81  WriteChar(']');
82  if (element()->is_root()) NewLine();
83  return this;
84 }
85 
87  bool value) {
88  return RenderSimple(name, value ? "true" : "false");
89 }
90 
92  int32 value) {
93  return RenderSimple(name, StrCat(value));
94 }
95 
97  uint32 value) {
98  return RenderSimple(name, StrCat(value));
99 }
100 
102  int64 value) {
103  WritePrefix(name);
104  WriteChar('"');
106  WriteChar('"');
107  return this;
108 }
109 
111  uint64 value) {
112  WritePrefix(name);
113  WriteChar('"');
115  WriteChar('"');
116  return this;
117 }
118 
120  double value) {
122  return RenderSimple(name, SimpleDtoa(value));
123  }
124 
125  // Render quoted with NaN/Infinity-aware DoubleAsString.
127 }
128 
130  float value) {
132  return RenderSimple(name, SimpleFtoa(value));
133  }
134 
135  // Render quoted with NaN/Infinity-aware FloatAsString.
137 }
138 
140  StringPiece value) {
141  WritePrefix(name);
142  WriteChar('"');
143  ArrayByteSource source(value);
145  WriteChar('"');
146  return this;
147 }
148 
150  StringPiece value) {
151  WritePrefix(name);
153 
156  else
158 
159  WriteChar('"');
160  // TODO(wpoon): Consider a ByteSink solution that writes the base64 bytes
161  // directly to the stream, rather than first putting them
162  // into a string and then writing them to the stream.
163  stream_->WriteRaw(base64.data(), base64.size());
164  WriteChar('"');
165  return this;
166 }
167 
169  return RenderSimple(name, "null");
170 }
171 
173  return RenderSimple(name, "");
174 }
175 
177  bool not_first = !element()->is_first();
178  if (not_first) WriteChar(',');
179  if (not_first || !element()->is_root()) NewLine();
180  if (!name.empty() || element()->is_json_object()) {
181  WriteChar('"');
182  if (!name.empty()) {
183  ArrayByteSource source(name);
185  }
186  stream_->WriteString("\":");
187  if (!indent_string_.empty()) WriteChar(' ');
188  }
189 }
190 
191 } // namespace converter
192 } // namespace util
193 } // namespace protobuf
194 } // namespace google
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
google::protobuf::io::CodedOutputStream::WriteRaw
void WriteRaw(const void *buffer, int size)
Definition: coded_stream.cc:696
google::protobuf::util::converter::JsonObjectWriter::RenderSimple
JsonObjectWriter * RenderSimple(StringPiece name, const std::string &value)
Definition: json_objectwriter.h:171
google::protobuf::SimpleDtoa
string SimpleDtoa(double value)
Definition: strutil.cc:1219
google::protobuf::util::converter::JsonObjectWriter::StartList
virtual JsonObjectWriter * StartList(StringPiece name)
Definition: json_objectwriter.cc:72
google::protobuf::util::converter::JsonObjectWriter::StartObject
virtual JsonObjectWriter * StartObject(StringPiece name)
Definition: json_objectwriter.cc:58
google::protobuf::int64
int64_t int64
Definition: protobuf/src/google/protobuf/stubs/port.h:151
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
google::protobuf::io::CodedOutputStream::WriteString
void WriteString(const std::string &str)
Definition: coded_stream.h:1273
google::protobuf::uint32
uint32_t uint32
Definition: protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf::util::converter::JsonObjectWriter::PushObject
void PushObject()
Definition: json_objectwriter.h:184
google::protobuf::util::converter::JsonObjectWriter::EndList
virtual JsonObjectWriter * EndList()
Definition: json_objectwriter.cc:79
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::util::converter::JsonObjectWriter::element_
std::unique_ptr< Element > element_
Definition: json_objectwriter.h:214
google::protobuf::WebSafeBase64EscapeWithPadding
void WebSafeBase64EscapeWithPadding(StringPiece src, string *dest)
Definition: strutil.cc:2248
utility.h
google::protobuf::util::converter::JsonObjectWriter::RenderBytes
virtual JsonObjectWriter * RenderBytes(StringPiece name, StringPiece value)
Definition: json_objectwriter.cc:149
google::protobuf::util::converter::JsonObjectWriter::EndObject
virtual JsonObjectWriter * EndObject()
Definition: json_objectwriter.cc:65
google::protobuf::util::converter::JsonObjectWriter::RenderInt32
virtual JsonObjectWriter * RenderInt32(StringPiece name, int32 value)
Definition: json_objectwriter.cc:91
google::protobuf::int32
int32_t int32
Definition: protobuf/src/google/protobuf/stubs/port.h:150
google::protobuf::MathLimits
Definition: mathlimits.h:78
base64
strutil.h
google::protobuf::util::converter::JsonObjectWriter::Element::is_first
bool is_first()
Definition: json_objectwriter.h:130
google::protobuf::util::converter::JsonObjectWriter::Pop
void Pop()
Definition: json_objectwriter.h:189
mathlimits.h
google::protobuf::StringPiece
Definition: stringpiece.h:180
google::protobuf::util::converter::JsonObjectWriter::WritePrefix
void WritePrefix(StringPiece name)
Definition: json_objectwriter.cc:176
google::protobuf::util::converter::JsonObjectWriter::RenderUint32
virtual JsonObjectWriter * RenderUint32(StringPiece name, uint32 value)
Definition: json_objectwriter.cc:96
google::protobuf::util::converter::JsonObjectWriter::~JsonObjectWriter
virtual ~JsonObjectWriter()
Definition: json_objectwriter.cc:52
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: logging.h:146
google::protobuf::uint64
uint64_t uint64
Definition: protobuf/src/google/protobuf/stubs/port.h:156
google::protobuf::WARNING
static const LogLevel WARNING
Definition: protobuf/src/google/protobuf/testing/googletest.h:71
google::protobuf::util::converter::JsonObjectWriter::RenderNull
virtual JsonObjectWriter * RenderNull(StringPiece name)
Definition: json_objectwriter.cc:168
source
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:3072
google::protobuf::util::converter::JsonObjectWriter::NewLine
void NewLine()
Definition: json_objectwriter.h:197
google::protobuf::util::converter::JsonObjectWriter::RenderBool
virtual JsonObjectWriter * RenderBool(StringPiece name, bool value)
Definition: json_objectwriter.cc:86
google::protobuf::util::converter::JsonObjectWriter
Definition: json_objectwriter.h:88
google::protobuf::SimpleFtoa
string SimpleFtoa(float value)
Definition: strutil.cc:1224
casts.h
google::protobuf::util::converter::JsonObjectWriter::stream_
io::CodedOutputStream * stream_
Definition: json_objectwriter.h:215
google::protobuf::util::converter::JsonObjectWriter::RenderNullAsEmpty
virtual JsonObjectWriter * RenderNullAsEmpty(StringPiece name)
Definition: json_objectwriter.cc:172
google::protobuf::util::converter::DoubleAsString
std::string DoubleAsString(double value)
Definition: utility.cc:378
google::protobuf::util::converter::JsonObjectWriter::RenderUint64
virtual JsonObjectWriter * RenderUint64(StringPiece name, uint64 value)
Definition: json_objectwriter.cc:110
google::protobuf::util::converter::JsonObjectWriter::PushArray
void PushArray()
Definition: json_objectwriter.h:179
google::protobuf::Base64Escape
int Base64Escape(const unsigned char *src, int szsrc, char *dest, int szdest)
Definition: strutil.cc:2204
common.h
google::protobuf::util::converter::JsonObjectWriter::RenderString
virtual JsonObjectWriter * RenderString(StringPiece name, StringPiece value)
Definition: json_objectwriter.cc:139
google::protobuf::util::converter::JsonObjectWriter::RenderDouble
virtual JsonObjectWriter * RenderDouble(StringPiece name, double value)
Definition: json_objectwriter.cc:119
logging.h
google::protobuf::util::converter::JsonEscaping::Escape
static void Escape(strings::ByteSource *input, strings::ByteSink *output)
Definition: json_escaping.cc:301
json_escaping.h
google::protobuf::util::converter::JsonObjectWriter::indent_string_
const std::string indent_string_
Definition: json_objectwriter.h:217
google::protobuf::util::converter::JsonObjectWriter::RenderInt64
virtual JsonObjectWriter * RenderInt64(StringPiece name, int64 value)
Definition: json_objectwriter.cc:101
google::protobuf::util::converter::JsonObjectWriter::sink_
ByteSinkWrapper sink_
Definition: json_objectwriter.h:216
google::protobuf::util::converter::JsonObjectWriter::RenderFloat
virtual JsonObjectWriter * RenderFloat(StringPiece name, float value)
Definition: json_objectwriter.cc:129
json_objectwriter.h
google::protobuf::util::converter::FloatAsString
std::string FloatAsString(float value)
Definition: utility.cc:386
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
google::protobuf::util::converter::JsonObjectWriter::use_websafe_base64_for_bytes_
bool use_websafe_base64_for_bytes_
Definition: json_objectwriter.h:221
google::protobuf::util::converter::JsonObjectWriter::WriteChar
void WriteChar(const char c)
Definition: json_objectwriter.h:212
google::protobuf::util::converter::JsonObjectWriter::element
Element * element() override
Definition: json_objectwriter.h:149
google
Definition: data_proto2_to_proto3_util.h:11


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:55