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 aHttpStreamingSerializerDeserializerthat serializesStrings withStandardCharsets.US_ASCIIencoding using fixedintlength delimited framing.Creates aHttpStreamingSerializerDeserializerthat serializesStrings withStandardCharsets.US_ASCIIencoding using variableintlength delimited framing.appSerializerFixLen(Charset charset) Creates aHttpStreamingSerializerDeserializerthat serializesStrings withcharsetencoding using fixedintlength delimited framing.Creates aHttpStreamingSerializerDeserializerthat serializesStrings withStandardCharsets.UTF_8encoding using fixedintlength delimited framing.Creates aHttpStreamingSerializerDeserializerthat serializesStrings withStandardCharsets.UTF_8encoding using variableintlength delimited framing.appSerializerVarLen(Charset charset) Creates aHttpStreamingSerializerDeserializerthat serializesStrings withcharsetencoding using fixedintlength delimited framing.static HttpStreamingSerializer<byte[]>bytesStreamingSerializer(Consumer<HttpHeaders> headersSerializeConsumer) Create aHttpStreamingSerializerthat serializesbyte[].static HttpSerializerDeserializer<Map<String,List<String>>> Get aHttpSerializerDeserializerthat can serialize a key-valuesMaps withStandardCharsets.UTF_8Charsetto urlencoded forms.static HttpSerializerDeserializer<Map<String,List<String>>> formUrlEncodedSerializer(Charset charset) Get aHttpSerializerDeserializerthat can serialize a key-valuesMaps with aCharsetto urlencoded forms.static <T> HttpSerializerDeserializer<T>jsonSerializer(SerializerDeserializer<T> serializer) Creates anHttpSerializerDeserializerthat targetsHttpHeaderValues.APPLICATION_JSON.static <T> HttpStreamingSerializerDeserializer<T>jsonStreamingSerializer(StreamingSerializerDeserializer<T> serializer) Creates anHttpStreamingSerializerDeserializerthat targetsHttpHeaderValues.APPLICATION_JSON.static <T> HttpSerializerDeserializer<T>serializer(SerializerDeserializer<T> serializer, Consumer<HttpHeaders> headersSerializeConsumer, Predicate<HttpHeaders> headersDeserializePredicate) Creates anHttpSerializerDeserializerthat usesSerializerDeserializerfor serialization.static <T> HttpStreamingSerializer<T>streamingSerializer(Serializer<T> serializer, ToIntFunction<T> bytesEstimator, Consumer<HttpHeaders> headersSerializeConsumer) Create aHttpStreamingSerializerthat uses aSerializerfor serialization.static <T> HttpStreamingSerializerDeserializer<T>streamingSerializer(StreamingSerializerDeserializer<T> serializer, Consumer<HttpHeaders> headersSerializeConsumer, Predicate<HttpHeaders> headersDeserializePredicate) Creates anHttpStreamingSerializerDeserializerthat usesStreamingSerializerDeserializerfor serialization.static HttpStreamingSerializer<String>stringStreamingSerializer(Charset charset, Consumer<HttpHeaders> headersSerializeConsumer) Create aHttpStreamingSerializerthat serializesString.static HttpSerializerDeserializer<String>textSerializer(Charset charset) static HttpSerializerDeserializer<String>static HttpSerializerDeserializer<String>
-
Method Details
-
formUrlEncodedSerializer
Get aHttpSerializerDeserializerthat can serialize a key-valuesMaps withStandardCharsets.UTF_8Charsetto urlencoded forms.- Returns:
HttpSerializerDeserializerthat could serialize key-valueMap.- See Also:
-
formUrlEncodedSerializer
public static HttpSerializerDeserializer<Map<String,List<String>>> formUrlEncodedSerializer(Charset charset) Get aHttpSerializerDeserializerthat can serialize a key-valuesMaps with aCharsetto urlencoded forms.- Parameters:
charset- TheCharsetto use for value encoding.- Returns:
HttpSerializerDeserializerthat could serialize key-valueMap.- See Also:
-
textSerializerUtf8
- Returns:
HttpSerializerDeserializerthat can serializeStrings.
-
textSerializerAscii
- Returns:
HttpSerializerDeserializerthat can serializeStrings.
-
textSerializer
- Parameters:
charset- TheCharsetto use for encoding.- Returns:
HttpSerializerDeserializerthat can serializeStrings.
-
appSerializerUtf8FixLen
Creates aHttpStreamingSerializerDeserializerthat serializesStrings withStandardCharsets.UTF_8encoding using fixedintlength delimited framing. The framing is required so the sameStringobjects 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_TYPEvalue prefix is "application/text-fix-int".- Returns:
- a
HttpStreamingSerializerDeserializerthat serializesStrings withStandardCharsets.UTF_8encoding using fixedintlength delimited framing. - See Also:
-
appSerializerUtf8VarLen
Creates aHttpStreamingSerializerDeserializerthat serializesStrings withStandardCharsets.UTF_8encoding using variableintlength delimited framing. The framing is required so the sameStringobjects 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_TYPEvalue prefix is "application/text-var-int".- Returns:
- a
HttpStreamingSerializerDeserializerthat serializesStrings withStandardCharsets.UTF_8encoding using variableintlength delimited framing. - See Also:
-
appSerializerAsciiFixLen
Creates aHttpStreamingSerializerDeserializerthat serializesStrings withStandardCharsets.US_ASCIIencoding using fixedintlength delimited framing. The framing is required so the sameStringobjects 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_TYPEvalue prefix is "application/text-fix-int".- Returns:
- a
HttpStreamingSerializerDeserializerthat serializesStrings withStandardCharsets.US_ASCIIencoding using fixedintlength delimited framing. - See Also:
-
appSerializerAsciiVarLen
Creates aHttpStreamingSerializerDeserializerthat serializesStrings withStandardCharsets.US_ASCIIencoding using variableintlength delimited framing. The framing is required so the sameStringobjects 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_TYPEvalue prefix is "application/text-var-int".- Returns:
- a
HttpStreamingSerializerDeserializerthat serializesStrings withStandardCharsets.US_ASCIIencoding using variableintlength delimited framing. - See Also:
-
appSerializerFixLen
Creates aHttpStreamingSerializerDeserializerthat serializesStrings withcharsetencoding using fixedintlength delimited framing. The framing is required so the sameStringobjects 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_TYPEvalue prefix is "application/text-fix-int".- Parameters:
charset- The character encoding to use for serialization.- Returns:
- a
HttpStreamingSerializerDeserializerthat serializesStrings withcharsetencoding using fixedintlength delimited framing. - See Also:
-
appSerializerVarLen
Creates aHttpStreamingSerializerDeserializerthat serializesStrings withcharsetencoding using fixedintlength delimited framing. The framing is required so the sameStringobjects 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_TYPEvalue prefix is "application/text-var-int".- Parameters:
charset- The character encoding to use for serialization.- Returns:
- a
HttpStreamingSerializerDeserializerthat serializesStrings withcharsetencoding using fixedintlength delimited framing. - See Also:
-
stringStreamingSerializer
public static HttpStreamingSerializer<String> stringStreamingSerializer(Charset charset, Consumer<HttpHeaders> headersSerializeConsumer) Create aHttpStreamingSerializerthat serializesString. This method is useful if the payload body is provided inStringand theHttpHeaderNames.CONTENT_TYPEis known a-priori (e.g. streaming raw json data from a stream ofStrings). 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
Stringobjects. 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 produceStrings 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
Stringencoding 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
HttpStreamingSerializerthat uses aSerializerfor 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 aHttpStreamingSerializerthat serializesbyte[]. This method is useful if the payload body is provided inbyte[]and theHttpHeaderNames.CONTENT_TYPEis 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
HttpStreamingSerializerthat uses aSerializerfor serialization.
-
jsonSerializer
public static <T> HttpSerializerDeserializer<T> jsonSerializer(SerializerDeserializer<T> serializer) Creates anHttpSerializerDeserializerthat targetsHttpHeaderValues.APPLICATION_JSON.- Type Parameters:
T- Type of object to serialize.- Parameters:
serializer- Used to serialize eachHttpSerializers.- Returns:
HttpSerializerDeserializerthat targetsHttpHeaderValues.APPLICATION_JSON.
-
jsonStreamingSerializer
public static <T> HttpStreamingSerializerDeserializer<T> jsonStreamingSerializer(StreamingSerializerDeserializer<T> serializer) Creates anHttpStreamingSerializerDeserializerthat targetsHttpHeaderValues.APPLICATION_JSON.- Type Parameters:
T- Type of object to serialize.- Parameters:
serializer- Used to serialize eachHttpSerializers.- Returns:
HttpStreamingSerializerDeserializerthat targetsHttpHeaderValues.APPLICATION_JSON.
-
serializer
public static <T> HttpSerializerDeserializer<T> serializer(SerializerDeserializer<T> serializer, Consumer<HttpHeaders> headersSerializeConsumer, Predicate<HttpHeaders> headersDeserializePredicate) Creates anHttpSerializerDeserializerthat usesSerializerDeserializerfor 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:
HttpSerializerDeserializerthat uses aSerializerDeserializerfor serialization.
-
streamingSerializer
public static <T> HttpStreamingSerializerDeserializer<T> streamingSerializer(StreamingSerializerDeserializer<T> serializer, Consumer<HttpHeaders> headersSerializeConsumer, Predicate<HttpHeaders> headersDeserializePredicate) Creates anHttpStreamingSerializerDeserializerthat usesStreamingSerializerDeserializerfor 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:
HttpStreamingSerializerDeserializerthat uses aStreamingSerializerDeserializerfor serialization.
-
streamingSerializer
public static <T> HttpStreamingSerializer<T> streamingSerializer(Serializer<T> serializer, ToIntFunction<T> bytesEstimator, Consumer<HttpHeaders> headersSerializeConsumer) Create aHttpStreamingSerializerthat uses aSerializerfor serialization. This method is useful if the payload body is provided in non-Buffertype and theHttpHeaderNames.CONTENT_TYPEis known a-priori (e.g. streaming raw json data from a stream ofStrings). 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 eachHttpSerializerschunk.bytesEstimator- Provides an estimate of how many bytes to allocate for eachBufferto serialize to.headersSerializeConsumer- Sets the headers to indicate the appropriate encoding and content type.- Returns:
- a
HttpStreamingSerializerthat uses aSerializerfor serialization.
-