org.phoebus.applications.alarm.messages.MessageParser Maven / Gradle / Ivy
The newest version!
package org.phoebus.applications.alarm.messages;
import java.util.Map;
import org.apache.kafka.common.errors.SerializationException;
import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serializer;
import org.phoebus.applications.alarm.model.EnabledState;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
/** XML Message parser */
public class MessageParser implements Serializer, Deserializer {
private ObjectMapper objectMapper = new ObjectMapper();
private Class tClass;
/**
* Create a message parser for type tClass
*
* @param tClass Class
*/
public MessageParser(Class tClass) {
this.tClass = tClass;
SimpleModule simple_module = new SimpleModule();
simple_module.addSerializer(EnabledState.class, new EnabledSerializer());
simple_module.addDeserializer(EnabledState.class, new EnabledDeserializer());
objectMapper.registerModule(simple_module);
}
@Override
public void close() {
// TODO Auto-generated method stub
}
@Override
public void configure(Map arg0, boolean arg1) {
}
@Override
public byte[] serialize(String topic, T message) {
if (message == null)
return null;
try {
return objectMapper.writeValueAsBytes(message);
} catch (Exception e) {
throw new SerializationException("Error serializing message ", e);
}
}
@Override
public T deserialize(String arg0, byte[] bytes) {
if (bytes == null)
return null;
T data;
try {
data = objectMapper.readValue(bytes, tClass);
} catch (Exception e) {
throw new SerializationException(e);
}
return data;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy