userver: protobuf::json Namespace Reference
Loading...
Searching...
No Matches
protobuf::json Namespace Reference

Detailed Description

Top namespace for the protobuf JSON utilities.

Classes

class  ConversionError
 JSON/protobuf conversion error. More...
class  ConversionErrorBase
 Base exception type for JSON/protobuf conversion errors. More...
class  ConversionErrorInfo
 JSON/protobuf conversion error information. More...
class  JsonError
 Base exception type for JSON utilities. More...
struct  ParseOptions
 Options which affect how JSON Value is converted to a protobuf message. More...
struct  PrintOptions
 Options which affect how protobuf message is converted to a JSON ValueBuilder/Value. More...

Typedefs

using ParseErrorInfo = ConversionErrorInfo<ParseErrorCode>
 JSON Value to protobuf message conversion error information.
using PrintErrorInfo = ConversionErrorInfo<PrintErrorCode>
 Protobuf message to JSON Value conversion error information.
using ParseError = ConversionError<ParseErrorCode>
 JSON Value to protobuf message conversion error.
using PrintError = ConversionError<PrintErrorCode>
 Protobuf message to JSON Value conversion error.

Enumerations

enum class  ParseErrorCode {
  kUnknownField = 1 ,
  kUnknownEnum = 2 ,
  kMultipleOneofFields = 3 ,
  kInvalidType = 4 ,
  kInvalidValue = 5
}
 JSON Value to protobuf message conversion error code. More...
enum class  PrintErrorCode { kInvalidValue = 1 }
 Protobuf message to JSON Value conversion error code. More...

Functions

formats::json::ValueBuilder MessageToJsonBuilder (const ::google::protobuf::Message &message, const PrintOptions &options)
 Converts protobuf message to JSON ValueBuilder.
formats::json::Value MessageToJson (const ::google::protobuf::Message &message, const PrintOptions &options)
 Converts protobuf message to JSON Value.
void JsonToMessage (const formats::json::Value &json, ::google::protobuf::Message &message, const ParseOptions &options={})
 Converts json to protobuf message .
template<typename T>
requires (std::is_base_of_v<::google::protobuf::Message, T> || !std::is_same_v<::google::protobuf::Message, T>)
JsonToMessage (const formats::json::Value &json, const ParseOptions &options={})
 Converts json to protobuf message of type T.
std::string MessageToJsonString (const ::google::protobuf::Message &message, const PrintOptions &options)
 Serializes protobuf message to a JSON string.
std::string MessageToDebugString (const ::google::protobuf::Message &message, std::size_t limit)
 Serializes protobuf message to a JSON string for debugging/logging, stopping early once limit bytes have been produced.

Typedef Documentation

◆ ParseError

JSON Value to protobuf message conversion error.

Definition at line 123 of file exceptions.hpp.

◆ ParseErrorInfo

JSON Value to protobuf message conversion error information.

Definition at line 80 of file exceptions.hpp.

◆ PrintError

Protobuf message to JSON Value conversion error.

Definition at line 126 of file exceptions.hpp.

◆ PrintErrorInfo

Protobuf message to JSON Value conversion error information.

Definition at line 83 of file exceptions.hpp.

Enumeration Type Documentation

◆ ParseErrorCode

enum class protobuf::json::ParseErrorCode
strong

JSON Value to protobuf message conversion error code.

Enumerator
kUnknownField 

JSON field is unknown (does not match to any protobuf message field).

kUnknownEnum 

Enum value name used as a JSON field value is unknown.

kMultipleOneofFields 

JSON contains more than one field for the same oneof in the protobuf message.

kInvalidType 

JSON field type is not compatible with corresponding protobuf message field type.

kInvalidValue 

JSON field value is invalid according to ProtoJSON rules.

Definition at line 17 of file exceptions.hpp.

◆ PrintErrorCode

enum class protobuf::json::PrintErrorCode
strong

Protobuf message to JSON Value conversion error code.

Enumerator
kInvalidValue 

Protobuf message field has invalid value. This code can be set when converting well-known message which may have more strict constraints than the types of their fields (see here for example).

Definition at line 35 of file exceptions.hpp.

Function Documentation

◆ JsonToMessage() [1/2]

void protobuf::json::JsonToMessage ( const formats::json::Value & json,
::google::protobuf::Message & message,
const ParseOptions & options = {} )

Converts json to protobuf message .

Exceptions
ParseErrorif conversion has failed
MemberMissingExceptionis json holds nothing The conversion is performed according to ProtoJSON specification.
Note
If conversion fails, message is left in a valid but unspecified state.
Warning
Most of the legacy ProtoJSON behavior introduced for compatibility with non-conformant implementations is not supported. This behavior may be disabled in the future versions of the protobuf library thus should not be relied upon.
The proto2 syntax is not fully supported and tested (at least extension fields are not supported).

