Package io.servicetalk.http.api
Class HttpSerializers
java.lang.Object
io.servicetalk.http.api.HttpSerializers
Factory for creation of
HttpSerializerDeserializer
and HttpStreamingSerializerDeserializer
.-
Method Summary
Modifier and TypeMethodDescriptionCreates aHttpStreamingSerializerDeserializer
that serializesString
s withStandardCharsets.US_ASCII
encoding using fixedint
length delimited framing.Creates aHttpStreamingSerializerDeserializer
that serializesString
s withStandardCharsets.US_ASCII
encoding using variableint
length delimited framing.appSerializerFixLen
(Charset charset) Creates aHttpStreamingSerializerDeserializer
that serializesString
s withcharset
encoding using fixedint
length delimited framing.Creates aHttpStreamingSerializerDeserializer
that serializesString
s withStandardCharsets.UTF_8
encoding using fixedint
length delimited framing.Creates aHttpStreamingSerializerDeserializer
that serializesString
s withStandardCharsets.UTF_8
encoding using variableint
length delimited framing.appSerializerVarLen
(Charset charset) Creates aHttpStreamingSerializerDeserializer
that serializesString
s withcharset
encoding using fixedint
length delimited framing.static HttpStreamingSerializer<byte[]>
bytesStreamingSerializer
(Consumer<HttpHeaders> headersSerializeConsumer) Create aHttpStreamingSerializer
that serializesbyte[]
.static HttpSerializerDeserializer<Map<String,
List<String>>> Get aHttpSerializerDeserializer
that can serialize a key-valuesMap
s withStandardCharsets.UTF_8
Charset
to urlencoded forms.static HttpSerializerDeserializer<Map<String,
List<String>>> formUrlEncodedSerializer
(Charset charset) Get aHttpSerializerDeserializer
that can serialize a key-valuesMap
s with aCharset
to urlencoded forms.static <T> HttpSerializerDeserializer<T>
jsonSerializer
(SerializerDeserializer<T> serializer) Creates anHttpSerializerDeserializer
that targetsHttpHeaderValues.APPLICATION_JSON
.static <T> HttpStreamingSerializerDeserializer<T>
jsonStreamingSerializer
(StreamingSerializerDeserializer<T> serializer) Creates anHttpStreamingSerializerDeserializer
that targetsHttpHeaderValues.APPLICATION_JSON
.static <T> HttpSerializerDeserializer<T>
serializer
(SerializerDeserializer<T> serializer, Consumer<HttpHeaders> headersSerializeConsumer, Predicate<HttpHeaders> headersDeserializePredicate) Creates anHttpSerializerDeserializer
that usesSerializerDeserializer
for serialization.static <T> HttpStreamingSerializer<T>
streamingSerializer
(Serializer<T> serializer, ToIntFunction<T> bytesEstimator, Consumer<HttpHeaders> headersSerializeConsumer) Create aHttpStreamingSerializer
that uses aSerializer
for serialization.static <T> HttpStreamingSerializerDeserializer<T>
streamingSerializer
(StreamingSerializerDeserializer<T> serializer, Consumer<HttpHeaders> headersSerializeConsumer, Predicate<HttpHeaders> headersDeserializePredicate) Creates anHttpStreamingSerializerDeserializer
that usesStreamingSerializerDeserializer
for serialization.static HttpStreamingSerializer<String>
stringStreamingSerializer
(Charset charset, Consumer<HttpHeaders> headersSerializeConsumer) Create aHttpStreamingSerializer
that serializesString
.static HttpSerializerDeserializer<String>
textSerializer
(Charset charset) static HttpSerializerDeserializer<String>
static HttpSerializerDeserializer<String>
-
Method Details
-
formUrlEncodedSerializer
Get aHttpSerializerDeserializer
that can serialize a key-valuesMap
s withStandardCharsets.UTF_8
Charset
to urlencoded forms.- Returns:
HttpSerializerDeserializer
that could serialize key-valueMap
.- See Also:
-
formUrlEncodedSerializer
public static HttpSerializerDeserializer<Map<String,List<String>>> formUrlEncodedSerializer(Charset charset) Get aHttpSerializerDeserializer
that can serialize a key-valuesMap
s with aCharset
to urlencoded forms.- Parameters:
charset
- TheCharset
to use for value encoding.- Returns:
HttpSerializerDeserializer
that could serialize key-valueMap
.- See Also:
-
textSerializerUtf8
- Returns:
HttpSerializerDeserializer
that can serializeString
s.
-
textSerializerAscii
- Returns:
HttpSerializerDeserializer
that can serializeString
s.
-
textSerializer
- Parameters:
charset
- TheCharset
to use for encoding.- Returns:
HttpSerializerDeserializer
that can serializeString
s.
-
appSerializerUtf8FixLen
Creates aHttpStreamingSerializerDeserializer
that serializesString
s withStandardCharsets.UTF_8
encoding using fixedint
length delimited framing. The framing is required so the sameString
objects can be deserialized by the peer, otherwise the boundaries aren't known. If the desire is to serialize raw data contained in theString
, seestringStreamingSerializer(Charset, Consumer)
. TheHttpHeaderNames.CONTENT_TYPE
value prefix is "application/text-fix-int".- Returns:
- a
HttpStreamingSerializerDeserializer
that serializesString
s withStandardCharsets.UTF_8
encoding using fixedint
length delimited framing. - See Also:
-
appSerializerUtf8VarLen
Creates aHttpStreamingSerializerDeserializer
that serializesString
s withStandardCharsets.UTF_8
encoding using variableint
length delimited framing. The framing is required so the sameString
objects can be deserialized by the peer, otherwise the boundaries aren't known. If the desire is to serialize raw data contained in theString
, seestringStreamingSerializer(Charset, Consumer)
. TheHttpHeaderNames.CONTENT_TYPE
value prefix is "application/text-var-int".- Returns:
- a
HttpStreamingSerializerDeserializer
that serializesString
s withStandardCharsets.UTF_8
encoding using variableint
length delimited framing. - See Also:
-
appSerializerAsciiFixLen
Creates aHttpStreamingSerializerDeserializer
that serializesString
s withStandardCharsets.US_ASCII
encoding using fixedint
length delimited framing. The framing is required so the sameString
objects can be deserialized by the peer, otherwise the boundaries aren't known. If the desire is to serialize raw data contained in theString
, seestringStreamingSerializer(Charset, Consumer)
. TheHttpHeaderNames.CONTENT_TYPE
value prefix is "application/text-fix-int".- Returns:
- a
HttpStreamingSerializerDeserializer
that serializesString
s withStandardCharsets.US_ASCII
encoding using fixedint
length delimited framing. - See Also:
-
appSerializerAsciiVarLen
Creates aHttpStreamingSerializerDeserializer
that serializesString
s withStandardCharsets.US_ASCII
encoding using variableint
length delimited framing. The framing is required so the sameString
objects can be deserialized by the peer, otherwise the boundaries aren't known. If the desire is to serialize raw data contained in theString
, seestringStreamingSerializer(Charset, Consumer)
. TheHttpHeaderNames.CONTENT_TYPE
value prefix is "application/text-var-int".- Returns:
- a
HttpStreamingSerializerDeserializer
that serializesString
s withStandardCharsets.US_ASCII
encoding using variableint
length delimited framing. - See Also:
-
appSerializerFixLen
Creates aHttpStreamingSerializerDeserializer
that serializesString
s withcharset
encoding using fixedint
length delimited framing. The framing is required so the sameString
objects can be deserialized by the peer, otherwise the boundaries aren't known. If the desire is to serialize raw data contained in theString
, seestringStreamingSerializer(Charset, Consumer)
. TheHttpHeaderNames.CONTENT_TYPE
value prefix is "application/text-fix-int".- Parameters:
charset
- The character encoding to use for serialization.- Returns:
- a
HttpStreamingSerializerDeserializer
that serializesString
s withcharset
encoding using fixedint
length delimited framing. - See Also:
-
appSerializerVarLen
Creates aHttpStreamingSerializerDeserializer
that serializesString
s withcharset
encoding using fixedint
length delimited framing. The framing is required so the sameString
objects can be deserialized by the peer, otherwise the boundaries aren't known. If the desire is to serialize raw data contained in theString
, seestringStreamingSerializer(Charset, Consumer)
. TheHttpHeaderNames.CONTENT_TYPE
value prefix is "application/text-var-int".- Parameters:
charset
- The character encoding to use for serialization.- Returns:
- a
HttpStreamingSerializerDeserializer
that serializesString
s withcharset
encoding using fixedint
length delimited framing. - See Also:
-
stringStreamingSerializer
public static HttpStreamingSerializer<String> stringStreamingSerializer(Charset charset, Consumer<HttpHeaders> headersSerializeConsumer) Create aHttpStreamingSerializer
that serializesString
. This method is useful if the payload body is provided inString
and theHttpHeaderNames.CONTENT_TYPE
is known a-priori (e.g. streaming raw json data from a stream ofString
s). Deserialization should be done using the a-priori knowledge to use a compatibleHttpStreamingDeserializer
. To elaborate why a-priori knowledge of how content is consumed/deserialized is required consider the following caveats of this method:- Lack of framing - There is no guarantee deserialization will be able to recover the same
String
objects. For example, if("foo", "bar")
is serialized the deserialization may not be able to reliably return"foo"
and"bar"
. If the result of the serialization is sent as a stream of bytes (e.g. TCP over a network) the deserialization will produceString
s as read from the stream. For example, if the deserialization is done in a streaming fashion this is a valid result: ("f", "o", "oba", "r"
). If the receiver always aggregates data before deserialization they will reliably get"foobar"
. - Broken encoding - If your
String
encoding represents a single character with multiple bytes (e.g.StandardCharsets.UTF_8
) the deserialization may encounter errors if the results are processed as a stream of bytes (see description above).
appSerializerFixLen(Charset)
andappSerializerVarLen(Charset)
introduce a framing and therefore are not subject to the above caveats.- Parameters:
charset
- The character encoding to use for serialization.headersSerializeConsumer
- Sets the headers to indicate the appropriate encoding and content type.- Returns:
- a
HttpStreamingSerializer
that uses aSerializer
for serialization.
- Lack of framing - There is no guarantee deserialization will be able to recover the same
-
bytesStreamingSerializer
public static HttpStreamingSerializer<byte[]> bytesStreamingSerializer(Consumer<HttpHeaders> headersSerializeConsumer) Create aHttpStreamingSerializer
that serializesbyte[]
. This method is useful if the payload body is provided inbyte[]
and theHttpHeaderNames.CONTENT_TYPE
is known a-priori (e.g. streaming raw json data from a stream ofbyte[]
s). Deserialization should be done using the a-priori knowledge to use a compatibleHttpStreamingDeserializer
.- Parameters:
headersSerializeConsumer
- Sets the headers to indicate the appropriate encoding and content type.- Returns:
- a
HttpStreamingSerializer
that uses aSerializer
for serialization.
-
jsonSerializer
public static <T> HttpSerializerDeserializer<T> jsonSerializer(SerializerDeserializer<T> serializer) Creates anHttpSerializerDeserializer
that targetsHttpHeaderValues.APPLICATION_JSON
.- Type Parameters:
T
- Type of object to serialize.- Parameters:
serializer
- Used to serialize eachHttpSerializers
.- Returns:
HttpSerializerDeserializer
that targetsHttpHeaderValues.APPLICATION_JSON
.
-
jsonStreamingSerializer
public static <T> HttpStreamingSerializerDeserializer<T> jsonStreamingSerializer(StreamingSerializerDeserializer<T> serializer) Creates anHttpStreamingSerializerDeserializer
that targetsHttpHeaderValues.APPLICATION_JSON
.- Type Parameters:
T
- Type of object to serialize.- Parameters:
serializer
- Used to serialize eachHttpSerializers
.- Returns:
HttpStreamingSerializerDeserializer
that targetsHttpHeaderValues.APPLICATION_JSON
.
-
serializer
public static <T> HttpSerializerDeserializer<T> serializer(SerializerDeserializer<T> serializer, Consumer<HttpHeaders> headersSerializeConsumer, Predicate<HttpHeaders> headersDeserializePredicate) Creates anHttpSerializerDeserializer
that usesSerializerDeserializer
for serialization.- Type Parameters:
T
- Type of object to serialize.- Parameters:
serializer
- Used to serialize eachHttpSerializers
.headersSerializeConsumer
- Sets the headers to indicate the appropriate encoding and content type.headersDeserializePredicate
- Validates the headers are of the supported encoding and content type.- Returns:
HttpSerializerDeserializer
that uses aSerializerDeserializer
for serialization.
-
streamingSerializer
public static <T> HttpStreamingSerializerDeserializer<T> streamingSerializer(StreamingSerializerDeserializer<T> serializer, Consumer<HttpHeaders> headersSerializeConsumer, Predicate<HttpHeaders> headersDeserializePredicate) Creates anHttpStreamingSerializerDeserializer
that usesStreamingSerializerDeserializer
for serialization.- Type Parameters:
T
- Type of object to serialize.- Parameters:
serializer
- Used to serialize eachHttpSerializers
.headersSerializeConsumer
- Sets the headers to indicate the appropriate encoding and content type.headersDeserializePredicate
- Validates the headers are of the supported encoding and content type.- Returns:
HttpStreamingSerializerDeserializer
that uses aStreamingSerializerDeserializer
for serialization.
-
streamingSerializer
public static <T> HttpStreamingSerializer<T> streamingSerializer(Serializer<T> serializer, ToIntFunction<T> bytesEstimator, Consumer<HttpHeaders> headersSerializeConsumer) Create aHttpStreamingSerializer
that uses aSerializer
for serialization. This method is useful if the payload body is provided in non-Buffer
type and theHttpHeaderNames.CONTENT_TYPE
is known a-priori (e.g. streaming raw json data from a stream ofString
s). Deserialization should be done using the a-priori knowledge to use a compatibleHttpStreamingDeserializer
.- Type Parameters:
T
- Type of object to serialize.- Parameters:
serializer
- Used to serialize eachHttpSerializers
chunk.bytesEstimator
- Provides an estimate of how many bytes to allocate for eachBuffer
to serialize to.headersSerializeConsumer
- Sets the headers to indicate the appropriate encoding and content type.- Returns:
- a
HttpStreamingSerializer
that uses aSerializer
for serialization.
-