Skip to main content

confluent_kafka/common/serialization/
string_deserializer.rs

1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! String deserializer.
16//!
17//! String encoding defaults to UTF-8. Unlike the Java implementation which
18//! supports configurable charsets (via `key.deserializer.encoding` /
19//! `value.deserializer.encoding` / `deserializer.encoding`), this deserializer
20//! always decodes as UTF-8.
21//!
22//! Corresponds to Java's `org.apache.kafka.common.serialization.StringDeserializer`.
23
24use bytes::Bytes;
25
26use crate::common::Error;
27use crate::common::header::RecordHeaders;
28use crate::common::serialization::Deserializer;
29
30/// String encoding defaults to UTF8 and can be customized by setting the
31/// property key.deserializer.encoding, value.deserializer.encoding or
32/// deserializer.encoding. The first two take precedence over the last.
33///
34/// # Translation notes
35///
36/// WARNING: the encoding is always UTF-8; the properties are ignored.
37// Like Java's `new String(data, StandardCharsets.UTF_8)`, malformed byte
38// sequences become U+FFFD rather than failing: a lossy UTF-8 decode.
39#[derive(Clone, Debug, Default)]
40#[non_exhaustive]
41#[doc(alias = "org.apache.kafka.common.serialization.StringDeserializer")]
42pub struct StringDeserializer;
43
44impl StringDeserializer {
45    /// Create a new `StringDeserializer`.
46    #[doc(alias = "org.apache.kafka.common.serialization.StringDeserializer#StringDeserializer")]
47    pub fn new() -> Self {
48        Self
49    }
50}
51
52impl Deserializer<String> for StringDeserializer {
53    async fn deserialize(&self, _topic: &str, data: &[u8]) -> Result<String, Error> {
54        // Java's `new String(data, UTF_8)` replaces malformed sequences with
55        // U+FFFD; `from_utf8_lossy` reproduces that behavior exactly.
56        Ok(String::from_utf8_lossy(data).into_owned())
57    }
58
59    // Decodes the record's bytes in place, without first copying them out of the
60    // fetched buffer, as Java's override of
61    // `deserialize(String topic, Headers headers, ByteBuffer data)` decodes the
62    // buffer's backing array directly instead of going through the interface
63    // default's `Utils.toNullableArray` copy. The headers are ignored, as in Java.
64    async fn deserialize_with_headers_bytes(
65        &self,
66        topic: &str,
67        _headers: &mut RecordHeaders,
68        data: &Bytes,
69    ) -> Result<String, Error> {
70        self.deserialize(topic, &data[..]).await
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77    use crate::common::serialization::Serializer;
78    use crate::common::serialization::StringSerializer;
79
80    #[tokio::test]
81    async fn test_deserialize_string() {
82        let deserializer = StringDeserializer::new();
83        let result = deserializer.deserialize("topic", b"my string").await.unwrap();
84        assert_eq!(result, "my string".to_string());
85    }
86
87    #[tokio::test]
88    async fn test_deserialize_empty() {
89        let deserializer = StringDeserializer::new();
90        let result = deserializer.deserialize("topic", b"").await.unwrap();
91        assert_eq!(result, String::new());
92    }
93
94    #[tokio::test]
95    async fn test_deserialize_utf8() {
96        let deserializer = StringDeserializer::new();
97        let unicode_str = "\u{00e9}\u{00e8}\u{00ea}"; // accented chars
98        let result = deserializer.deserialize("topic", unicode_str.as_bytes()).await.unwrap();
99        assert_eq!(result, unicode_str.to_string());
100    }
101
102    #[tokio::test]
103    async fn test_deserialize_malformed_is_lossy() {
104        // Java's `new String(bytes, UTF_8)` replaces malformed bytes with
105        // U+FFFD instead of throwing; the Rust translation matches.
106        let deserializer = StringDeserializer::new();
107        let result = deserializer.deserialize("topic", &[0xff, 0xfe]).await.unwrap();
108        assert_eq!(result, "\u{fffd}\u{fffd}".to_string());
109    }
110
111    #[tokio::test]
112    async fn test_serialize_deserialize_round_trip() {
113        let serializer = StringSerializer::new();
114        let deserializer = StringDeserializer::new();
115        let original = "round trip \u{00e9}";
116        let bytes = Serializer::<str>::serialize(&serializer, "topic", Some(original))
117            .await
118            .unwrap()
119            .unwrap();
120        let decoded = deserializer.deserialize("topic", &bytes).await.unwrap();
121        assert_eq!(decoded, original.to_string());
122    }
123}