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 ValueBuider.
 
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, typename = std::enable_if_t< 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.
 

Typedef Documentation

◆ ParseError

JSON Value to protobuf message conversion error.

Definition at line 125 of file exceptions.hpp.

◆ ParseErrorInfo

JSON Value to protobuf message conversion error information.

Definition at line 82 of file exceptions.hpp.

◆ PrintError

Protobuf message to JSON Value conversion error.

Definition at line 128 of file exceptions.hpp.

◆ PrintErrorInfo

Protobuf message to JSON Value conversion error information.

Definition at line 85 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 perfomed 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 compatability 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 17 of file convert.cpp.

◆ JsonToMessage() [2/2]

template<typename T, typename = std::enable_if_t< 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 perfomed according to ProtoJSON specification.
Warning
Most of the legacy ProtoJSON behavior introduced for compatability 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.

◆ 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 perfomed 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 compatability 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 45 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 ValueBuider.

Exceptions
PrintErrorif conversion has failed The conversion is perfomed 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 compatability 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 10 of file convert.cpp.