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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 const string& type_url, | 95 const string& type_url, |
96 const string& binary_input, | 96 const string& binary_input, |
97 string* json_output, | 97 string* json_output, |
98 const JsonOptions& options) { | 98 const JsonOptions& options) { |
99 io::ArrayInputStream input_stream(binary_input.data(), binary_input.size()); | 99 io::ArrayInputStream input_stream(binary_input.data(), binary_input.size()); |
100 io::StringOutputStream output_stream(json_output); | 100 io::StringOutputStream output_stream(json_output); |
101 return BinaryToJsonStream(resolver, type_url, &input_stream, &output_stream, | 101 return BinaryToJsonStream(resolver, type_url, &input_stream, &output_stream, |
102 options); | 102 options); |
103 } | 103 } |
104 | 104 |
| 105 namespace { |
| 106 class StatusErrorListener : public converter::ErrorListener { |
| 107 public: |
| 108 StatusErrorListener() : status_(util::Status::OK) {} |
| 109 virtual ~StatusErrorListener() {} |
| 110 |
| 111 util::Status GetStatus() { return status_; } |
| 112 |
| 113 virtual void InvalidName(const converter::LocationTrackerInterface& loc, |
| 114 StringPiece unknown_name, StringPiece message) { |
| 115 status_ = util::Status(util::error::INVALID_ARGUMENT, |
| 116 loc.ToString() + ": " + message.ToString()); |
| 117 } |
| 118 |
| 119 virtual void InvalidValue(const converter::LocationTrackerInterface& loc, |
| 120 StringPiece type_name, StringPiece value) { |
| 121 status_ = |
| 122 util::Status(util::error::INVALID_ARGUMENT, |
| 123 loc.ToString() + ": invalid value " + value.ToString() + |
| 124 " for type " + type_name.ToString()); |
| 125 } |
| 126 |
| 127 virtual void MissingField(const converter::LocationTrackerInterface& loc, |
| 128 StringPiece missing_name) { |
| 129 status_ = util::Status( |
| 130 util::error::INVALID_ARGUMENT, |
| 131 loc.ToString() + ": missing field " + missing_name.ToString()); |
| 132 } |
| 133 |
| 134 private: |
| 135 util::Status status_; |
| 136 |
| 137 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StatusErrorListener); |
| 138 }; |
| 139 } // namespace |
| 140 |
105 util::Status JsonToBinaryStream(TypeResolver* resolver, | 141 util::Status JsonToBinaryStream(TypeResolver* resolver, |
106 const string& type_url, | 142 const string& type_url, |
107 io::ZeroCopyInputStream* json_input, | 143 io::ZeroCopyInputStream* json_input, |
108 io::ZeroCopyOutputStream* binary_output) { | 144 io::ZeroCopyOutputStream* binary_output) { |
109 google::protobuf::Type type; | 145 google::protobuf::Type type; |
110 RETURN_IF_ERROR(resolver->ResolveMessageType(type_url, &type)); | 146 RETURN_IF_ERROR(resolver->ResolveMessageType(type_url, &type)); |
111 internal::ZeroCopyStreamByteSink sink(binary_output); | 147 internal::ZeroCopyStreamByteSink sink(binary_output); |
112 converter::NoopErrorListener listener; | 148 StatusErrorListener listener; |
113 converter::ProtoStreamObjectWriter proto_writer(resolver, type, &sink, | 149 converter::ProtoStreamObjectWriter proto_writer(resolver, type, &sink, |
114 &listener); | 150 &listener); |
115 | 151 |
116 converter::JsonStreamParser parser(&proto_writer); | 152 converter::JsonStreamParser parser(&proto_writer); |
117 const void* buffer; | 153 const void* buffer; |
118 int length; | 154 int length; |
119 while (json_input->Next(&buffer, &length)) { | 155 while (json_input->Next(&buffer, &length)) { |
120 if (length == 0) continue; | 156 if (length == 0) continue; |
121 RETURN_IF_ERROR( | 157 RETURN_IF_ERROR( |
122 parser.Parse(StringPiece(static_cast<const char*>(buffer), length))); | 158 parser.Parse(StringPiece(static_cast<const char*>(buffer), length))); |
123 } | 159 } |
124 RETURN_IF_ERROR(parser.FinishParse()); | 160 RETURN_IF_ERROR(parser.FinishParse()); |
125 | 161 |
126 return util::Status::OK; | 162 return listener.GetStatus(); |
127 } | 163 } |
128 | 164 |
129 util::Status JsonToBinaryString(TypeResolver* resolver, | 165 util::Status JsonToBinaryString(TypeResolver* resolver, |
130 const string& type_url, | 166 const string& type_url, |
131 const string& json_input, | 167 const string& json_input, |
132 string* binary_output) { | 168 string* binary_output) { |
133 io::ArrayInputStream input_stream(json_input.data(), json_input.size()); | 169 io::ArrayInputStream input_stream(json_input.data(), json_input.size()); |
134 io::StringOutputStream output_stream(binary_output); | 170 io::StringOutputStream output_stream(binary_output); |
135 return JsonToBinaryStream(resolver, type_url, &input_stream, &output_stream); | 171 return JsonToBinaryStream(resolver, type_url, &input_stream, &output_stream); |
136 } | 172 } |
137 | 173 |
138 } // namespace util | 174 } // namespace util |
139 } // namespace protobuf | 175 } // namespace protobuf |
140 } // namespace google | 176 } // namespace google |
OLD | NEW |