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  ReadOptions
 JSON read options which affect how JSON Value is converted to a protobuf message. More...
 
struct  WriteOptions
 JSON write options which affect how protobuf message is converted to a JSON ValueBuilder/Value. More...
 

Typedefs

using ReadErrorInfo = ConversionErrorInfo< ReadErrorCode >
 JSON read error information.
 
using WriteErrorInfo = ConversionErrorInfo< WriteErrorCode >
 JSON write error information.
 
using ReadError = ConversionError< ReadErrorCode >
 JSON read error.
 
using WriteError = ConversionError< WriteErrorCode >
 JSON write error.
 

Enumerations

enum class  ReadErrorCode {
  kUnknownField = 1 ,
  kUnknownEnum = 2 ,
  kMultipleOneofFields = 3 ,
  kInvalidType = 4 ,
  kInvalidValue = 5
}
 JSON read error code. More...
 
enum class  WriteErrorCode { kInvalidValue = 1 }
 JSON write error code. More...
 

Functions

formats::json::ValueBuilder MessageToJsonBuilder (const ::google::protobuf::Message &message, const WriteOptions &options={})
 Converts protobuf message to JSON ValueBuider.
 
void JsonToMessage (const formats::json::Value &json, const ReadOptions &options, ::google::protobuf::Message &message)
 Converts json to protobuf message .
 
formats::json::Value MessageToJson (const ::google::protobuf::Message &message, const WriteOptions &options={})
 Converts protobuf message to JSON Value.
 
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 ReadOptions &options={})
 Converts json to protobuf message of type T.
 

Typedef Documentation

◆ ReadError

JSON read error.

Definition at line 126 of file exceptions.hpp.

◆ ReadErrorInfo

JSON read error information.

Definition at line 83 of file exceptions.hpp.

◆ WriteError

JSON write error.

Definition at line 129 of file exceptions.hpp.

◆ WriteErrorInfo

JSON write error information.

Definition at line 86 of file exceptions.hpp.

Enumeration Type Documentation

◆ ReadErrorCode

enum class protobuf::json::ReadErrorCode
strong

JSON read 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.

◆ WriteErrorCode

enum class protobuf::json::WriteErrorCode
strong

JSON write error code.

Enumerator
kInvalidValue 

Protobuf well-known message does not have expected field.

Protobuf message field has invalid value. This code can be set when converting well-known message which may have more strict constraints than the typesof 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,
const ReadOptions options,
::google::protobuf::Message &  message 
)

Converts json to protobuf message .

Exceptions
ReadErrorif reading protobuf message from JSON 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 and required label are not supported, but also some other proto2-specific parts of the syntax may not be correctly handled.

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 ReadOptions options = {} 
)

Converts json to protobuf message of type T.

Template Parameters
Tprotobuf message type
Exceptions
ReadErrorif reading protobuf message from JSON 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 and required label are not supported, but also some other proto2-specific parts of the syntax may not be correctly handled.

Definition at line 80 of file convert.hpp.

◆ MessageToJson()

formats::json::Value protobuf::json::MessageToJson ( const ::google::protobuf::Message &  message,
const WriteOptions options = {} 
)
inline

Converts protobuf message to JSON Value.

Exceptions
WriteErrorif writing protobuf message to JSON 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 and required label are not supported, but also some other proto2-specific parts of the syntax may not be correctly handled.

Definition at line 47 of file convert.hpp.

◆ MessageToJsonBuilder()

formats::json::ValueBuilder protobuf::json::MessageToJsonBuilder ( const ::google::protobuf::Message &  message,
const WriteOptions options = {} 
)

Converts protobuf message to JSON ValueBuider.

Exceptions
WriteErrorif writing protobuf message to JSON 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 and required label are not supported, but also some other proto2-specific parts of the syntax may not be correctly handled.

Definition at line 10 of file convert.cpp.