AnCH codec library namespace.
More...
|
| namespace | media |
| | Common media types.
|
|
| std::string | readContent (std::istream &input, std::size_t maxSize) |
| std::size_t & | maxBodySize (anch::codec::Side side) |
| std::string | readBody (std::istream &input, anch::codec::Side side) |
template<typename C, typename T>
requires anch::codec::Serializer<C, T> |
| anch::codec::MediaType | contentTypeOf (const T &value) |
template<typename C, typename T>
requires anch::codec::Serializer<C, T> |
| void | serialize (const T &value, std::ostream &out, const anch::codec::MediaType &contentType, anch::codec::Side side) |
template<typename C, typename T>
requires anch::codec::Deserializer<C, T> |
| void | deserialize (T &value, std::istream &input, const anch::codec::MediaType &contentType, anch::codec::Side side) |
| bool | registerFormat (anch::codec::Format format) |
| std::vector< anch::codec::Format > | formats () |
| std::optional< std::size_t > | negotiate (const std::vector< anch::codec::MediaType > &accept, const std::vector< anch::codec::MediaType > &available) |
| std::string_view | thriftMediaType (anch::codec::ThriftProtocol protocol) noexcept |
| std::optional< anch::codec::ThriftProtocol > | thriftProtocolOf (const anch::codec::MediaType &mediaType) |
| std::shared_ptr< apache::thrift::protocol::TProtocol > | thriftProtocol (anch::codec::ThriftProtocol protocol, std::shared_ptr< apache::thrift::transport::TTransport > transport) |
| std::string | thriftWrite (anch::codec::ThriftProtocol protocol, const std::function< void(apache::thrift::protocol::TProtocol *)> &write) |
| void | thriftRead (anch::codec::ThriftProtocol protocol, std::string_view content, const std::function< void(apache::thrift::protocol::TProtocol *)> &read) |
AnCH codec library namespace.
It contains codecs concepts (Serializer, Deserializer), media types (MediaType) and their negotiation, the Text codec and the codecs of format plugins (Json, ...). See AnCH codec library documentation.
◆ Csv
text/csv codec based on anch-csv (anch-codec-csv plugin, option ANCH_CODEC_CSV ): values are lists of described objects ( anch::Mapping , anch-utils), one record per object. Options of each side: see FormatPolicy.
- Since
- 0.3
◆ Json
application/json codec based on anch-json (anch-codec-json plugin, option ANCH_CODEC_JSON ): values need a description ( anch::Mapping , anch-utils). Options of each side: see FormatPolicy.
- Since
- 0.3 (
anch::rest::Json in 0.2)
◆ ThriftBinary
application/vnd.apache.thrift.binary codec (see ThriftCodec )
◆ ThriftCompact
application/vnd.apache.thrift.compact codec (see ThriftCodec )
◆ ThriftJson
application/vnd.apache.thrift.json codec (see ThriftCodec )
◆ Side
Trust policy of a codec user: what is checked and bounded when values are read and written.
SERVER reads data it does not control and trusts what it builds ; CLIENT checks what it sends and trusts what it reads. Codecs can behave according to it: anch::codec::Json checks constraints of values read with SERVER and of values written with CLIENT , and bounds what SERVER reads.
anch-rest servers use SERVER and clients CLIENT ; a message consumer reads with SERVER and a producer writes with CLIENT .
- Since
- 0.2
| Enumerator |
|---|
| SERVER | Read values are checked and bounded, written values are trusted (HTTP server, message consumer)
|
| CLIENT | Written values are checked, read values are trusted (HTTP client, message producer)
|
◆ ThriftProtocol
Thrift protocols
- Since
- 0.2
| Enumerator |
|---|
| BINARY | Binary protocol: application/vnd.apache.thrift.binary ( application/x-thrift for RPC)
|
| COMPACT | Compact protocol: application/vnd.apache.thrift.compact
|
| JSON | JSON protocol: application/vnd.apache.thrift.json
|
◆ contentTypeOf()
Get content type of a value serialized with codec: its contentType(value) function is used when it is defined, mediaType() otherwise
- Template Parameters
-
| C | the codec type |
| T | the value type |
- Parameters
-
- Returns
- the media type
- Exceptions
-
| std::invalid_argument | when codec media type is invalid |
- Since
- 0.2
◆ deserialize()
Deserialize value with codec: its deserialize(value, input, contentType, side) function is used when it is defined, then deserialize(value, input, side) , then deserialize(value, input)
- Template Parameters
-
| C | the codec type |
| T | the value type |
- Parameters
-
| value | the value to fill |
| input | the input stream |
| contentType | the content media type, with its parameters |
| side | the side which deserializes value |
- Since
- 0.3 (
anch::rest::deserializeBody in 0.2)
◆ formats()
Get registered formats (thread safe)
- Returns
- the formats sorted by name
- Since
- 0.3
◆ maxBodySize()
Maximum size of contents read in memory by codecs which have no limit of their own (Protobuf, Protobuf JSON, Thrift), to change before reading contents (0 for no limit).
Default: 2 MiB for SERVER , as JSON and form bodies ; no limit for CLIENT (anch-rest responses are bounded by transports: TransportConfig::maxResponseSize ).
- Parameters
-
- Returns
- the maximum size in bytes
- Since
- 0.3
◆ negotiate()
Select the best available media type for Accept media ranges (RFC 9110 proactive negotiation): the quality of an available media type is the quality of the most specific media range which includes it. On equal quality, the first available media type is selected.
- Parameters
-
| accept | the Accept media ranges ; empty means that every media type is accepted |
| available | the available media types |
- Returns
- the selected media type index, empty when no media type is acceptable
◆ readBody()
Read whole content with the limit of a side (maxBodySize )
- Parameters
-
| input | the input stream |
| side | the side |
- Returns
- the content
- Exceptions
-
- Since
- 0.3
◆ readContent()
| std::string anch::codec::readContent |
( |
std::istream & | input, |
|
|
std::size_t | maxSize ) |
Read whole content
- Parameters
-
| input | the input stream |
| maxSize | the maximum content size in bytes |
- Returns
- the content
- Exceptions
-
- Since
- 0.2
◆ registerFormat()
Register format. A format which is already registered (same name, from several shared libraries for example) is not registered twice.
- Parameters
-
- Returns
true when format has been registered, false when it was already registered
- Since
- 0.3
◆ serialize()
Serialize value with codec: its serialize(value, out, contentType, side) function is used when it is defined, then serialize(value, out, side) , then serialize(value, out)
- Template Parameters
-
| C | the codec type |
| T | the value type |
- Parameters
-
| value | the value |
| out | the output stream |
| contentType | the content media type (from contentTypeOf ) |
| side | the side which serializes value |
- Since
- 0.3 (
anch::rest::serializeBody in 0.2)
◆ thriftMediaType()
Get protocol media type
- Parameters
-
- Returns
- the media type
- Since
- 0.2
◆ thriftProtocol()
| std::shared_ptr< apache::thrift::protocol::TProtocol > anch::codec::thriftProtocol |
( |
anch::codec::ThriftProtocol | protocol, |
|
|
std::shared_ptr< apache::thrift::transport::TTransport > | transport ) |
Create Thrift protocol over a transport
- Parameters
-
| protocol | the protocol |
| transport | the transport |
- Returns
- the protocol
- Since
- 0.2
◆ thriftProtocolOf()
Get protocol of a media type ( application/x-thrift is the binary protocol, as Thrift HTTP transports)
- Parameters
-
- Returns
- the protocol, empty when media type is not a Thrift media type
- Since
- 0.2
◆ thriftRead()
| void anch::codec::thriftRead |
( |
anch::codec::ThriftProtocol | protocol, |
|
|
std::string_view | content, |
|
|
const std::function< void(apache::thrift::protocol::TProtocol *)> & | read ) |
Read structure with protocol
- Parameters
-
| protocol | the protocol |
| content | the encoded structure |
| read | the structure reader |
- Exceptions
-
| std::invalid_argument | when content is not a valid structure (missing required field, ...) |
- Since
- 0.2
◆ thriftWrite()
| std::string anch::codec::thriftWrite |
( |
anch::codec::ThriftProtocol | protocol, |
|
|
const std::function< void(apache::thrift::protocol::TProtocol *)> & | write ) |
Write structure with protocol
- Parameters
-
| protocol | the protocol |
| write | the structure writer |
- Returns
- the encoded structure
- Since
- 0.2