Browse Source

kafka-1797; (addressing Manikumar Reddy's comment) add the serializer/deserializer api to the new java client; patched by Jun Rao; reviewed by Manikumar Reddy and Neha Narkhede

pull/38/merge
Jun Rao 10 years ago
parent
commit
a93ef199b2
  1. 5
      clients/src/main/java/org/apache/kafka/common/serialization/StringDeserializer.java
  2. 5
      clients/src/main/java/org/apache/kafka/common/serialization/StringSerializer.java
  3. 4
      clients/src/test/java/org/apache/kafka/common/serialization/SerializationTest.java

5
clients/src/main/java/org/apache/kafka/common/serialization/StringDeserializer.java

@ -37,7 +37,10 @@ public class StringDeserializer implements Deserializer<String> {
@Override @Override
public String deserialize(String topic, byte[] data) { public String deserialize(String topic, byte[] data) {
try { try {
return new String(data, encoding); if (data == null)
return null;
else
return new String(data, encoding);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new SerializationException("Error when deserializing byte[] to string due to unsupported encoding " + encoding); throw new SerializationException("Error when deserializing byte[] to string due to unsupported encoding " + encoding);
} }

5
clients/src/main/java/org/apache/kafka/common/serialization/StringSerializer.java

@ -37,7 +37,10 @@ public class StringSerializer implements Serializer<String> {
@Override @Override
public byte[] serialize(String topic, String data) { public byte[] serialize(String topic, String data) {
try { try {
return data.getBytes(encoding); if (data == null)
return null;
else
return data.getBytes(encoding);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new SerializationException("Error when serializing string to byte[] due to unsupported encoding " + encoding); throw new SerializationException("Error when serializing string to byte[] due to unsupported encoding " + encoding);
} }

4
clients/src/test/java/org/apache/kafka/common/serialization/SerializationTest.java

@ -48,8 +48,10 @@ public class SerializationTest {
assertEquals("Should get the original string after serialization and deserialization with encoding " + encoding, assertEquals("Should get the original string after serialization and deserialization with encoding " + encoding,
str, deserializer.deserialize(mytopic, serializer.serialize(mytopic, str))); str, deserializer.deserialize(mytopic, serializer.serialize(mytopic, str)));
}
assertEquals("Should support null in serialization and deserialization with encoding " + encoding,
null, deserializer.deserialize(mytopic, serializer.serialize(mytopic, null)));
}
} }
private SerDeser<String> getStringSerDeser(String encoder) { private SerDeser<String> getStringSerDeser(String encoder) {

Loading…
Cancel
Save