Yifan Hong | cb7a7ab | 2019-02-08 16:26:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <jsonpb/jsonpb.h> |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <google/protobuf/descriptor.h> |
| 21 | #include <google/protobuf/message.h> |
| 22 | #include <google/protobuf/util/json_util.h> |
| 23 | #include <google/protobuf/util/type_resolver_util.h> |
| 24 | |
| 25 | namespace android { |
| 26 | namespace jsonpb { |
| 27 | |
| 28 | using google::protobuf::DescriptorPool; |
| 29 | using google::protobuf::Message; |
Yifan Hong | cb7a7ab | 2019-02-08 16:26:22 -0800 | [diff] [blame] | 30 | using google::protobuf::util::NewTypeResolverForDescriptorPool; |
| 31 | using google::protobuf::util::TypeResolver; |
| 32 | |
| 33 | static constexpr char kTypeUrlPrefix[] = "type.googleapis.com"; |
| 34 | |
| 35 | std::string GetTypeUrl(const Message& message) { |
Thiébaud Weksteen | f539748 | 2020-10-26 15:44:13 +0100 | [diff] [blame] | 36 | return std::string(kTypeUrlPrefix) + "/" + message.GetDescriptor()->full_name(); |
Yifan Hong | cb7a7ab | 2019-02-08 16:26:22 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | ErrorOr<std::string> MessageToJsonString(const Message& message) { |
Thiébaud Weksteen | f539748 | 2020-10-26 15:44:13 +0100 | [diff] [blame] | 40 | std::unique_ptr<TypeResolver> resolver( |
| 41 | NewTypeResolverForDescriptorPool(kTypeUrlPrefix, DescriptorPool::generated_pool())); |
Yifan Hong | cb7a7ab | 2019-02-08 16:26:22 -0800 | [diff] [blame] | 42 | |
Krzysztof Kosiński | f8423f6 | 2024-03-15 23:08:34 +0000 | [diff] [blame] | 43 | google::protobuf::util::JsonPrintOptions options; |
Thiébaud Weksteen | f539748 | 2020-10-26 15:44:13 +0100 | [diff] [blame] | 44 | options.add_whitespace = true; |
Yifan Hong | cb7a7ab | 2019-02-08 16:26:22 -0800 | [diff] [blame] | 45 | |
Thiébaud Weksteen | f539748 | 2020-10-26 15:44:13 +0100 | [diff] [blame] | 46 | std::string json; |
| 47 | auto status = BinaryToJsonString(resolver.get(), GetTypeUrl(message), message.SerializeAsString(), |
| 48 | &json, options); |
Yifan Hong | cb7a7ab | 2019-02-08 16:26:22 -0800 | [diff] [blame] | 49 | |
Thiébaud Weksteen | f539748 | 2020-10-26 15:44:13 +0100 | [diff] [blame] | 50 | if (!status.ok()) { |
Krzysztof Kosiński | f8423f6 | 2024-03-15 23:08:34 +0000 | [diff] [blame] | 51 | return MakeError<std::string>(std::string(status.message())); |
Thiébaud Weksteen | f539748 | 2020-10-26 15:44:13 +0100 | [diff] [blame] | 52 | } |
| 53 | return ErrorOr<std::string>(std::move(json)); |
Yifan Hong | cb7a7ab | 2019-02-08 16:26:22 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | namespace internal { |
| 57 | ErrorOr<std::monostate> JsonStringToMessage(const std::string& content, Message* message) { |
Thiébaud Weksteen | f539748 | 2020-10-26 15:44:13 +0100 | [diff] [blame] | 58 | std::unique_ptr<TypeResolver> resolver( |
| 59 | NewTypeResolverForDescriptorPool(kTypeUrlPrefix, DescriptorPool::generated_pool())); |
Yifan Hong | cb7a7ab | 2019-02-08 16:26:22 -0800 | [diff] [blame] | 60 | |
Thiébaud Weksteen | f539748 | 2020-10-26 15:44:13 +0100 | [diff] [blame] | 61 | std::string binary; |
| 62 | auto status = JsonToBinaryString(resolver.get(), GetTypeUrl(*message), content, &binary); |
| 63 | if (!status.ok()) { |
Krzysztof Kosiński | f8423f6 | 2024-03-15 23:08:34 +0000 | [diff] [blame] | 64 | return MakeError<std::monostate>(std::string(status.message())); |
Thiébaud Weksteen | f539748 | 2020-10-26 15:44:13 +0100 | [diff] [blame] | 65 | } |
| 66 | if (!message->ParseFromString(binary)) { |
| 67 | return MakeError<std::monostate>("Fail to parse."); |
| 68 | } |
| 69 | return ErrorOr<std::monostate>(); |
Yifan Hong | cb7a7ab | 2019-02-08 16:26:22 -0800 | [diff] [blame] | 70 | } |
| 71 | } // namespace internal |
| 72 | |
| 73 | } // namespace jsonpb |
| 74 | } // namespace android |