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