diff options
Diffstat (limited to 'src/google/protobuf/util/json_util.cc')
-rw-r--r-- | src/google/protobuf/util/json_util.cc | 63 |
1 files changed, 42 insertions, 21 deletions
diff --git a/src/google/protobuf/util/json_util.cc b/src/google/protobuf/util/json_util.cc index 7a7f299f..bf82fa7c 100644 --- a/src/google/protobuf/util/json_util.cc +++ b/src/google/protobuf/util/json_util.cc @@ -33,6 +33,7 @@ #include <google/protobuf/stubs/common.h> #include <google/protobuf/io/coded_stream.h> #include <google/protobuf/io/zero_copy_stream.h> +#include <google/protobuf/stubs/once.h> #include <google/protobuf/util/internal/default_value_objectwriter.h> #include <google/protobuf/util/internal/error_listener.h> #include <google/protobuf/util/internal/json_objectwriter.h> @@ -42,8 +43,12 @@ #include <google/protobuf/util/type_resolver.h> #include <google/protobuf/util/type_resolver_util.h> #include <google/protobuf/stubs/bytestream.h> + + #include <google/protobuf/stubs/status_macros.h> +#include <google/protobuf/port_def.inc> + namespace google { namespace protobuf { namespace util { @@ -119,37 +124,51 @@ util::Status BinaryToJsonString(TypeResolver* resolver, namespace { class StatusErrorListener : public converter::ErrorListener { public: - StatusErrorListener() {} - virtual ~StatusErrorListener() override {} + ~StatusErrorListener() override {} util::Status GetStatus() { return status_; } virtual void InvalidName(const converter::LocationTrackerInterface& loc, - StringPiece unknown_name, StringPiece message) override { - status_ = util::Status(util::error::INVALID_ARGUMENT, - loc.ToString() + ": invalid name " + - string(unknown_name) + ": " + string(message)); - } - - virtual void InvalidValue(const converter::LocationTrackerInterface& loc, - StringPiece type_name, StringPiece value) override { + StringPiece unknown_name, + StringPiece message) { + string loc_string = GetLocString(loc); + if (!loc_string.empty()) { + loc_string.append(" "); + } status_ = util::Status(util::error::INVALID_ARGUMENT, - loc.ToString() + ": invalid value " + string(value) + - " for type " + string(type_name)); + StrCat(loc_string, unknown_name, ": ", message)); } - virtual void MissingField(const converter::LocationTrackerInterface& loc, - StringPiece missing_name) override { + virtual void InvalidValue(const converter::LocationTrackerInterface& loc, + StringPiece type_name, + StringPiece value) { status_ = util::Status( util::error::INVALID_ARGUMENT, - loc.ToString() + ": missing field " + string(missing_name)); + StrCat(GetLocString(loc), ": invalid value ", string(value), + " for type ", string(type_name))); + } + + virtual void MissingField(const converter::LocationTrackerInterface& loc, + StringPiece missing_name) { + status_ = util::Status(util::error::INVALID_ARGUMENT, + StrCat(GetLocString(loc), ": missing field ", + string(missing_name))); } private: util::Status status_; + string GetLocString(const converter::LocationTrackerInterface& loc) { + string loc_string = loc.ToString(); + StripWhitespace(&loc_string); + if (!loc_string.empty()) { + loc_string = StrCat("(", loc_string, ")"); + } + return loc_string; + } + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StatusErrorListener); }; } // namespace @@ -174,8 +193,8 @@ util::Status JsonToBinaryStream(TypeResolver* resolver, int length; while (json_input->Next(&buffer, &length)) { if (length == 0) continue; - RETURN_IF_ERROR( - parser.Parse(StringPiece(static_cast<const char*>(buffer), length))); + RETURN_IF_ERROR(parser.Parse( + StringPiece(static_cast<const char*>(buffer), length))); } RETURN_IF_ERROR(parser.FinishParse()); @@ -184,7 +203,7 @@ util::Status JsonToBinaryStream(TypeResolver* resolver, util::Status JsonToBinaryString(TypeResolver* resolver, const string& type_url, - const string& json_input, + StringPiece json_input, string* binary_output, const JsonParseOptions& options) { io::ArrayInputStream input_stream(json_input.data(), json_input.size()); @@ -196,7 +215,8 @@ util::Status JsonToBinaryString(TypeResolver* resolver, namespace { const char* kTypeUrlPrefix = "type.googleapis.com"; TypeResolver* generated_type_resolver_ = NULL; -GOOGLE_PROTOBUF_DECLARE_ONCE(generated_type_resolver_init_); +GOOGLE_PROTOBUF_NAMESPACE_ID::internal::once_flag + generated_type_resolver_init_; string GetTypeUrl(const Message& message) { return string(kTypeUrlPrefix) + "/" + message.GetDescriptor()->full_name(); @@ -211,7 +231,8 @@ void InitGeneratedTypeResolver() { } TypeResolver* GetGeneratedTypeResolver() { - ::google::protobuf::GoogleOnceInit(&generated_type_resolver_init_, &InitGeneratedTypeResolver); + GOOGLE_PROTOBUF_NAMESPACE_ID::internal::call_once( + generated_type_resolver_init_, InitGeneratedTypeResolver); return generated_type_resolver_; } } // namespace @@ -232,7 +253,7 @@ util::Status MessageToJsonString(const Message& message, string* output, return result; } -util::Status JsonStringToMessage(const string& input, Message* message, +util::Status JsonStringToMessage(StringPiece input, Message* message, const JsonParseOptions& options) { const DescriptorPool* pool = message->GetDescriptor()->file()->pool(); TypeResolver* resolver = |