Definition at line 19 of file convert.cpp.

◆ JsonToMessage() [2/2]

template<typename T>
requires (std::is_base_of_v<::google::protobuf::Message, T> || !std::is_same_v<::google::protobuf::Message, T>)
T protobuf::json::JsonToMessage ( const formats::json::Value & json,
const ParseOptions & options = {} )
nodiscard

Converts json to protobuf message of type T.

Template Parameters
Tprotobuf message type
Exceptions
ParseErrorif conversion has failed
MemberMissingExceptionis json holds nothing The conversion is performed according to ProtoJSON specification.
Warning
Most of the legacy ProtoJSON behavior introduced for compatibility with non-conformant implementations is not supported. This behavior may be disabled in the future versions of the protobuf library thus should not be relied upon.
The proto2 syntax is not fully supported and tested (at least extension fields are not supported).

Definition at line 80 of file convert.hpp.

◆ MessageToDebugString()

std::string protobuf::json::MessageToDebugString ( const ::google::protobuf::Message & message,
std::size_t limit )

Serializes protobuf message to a JSON string for debugging/logging, stopping early once limit bytes have been produced.

Unlike MessageToJson / MessageToJsonBuilder / MessageToJsonString, this is a debug serializer:

  • Field names are always the proto field names (preserve_proto_field_names), not json_name, to match the .proto definition.
  • Fields marked with the [debug_redact = true] option are hidden: their value is replaced with a "[REDACTED]" marker.
  • google.protobuf.Any is expanded (like MessageToJsonString with default options), but falls back to the raw representation instead of failing when the payload type can't be resolved in the descriptor pool or parsed.
  • Serialization stops early once limit bytes have been produced instead of serializing the whole message and only then truncating, which saves CPU on large messages. The already-open JSON containers are closed, so the truncated part before the marker stays a well-formed JSON document.
Parameters
messageThe protobuf message to convert.
limitMaximum size of the resulting string. The output may exceed it by at most one scalar value, since the traversal is interrupted at container-element granularity. This function does not append any truncation marker itself; callers that need one (e.g. for logging) should detect truncation by checking whether the result size exceeds limit.
Returns
JSON representation of message, truncated to (approximately) limit bytes if necessary.
Exceptions
PrintErrorif conversion has failed
Warning
This is a debug representation of protobuf that is unstable and should only be used for diagnostics. The order of keys in maps is unstable; the format itself can change even within a single run. You CANNOT parse back from this (possibly truncated) representation. You CANNOT use it for equality match with reference values in gtest.

Definition at line 31 of file convert.cpp.

◆ MessageToJson()

formats::json::Value protobuf::json::MessageToJson ( const ::google::protobuf::Message & message,
const PrintOptions & options )
inlinenodiscard

Converts protobuf message to JSON Value.

Exceptions
PrintErrorif conversion has failed The conversion is performed according to ProtoJSON specification.
Note
If protobuf enum value has multiple aliases (allow_alias enum option is on) then the first alias in the definition order is outputted.
Warning
Most of the legacy ProtoJSON behavior introduced for compatibility with non-conformant implementations is not supported. This behavior may be disabled in the future versions of the protobuf library thus should not be relied upon.
The proto2 syntax is not fully supported and tested (at least extension fields are not supported).

Definition at line 47 of file convert.hpp.

◆ MessageToJsonBuilder()

formats::json::ValueBuilder protobuf::json::MessageToJsonBuilder ( const ::google::protobuf::Message & message,
const PrintOptions & options )
nodiscard

Converts protobuf message to JSON ValueBuilder.

Exceptions
PrintErrorif conversion has failed The conversion is performed according to ProtoJSON specification.
Note
If protobuf enum value has multiple aliases (allow_alias enum option is on) then the first alias in the definition order is outputted.
Warning
Most of the legacy ProtoJSON behavior introduced for compatibility with non-conformant implementations is not supported. This behavior may be disabled in the future versions of the protobuf library thus should not be relied upon.
The proto2 syntax is not fully supported and tested (at least extension fields are not supported).

Definition at line 12 of file convert.cpp.

◆ MessageToJsonString()

std::string protobuf::json::MessageToJsonString ( const ::google::protobuf::Message & message,
const PrintOptions & options )

Serializes protobuf message to a JSON string.

Honors PrintOptions in exactly the same way as MessageToJson / MessageToJsonBuilder do (including nonportable_raw_any, which controls whether google.protobuf.Any is expanded or emitted raw). In particular, [debug_redact = true] fields are not redacted here either, same as in MessageToJson / MessageToJsonBuilder.

Parameters
messageThe protobuf message to convert.
optionsSame conversion options as for MessageToJson / MessageToJsonBuilder.
Returns
ProtoJSON representation of message.
Exceptions
PrintErrorif conversion has failed

Definition at line 27 of file convert.cpp.