json_objectwriter.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include <google/protobuf/util/internal/json_objectwriter.h>
  31. #include <math.h>
  32. #include <google/protobuf/stubs/casts.h>
  33. #include <google/protobuf/stubs/logging.h>
  34. #include <google/protobuf/stubs/common.h>
  35. #include <google/protobuf/util/internal/utility.h>
  36. #include <google/protobuf/util/internal/json_escaping.h>
  37. #include <google/protobuf/stubs/strutil.h>
  38. #include <google/protobuf/stubs/mathlimits.h>
  39. namespace google {
  40. namespace protobuf {
  41. namespace util {
  42. namespace converter {
  43. using strings::ArrayByteSource;
  44. ;
  45. JsonObjectWriter::~JsonObjectWriter() {
  46. if (!element_->is_root()) {
  47. GOOGLE_LOG(WARNING) << "JsonObjectWriter was not fully closed.";
  48. }
  49. }
  50. JsonObjectWriter* JsonObjectWriter::StartObject(StringPiece name) {
  51. WritePrefix(name);
  52. WriteChar('{');
  53. PushObject();
  54. return this;
  55. }
  56. JsonObjectWriter* JsonObjectWriter::EndObject() {
  57. Pop();
  58. WriteChar('}');
  59. if (element()->is_root()) NewLine();
  60. return this;
  61. }
  62. JsonObjectWriter* JsonObjectWriter::StartList(StringPiece name) {
  63. WritePrefix(name);
  64. WriteChar('[');
  65. PushArray();
  66. return this;
  67. }
  68. JsonObjectWriter* JsonObjectWriter::EndList() {
  69. Pop();
  70. WriteChar(']');
  71. if (element()->is_root()) NewLine();
  72. return this;
  73. }
  74. JsonObjectWriter* JsonObjectWriter::RenderBool(StringPiece name, bool value) {
  75. return RenderSimple(name, value ? "true" : "false");
  76. }
  77. JsonObjectWriter* JsonObjectWriter::RenderInt32(StringPiece name, int32 value) {
  78. return RenderSimple(name, SimpleItoa(value));
  79. }
  80. JsonObjectWriter* JsonObjectWriter::RenderUint32(StringPiece name,
  81. uint32 value) {
  82. return RenderSimple(name, SimpleItoa(value));
  83. }
  84. JsonObjectWriter* JsonObjectWriter::RenderInt64(StringPiece name, int64 value) {
  85. WritePrefix(name);
  86. WriteChar('"');
  87. stream_->WriteString(SimpleItoa(value));
  88. WriteChar('"');
  89. return this;
  90. }
  91. JsonObjectWriter* JsonObjectWriter::RenderUint64(StringPiece name,
  92. uint64 value) {
  93. WritePrefix(name);
  94. WriteChar('"');
  95. stream_->WriteString(SimpleItoa(value));
  96. WriteChar('"');
  97. return this;
  98. }
  99. JsonObjectWriter* JsonObjectWriter::RenderDouble(StringPiece name,
  100. double value) {
  101. if (MathLimits<double>::IsFinite(value)) {
  102. return RenderSimple(name, SimpleDtoa(value));
  103. }
  104. // Render quoted with NaN/Infinity-aware DoubleAsString.
  105. return RenderString(name, DoubleAsString(value));
  106. }
  107. JsonObjectWriter* JsonObjectWriter::RenderFloat(StringPiece name, float value) {
  108. if (MathLimits<float>::IsFinite(value)) {
  109. return RenderSimple(name, SimpleFtoa(value));
  110. }
  111. // Render quoted with NaN/Infinity-aware FloatAsString.
  112. return RenderString(name, FloatAsString(value));
  113. }
  114. JsonObjectWriter* JsonObjectWriter::RenderString(StringPiece name,
  115. StringPiece value) {
  116. WritePrefix(name);
  117. WriteChar('"');
  118. ArrayByteSource source(value);
  119. JsonEscaping::Escape(&source, &sink_);
  120. WriteChar('"');
  121. return this;
  122. }
  123. JsonObjectWriter* JsonObjectWriter::RenderBytes(StringPiece name,
  124. StringPiece value) {
  125. WritePrefix(name);
  126. string base64;
  127. if (use_websafe_base64_for_bytes_)
  128. WebSafeBase64EscapeWithPadding(value.ToString(), &base64);
  129. else
  130. Base64Escape(value, &base64);
  131. WriteChar('"');
  132. // TODO(wpoon): Consider a ByteSink solution that writes the base64 bytes
  133. // directly to the stream, rather than first putting them
  134. // into a string and then writing them to the stream.
  135. stream_->WriteRaw(base64.data(), base64.size());
  136. WriteChar('"');
  137. return this;
  138. }
  139. JsonObjectWriter* JsonObjectWriter::RenderNull(StringPiece name) {
  140. return RenderSimple(name, "null");
  141. }
  142. JsonObjectWriter* JsonObjectWriter::RenderNullAsEmpty(StringPiece name) {
  143. return RenderSimple(name, "");
  144. }
  145. void JsonObjectWriter::WritePrefix(StringPiece name) {
  146. bool not_first = !element()->is_first();
  147. if (not_first) WriteChar(',');
  148. if (not_first || !element()->is_root()) NewLine();
  149. if (!name.empty() || element()->is_json_object()) {
  150. WriteChar('"');
  151. if (!name.empty()) {
  152. ArrayByteSource source(name);
  153. JsonEscaping::Escape(&source, &sink_);
  154. }
  155. stream_->WriteString("\":");
  156. if (!indent_string_.empty()) WriteChar(' ');
  157. }
  158. }
  159. } // namespace converter
  160. } // namespace util
  161. } // namespace protobuf
  162. } // namespace google