| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 #include <google/protobuf/util/internal/json_escaping.h> | 39 #include <google/protobuf/util/internal/json_escaping.h> |
| 40 #include <google/protobuf/stubs/strutil.h> | 40 #include <google/protobuf/stubs/strutil.h> |
| 41 #include <google/protobuf/stubs/mathlimits.h> | 41 #include <google/protobuf/stubs/mathlimits.h> |
| 42 | 42 |
| 43 namespace google { | 43 namespace google { |
| 44 namespace protobuf { | 44 namespace protobuf { |
| 45 namespace util { | 45 namespace util { |
| 46 namespace converter { | 46 namespace converter { |
| 47 | 47 |
| 48 using strings::ArrayByteSource; | 48 using strings::ArrayByteSource; |
| 49 ; |
| 49 | 50 |
| 50 JsonObjectWriter::~JsonObjectWriter() { | 51 JsonObjectWriter::~JsonObjectWriter() { |
| 51 if (!element_->is_root()) { | 52 if (!element_->is_root()) { |
| 52 GOOGLE_LOG(WARNING) << "JsonObjectWriter was not fully closed."; | 53 GOOGLE_LOG(WARNING) << "JsonObjectWriter was not fully closed."; |
| 53 } | 54 } |
| 54 } | 55 } |
| 55 | 56 |
| 56 JsonObjectWriter* JsonObjectWriter::StartObject(StringPiece name) { | 57 JsonObjectWriter* JsonObjectWriter::StartObject(StringPiece name) { |
| 57 WritePrefix(name); | 58 WritePrefix(name); |
| 58 WriteChar('{'); | 59 WriteChar('{'); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 74 return this; | 75 return this; |
| 75 } | 76 } |
| 76 | 77 |
| 77 JsonObjectWriter* JsonObjectWriter::EndList() { | 78 JsonObjectWriter* JsonObjectWriter::EndList() { |
| 78 Pop(); | 79 Pop(); |
| 79 WriteChar(']'); | 80 WriteChar(']'); |
| 80 if (element()->is_root()) NewLine(); | 81 if (element()->is_root()) NewLine(); |
| 81 return this; | 82 return this; |
| 82 } | 83 } |
| 83 | 84 |
| 84 JsonObjectWriter* JsonObjectWriter::RenderBool(StringPiece name, | 85 JsonObjectWriter* JsonObjectWriter::RenderBool(StringPiece name, bool value) { |
| 85 bool value) { | |
| 86 return RenderSimple(name, value ? "true" : "false"); | 86 return RenderSimple(name, value ? "true" : "false"); |
| 87 } | 87 } |
| 88 | 88 |
| 89 JsonObjectWriter* JsonObjectWriter::RenderInt32(StringPiece name, | 89 JsonObjectWriter* JsonObjectWriter::RenderInt32(StringPiece name, int32 value) { |
| 90 int32 value) { | |
| 91 return RenderSimple(name, SimpleItoa(value)); | 90 return RenderSimple(name, SimpleItoa(value)); |
| 92 } | 91 } |
| 93 | 92 |
| 94 JsonObjectWriter* JsonObjectWriter::RenderUint32(StringPiece name, | 93 JsonObjectWriter* JsonObjectWriter::RenderUint32(StringPiece name, |
| 95 uint32 value) { | 94 uint32 value) { |
| 96 return RenderSimple(name, SimpleItoa(value)); | 95 return RenderSimple(name, SimpleItoa(value)); |
| 97 } | 96 } |
| 98 | 97 |
| 99 JsonObjectWriter* JsonObjectWriter::RenderInt64(StringPiece name, | 98 JsonObjectWriter* JsonObjectWriter::RenderInt64(StringPiece name, int64 value) { |
| 100 int64 value) { | |
| 101 WritePrefix(name); | 99 WritePrefix(name); |
| 102 WriteChar('"'); | 100 WriteChar('"'); |
| 103 stream_->WriteString(SimpleItoa(value)); | 101 stream_->WriteString(SimpleItoa(value)); |
| 104 WriteChar('"'); | 102 WriteChar('"'); |
| 105 return this; | 103 return this; |
| 106 } | 104 } |
| 107 | 105 |
| 108 JsonObjectWriter* JsonObjectWriter::RenderUint64(StringPiece name, | 106 JsonObjectWriter* JsonObjectWriter::RenderUint64(StringPiece name, |
| 109 uint64 value) { | 107 uint64 value) { |
| 110 WritePrefix(name); | 108 WritePrefix(name); |
| 111 WriteChar('"'); | 109 WriteChar('"'); |
| 112 stream_->WriteString(SimpleItoa(value)); | 110 stream_->WriteString(SimpleItoa(value)); |
| 113 WriteChar('"'); | 111 WriteChar('"'); |
| 114 return this; | 112 return this; |
| 115 } | 113 } |
| 116 | 114 |
| 117 JsonObjectWriter* JsonObjectWriter::RenderDouble(StringPiece name, | 115 JsonObjectWriter* JsonObjectWriter::RenderDouble(StringPiece name, |
| 118 double value) { | 116 double value) { |
| 119 if (MathLimits<double>::IsFinite(value)) { | 117 if (MathLimits<double>::IsFinite(value)) { |
| 120 return RenderSimple(name, SimpleDtoa(value)); | 118 return RenderSimple(name, SimpleDtoa(value)); |
| 121 } | 119 } |
| 122 | 120 |
| 123 // Render quoted with NaN/Infinity-aware DoubleAsString. | 121 // Render quoted with NaN/Infinity-aware DoubleAsString. |
| 124 return RenderString(name, DoubleAsString(value)); | 122 return RenderString(name, DoubleAsString(value)); |
| 125 } | 123 } |
| 126 | 124 |
| 127 JsonObjectWriter* JsonObjectWriter::RenderFloat(StringPiece name, | 125 JsonObjectWriter* JsonObjectWriter::RenderFloat(StringPiece name, float value) { |
| 128 float value) { | |
| 129 if (MathLimits<float>::IsFinite(value)) { | 126 if (MathLimits<float>::IsFinite(value)) { |
| 130 return RenderSimple(name, SimpleFtoa(value)); | 127 return RenderSimple(name, SimpleFtoa(value)); |
| 131 } | 128 } |
| 132 | 129 |
| 133 // Render quoted with NaN/Infinity-aware FloatAsString. | 130 // Render quoted with NaN/Infinity-aware FloatAsString. |
| 134 return RenderString(name, FloatAsString(value)); | 131 return RenderString(name, FloatAsString(value)); |
| 135 } | 132 } |
| 136 | 133 |
| 137 JsonObjectWriter* JsonObjectWriter::RenderString(StringPiece name, | 134 JsonObjectWriter* JsonObjectWriter::RenderString(StringPiece name, |
| 138 StringPiece value) { | 135 StringPiece value) { |
| 139 WritePrefix(name); | 136 WritePrefix(name); |
| 140 WriteChar('"'); | 137 WriteChar('"'); |
| 141 ArrayByteSource source(value); | 138 ArrayByteSource source(value); |
| 142 JsonEscaping::Escape(&source, &sink_); | 139 JsonEscaping::Escape(&source, &sink_); |
| 143 WriteChar('"'); | 140 WriteChar('"'); |
| 144 return this; | 141 return this; |
| 145 } | 142 } |
| 146 | 143 |
| 147 JsonObjectWriter* JsonObjectWriter::RenderBytes(StringPiece name, | 144 JsonObjectWriter* JsonObjectWriter::RenderBytes(StringPiece name, |
| 148 StringPiece value) { | 145 StringPiece value) { |
| 149 WritePrefix(name); | 146 WritePrefix(name); |
| 150 string base64; | 147 string base64; |
| 151 Base64Escape(value, &base64); | 148 |
| 149 if (use_websafe_base64_for_bytes_) |
| 150 WebSafeBase64Escape(value.ToString(), &base64); |
| 151 else |
| 152 Base64Escape(value, &base64); |
| 153 |
| 152 WriteChar('"'); | 154 WriteChar('"'); |
| 153 // TODO(wpoon): Consider a ByteSink solution that writes the base64 bytes | 155 // TODO(wpoon): Consider a ByteSink solution that writes the base64 bytes |
| 154 // directly to the stream, rather than first putting them | 156 // directly to the stream, rather than first putting them |
| 155 // into a string and then writing them to the stream. | 157 // into a string and then writing them to the stream. |
| 156 stream_->WriteRaw(base64.data(), base64.size()); | 158 stream_->WriteRaw(base64.data(), base64.size()); |
| 157 WriteChar('"'); | 159 WriteChar('"'); |
| 158 return this; | 160 return this; |
| 159 } | 161 } |
| 160 | 162 |
| 161 JsonObjectWriter* JsonObjectWriter::RenderNull(StringPiece name) { | 163 JsonObjectWriter* JsonObjectWriter::RenderNull(StringPiece name) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 172 JsonEscaping::Escape(&source, &sink_); | 174 JsonEscaping::Escape(&source, &sink_); |
| 173 stream_->WriteString("\":"); | 175 stream_->WriteString("\":"); |
| 174 if (!indent_string_.empty()) WriteChar(' '); | 176 if (!indent_string_.empty()) WriteChar(' '); |
| 175 } | 177 } |
| 176 } | 178 } |
| 177 | 179 |
| 178 } // namespace converter | 180 } // namespace converter |
| 179 } // namespace util | 181 } // namespace util |
| 180 } // namespace protobuf | 182 } // namespace protobuf |
| 181 } // namespace google | 183 } // namespace google |
| OLD | NEW |