AnCH codec library associates content types ( MediaType ) with serialization and deserialization of C++ values. anch-rest uses codecs for requests and responses bodies ; they do not depend on HTTP, so that other libraries (messaging) read and write their contents with the same codecs.
anch-codec is built with anch-rest (since 0.3, codecs were part of anch-rest in 0.2). The core has no format dependency ; each format is a plugin library built according to its option. Plugins with external dependencies are skipped with a CMake status message when their dependencies are not found.
| CMake target | Option | Content |
|---|---|---|
anch::codec | concepts, MediaType and negotiation, limits, Side, Text, Codecs, formats registry | |
anch::codec-json | ANCH_CODEC_JSON (needed by anch-rest) | Json (anch-json) |
anch::codec-csv | ANCH_CODEC_CSV (anch-csv) | Csv (lists of described objects) |
anch::codec-protobuf | ANCH_CODEC_PROTOBUF (Protobuf) | Protobuf, ProtobufJson |
anch::codec-thrift | ANCH_CODEC_THRIFT (Thrift and Boost headers) | ThriftBinary, ThriftCompact, ThriftJson |
anch::codec-all | codec/all.hpp : every plugin built and anch::codec::Available |
See installation.
A codec is a type with static functions: mediaType() and serialize / deserialize , with one of their forms (value and stream ; plus Side ; plus content type and side), see Serializer and Deserializer:
contentTypeOf, serialize and deserialize call the richest form a codec defines:
A data format ( DataFormat: anch::json::JsonFormat , anch::csv::CsvFormat ) serializes values with options, without depending on anch-codec. FormatCodec makes a codec of it: media type, options of each side copied from the format default options and configured by the format FormatPolicy (specialized by its plugin, which also translates errors into ContentTooLarge). Json is FormatCodec<anch::json::JsonFormat> and Csv FormatCodec<anch::csv::CsvFormat> ; a new text format gets its codec without code of its own.
Side is the trust policy of the codec user: SERVER reads data it does not control (checked and bounded) and trusts what it builds ; CLIENT checks what it sends and trusts what it reads. anch-rest servers use SERVER and clients CLIENT ; a message consumer reads with SERVER and a producer writes with CLIENT .
SERVER and on values written with CLIENT , and has mapping options per side ( options , configured by its policy): SERVER by default): larger contents raise ContentTooLarge ( 413 in anch-rest servers).Codecs is a list of codecs resolved at compilation: it selects the codec of a content type when reading, and negotiates the codec of accepted media ranges ( Accept , q-values) when writing:
codec/all.hpp (target anch::codec-all ) is generated with the plugins built: it includes their headers, defines ANCH_CODEC_JSON_ENABLED , ANCH_CODEC_CSV_ENABLED , ANCH_CODEC_PROTOBUF_ENABLED and ANCH_CODEC_THRIFT_ENABLED , and declares anch::codec::Available , the list of their codecs (Json, Csv, Protobuf, ProtobufJson, ThriftBinary, ThriftCompact, ThriftJson, then Text). Types described by an external schema (Protobuf messages, Thrift structures) are not described by anch::Mapping : Json does not handle them ( anch::json::Mappable ), so the list selects the codecs of their format. Each plugin registers its format when its library is loaded ( formats, for introspection: accepted media types, startup logs, ...):
protoc messages are read and written by Protobuf ( application/x-protobuf ) and ProtobufJson ( application/json , Protobuf JSON mapping) codecs. Structures generated by thrift –gen cpp are read and written by ThriftBinary ( application/vnd.apache.thrift.binary ), ThriftCompact ( .compact ) and ThriftJson ( .json ). Their RPC (Connect, gRPC-Web, gRPC, Thrift over HTTP) is in anch-rest (see AnCH REST library documentation).