| OLD | NEW |
| (Empty) | |
| 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/json_util.h> |
| 32 |
| 33 #include <google/protobuf/stubs/common.h> |
| 34 #include <google/protobuf/io/coded_stream.h> |
| 35 #include <google/protobuf/io/zero_copy_stream.h> |
| 36 #include <google/protobuf/util/internal/default_value_objectwriter.h> |
| 37 #include <google/protobuf/util/internal/error_listener.h> |
| 38 #include <google/protobuf/util/internal/json_objectwriter.h> |
| 39 #include <google/protobuf/util/internal/json_stream_parser.h> |
| 40 #include <google/protobuf/util/internal/protostream_objectsource.h> |
| 41 #include <google/protobuf/util/internal/protostream_objectwriter.h> |
| 42 #include <google/protobuf/util/type_resolver.h> |
| 43 #include <google/protobuf/util/type_resolver_util.h> |
| 44 #include <google/protobuf/stubs/bytestream.h> |
| 45 #include <google/protobuf/stubs/status_macros.h> |
| 46 |
| 47 namespace google { |
| 48 namespace protobuf { |
| 49 namespace util { |
| 50 |
| 51 namespace internal { |
| 52 void ZeroCopyStreamByteSink::Append(const char* bytes, size_t len) { |
| 53 while (len > 0) { |
| 54 void* buffer; |
| 55 int length; |
| 56 if (!stream_->Next(&buffer, &length)) { |
| 57 // There isn't a way for ByteSink to report errors. |
| 58 return; |
| 59 } |
| 60 if (len < length) { |
| 61 memcpy(buffer, bytes, len); |
| 62 stream_->BackUp(length - len); |
| 63 break; |
| 64 } else { |
| 65 memcpy(buffer, bytes, length); |
| 66 bytes += length; |
| 67 len -= length; |
| 68 } |
| 69 } |
| 70 } |
| 71 } // namespace internal |
| 72 |
| 73 util::Status BinaryToJsonStream(TypeResolver* resolver, |
| 74 const string& type_url, |
| 75 io::ZeroCopyInputStream* binary_input, |
| 76 io::ZeroCopyOutputStream* json_output, |
| 77 const JsonOptions& options) { |
| 78 io::CodedInputStream in_stream(binary_input); |
| 79 google::protobuf::Type type; |
| 80 RETURN_IF_ERROR(resolver->ResolveMessageType(type_url, &type)); |
| 81 converter::ProtoStreamObjectSource proto_source(&in_stream, resolver, type); |
| 82 io::CodedOutputStream out_stream(json_output); |
| 83 converter::JsonObjectWriter json_writer(options.add_whitespace ? " " : "", |
| 84 &out_stream); |
| 85 if (options.always_print_primitive_fields) { |
| 86 converter::DefaultValueObjectWriter default_value_writer( |
| 87 resolver, type, &json_writer); |
| 88 return proto_source.WriteTo(&default_value_writer); |
| 89 } else { |
| 90 return proto_source.WriteTo(&json_writer); |
| 91 } |
| 92 } |
| 93 |
| 94 util::Status BinaryToJsonString(TypeResolver* resolver, |
| 95 const string& type_url, |
| 96 const string& binary_input, |
| 97 string* json_output, |
| 98 const JsonOptions& options) { |
| 99 io::ArrayInputStream input_stream(binary_input.data(), binary_input.size()); |
| 100 io::StringOutputStream output_stream(json_output); |
| 101 return BinaryToJsonStream(resolver, type_url, &input_stream, &output_stream, |
| 102 options); |
| 103 } |
| 104 |
| 105 util::Status JsonToBinaryStream(TypeResolver* resolver, |
| 106 const string& type_url, |
| 107 io::ZeroCopyInputStream* json_input, |
| 108 io::ZeroCopyOutputStream* binary_output) { |
| 109 google::protobuf::Type type; |
| 110 RETURN_IF_ERROR(resolver->ResolveMessageType(type_url, &type)); |
| 111 internal::ZeroCopyStreamByteSink sink(binary_output); |
| 112 converter::NoopErrorListener listener; |
| 113 converter::ProtoStreamObjectWriter proto_writer(resolver, type, &sink, |
| 114 &listener); |
| 115 |
| 116 converter::JsonStreamParser parser(&proto_writer); |
| 117 const void* buffer; |
| 118 int length; |
| 119 while (json_input->Next(&buffer, &length)) { |
| 120 if (length == 0) continue; |
| 121 RETURN_IF_ERROR( |
| 122 parser.Parse(StringPiece(static_cast<const char*>(buffer), length))); |
| 123 } |
| 124 RETURN_IF_ERROR(parser.FinishParse()); |
| 125 |
| 126 return util::Status::OK; |
| 127 } |
| 128 |
| 129 util::Status JsonToBinaryString(TypeResolver* resolver, |
| 130 const string& type_url, |
| 131 const string& json_input, |
| 132 string* binary_output) { |
| 133 io::ArrayInputStream input_stream(json_input.data(), json_input.size()); |
| 134 io::StringOutputStream output_stream(binary_output); |
| 135 return JsonToBinaryStream(resolver, type_url, &input_stream, &output_stream); |
| 136 } |
| 137 |
| 138 } // namespace util |
| 139 } // namespace protobuf |
| 140 } // namespace google |
| OLD | NEW |