kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/json_proto.h (about)

     1  /*
     2   * Copyright 2015 The Kythe Authors. All rights reserved.
     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  #ifndef KYTHE_CXX_COMMON_JSON_PROTO_H_
    18  #define KYTHE_CXX_COMMON_JSON_PROTO_H_
    19  
    20  #include <string>
    21  
    22  #include "absl/status/status.h"
    23  #include "absl/status/statusor.h"
    24  #include "absl/strings/string_view.h"
    25  #include "google/protobuf/any.pb.h"
    26  #include "google/protobuf/io/zero_copy_stream.h"
    27  #include "google/protobuf/message.h"
    28  #include "google/protobuf/util/json_util.h"
    29  
    30  namespace kythe {
    31  
    32  /// \brief Deserializes a protobuf from JSON text.
    33  /// \param input The input text to parse.
    34  /// \param message The message to parse.
    35  /// \return The status message result of parsing.
    36  absl::Status ParseFromJsonString(absl::string_view input,
    37                                   google::protobuf::Message* message);
    38  
    39  /// \brief Deserializes a protobuf from JSON text.
    40  /// \param input The input text to parse.
    41  /// \param options The JsonParseOptions to use.
    42  /// \param message The message to parse.
    43  /// \return The status message result of parsing.
    44  absl::Status ParseFromJsonString(
    45      absl::string_view input,
    46      const google::protobuf::util::JsonParseOptions& options,
    47      google::protobuf::Message* message);
    48  
    49  /// \brief Deserializes a protobuf from a JSON text stream.
    50  /// \param stream The input stream from which to read.
    51  /// \param message The message to parse.
    52  /// \return The status message result of parsing.
    53  absl::Status ParseFromJsonStream(
    54      google::protobuf::io::ZeroCopyInputStream* input,
    55      google::protobuf::Message* message);
    56  
    57  /// \brief Deserializes a protobuf from a JSON text stream.
    58  /// \param input The input stream from which to read.
    59  /// \param options The JsonParseOptions to use.
    60  /// \param message The message to parse.
    61  /// \return The status message result of parsing.
    62  absl::Status ParseFromJsonStream(
    63      google::protobuf::io::ZeroCopyInputStream* input,
    64      const google::protobuf::util::JsonParseOptions& options,
    65      google::protobuf::Message* message);
    66  
    67  /// \brief Serializes a protobuf to JSON form, including the format wrapper.
    68  /// \param message The protobuf to serialize.
    69  /// \param format_key Specifies the format to declare in the wrapper.
    70  /// \param out Set to the serialized message on success.
    71  /// \return True on success; false on failure.
    72  bool WriteMessageAsJsonToString(const google::protobuf::Message& message,
    73                                  const std::string& format_key,
    74                                  std::string* out);
    75  
    76  /// \brief Serializes a protobuf to JSON form with no wrapper.
    77  /// \param message The protobuf to serialize.
    78  /// \param out Set to the serialized message on success.
    79  /// \return True on success; false on failure.
    80  bool WriteMessageAsJsonToString(const google::protobuf::Message& message,
    81                                  std::string* out);
    82  
    83  /// \brief Serializes a protobuf to JSON form with no wrapper.
    84  /// \param message The protobuf to serialize.
    85  /// \return JSON string on success; Status on failure.
    86  absl::StatusOr<std::string> WriteMessageAsJsonToString(
    87      const google::protobuf::Message& message);
    88  
    89  /// \brief Wrap a protobuf up into an Any.
    90  /// \param message The message to wrap.
    91  /// \param type_uri The URI of the message type.
    92  /// \param out The resulting Any.
    93  void PackAny(const google::protobuf::Message& message,
    94               absl::string_view type_uri, google::protobuf::Any* out);
    95  
    96  /// \brief Unpack a protobuf from an Any.
    97  /// \param any The Any to unpack.
    98  /// \param result The message to unpack it over.
    99  /// \return false if unpacking failed
   100  bool UnpackAny(const google::protobuf::Any& any,
   101                 google::protobuf::Message* result);
   102  
   103  }  // namespace kythe
   104  
   105  #endif  // KYTHE_CXX_COMMON_JSON_PROTO_H_