xref: /aosp_15_r20/system/extras/libjsonpb/parse/include/jsonpb/jsonpb.h (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 #pragma once
18*288bf522SAndroid Build Coastguard Worker 
19*288bf522SAndroid Build Coastguard Worker #include <string>
20*288bf522SAndroid Build Coastguard Worker 
21*288bf522SAndroid Build Coastguard Worker #include <jsonpb/error_or.h>
22*288bf522SAndroid Build Coastguard Worker 
23*288bf522SAndroid Build Coastguard Worker #include <google/protobuf/message.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 namespace internal {
29*288bf522SAndroid Build Coastguard Worker ErrorOr<std::monostate> JsonStringToMessage(const std::string& content,
30*288bf522SAndroid Build Coastguard Worker                                             google::protobuf::Message* message);
31*288bf522SAndroid Build Coastguard Worker }  // namespace internal
32*288bf522SAndroid Build Coastguard Worker 
33*288bf522SAndroid Build Coastguard Worker // TODO: JsonStringToMessage is a newly added function in protobuf
34*288bf522SAndroid Build Coastguard Worker // and is not yet available in the android tree. Replace this function with
35*288bf522SAndroid Build Coastguard Worker // https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.util.json_util#JsonStringToMessage.details
36*288bf522SAndroid Build Coastguard Worker // when the android tree gets updated
37*288bf522SAndroid Build Coastguard Worker template <typename T>
JsonStringToMessage(const std::string & content)38*288bf522SAndroid Build Coastguard Worker ErrorOr<T> JsonStringToMessage(const std::string& content) {
39*288bf522SAndroid Build Coastguard Worker   ErrorOr<T> ret;
40*288bf522SAndroid Build Coastguard Worker   auto error = internal::JsonStringToMessage(content, &*ret);
41*288bf522SAndroid Build Coastguard Worker   if (!error.ok()) {
42*288bf522SAndroid Build Coastguard Worker     return MakeError<T>(error.error());
43*288bf522SAndroid Build Coastguard Worker   }
44*288bf522SAndroid Build Coastguard Worker   return ret;
45*288bf522SAndroid Build Coastguard Worker }
46*288bf522SAndroid Build Coastguard Worker 
47*288bf522SAndroid Build Coastguard Worker // TODO: MessageToJsonString is a newly added function in protobuf
48*288bf522SAndroid Build Coastguard Worker // and is not yet available in the android tree. Replace this function with
49*288bf522SAndroid Build Coastguard Worker // https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.util.json_util#MessageToJsonString.details
50*288bf522SAndroid Build Coastguard Worker // when the android tree gets updated.
51*288bf522SAndroid Build Coastguard Worker //
52*288bf522SAndroid Build Coastguard Worker // The new MessageToJsonString also allows preserving proto field names. However,
53*288bf522SAndroid Build Coastguard Worker // the function here can't. Hence, a field name "foo_bar" without json_name option
54*288bf522SAndroid Build Coastguard Worker // will be "fooBar" in the final output. Additional checks are needed to ensure
55*288bf522SAndroid Build Coastguard Worker // that doesn't happen.
56*288bf522SAndroid Build Coastguard Worker ErrorOr<std::string> MessageToJsonString(const google::protobuf::Message& message);
57*288bf522SAndroid Build Coastguard Worker 
58*288bf522SAndroid Build Coastguard Worker }  // namespace jsonpb
59*288bf522SAndroid Build Coastguard Worker }  // namespace android
60