If anyone know better algorithm to parse and map NOT JSON string to DTO instead of this?
@Getter
@Setter
public class SensorDataDTO {
private String serial;
private Double moisture;
private Integer humidity;
}
SensorDataDTO parseAndMapSensorData(String rawMessage) {
// bad message = "Sensor Connected!";
// or message can be like this "serial=AA12;moisture=3,14;";
// or "serial=AA12;moisture=3,14;humidity=75;";
// or completely different.
if (rawMessage.contains(";")) {
SensorDataDTO sensorDataDTO = new SensorDataDTO();
Map<String, String> mapMessage = Splitter.on(';')
.omitEmptyStrings()
.trimResults()
.withKeyValueSeparator(
Splitter.on('=')
.limit(2)
.trimResults())
.split(rawMessage);
for (Map.Entry<String, String> entry : mapMessage.entrySet()) {
if (serial.equals(entry.getKey()))
sensorDataDTO.setSerial(entry.getValue());
if (secondKey.equals(entry.getKey()))
sensorDataDTO.setMoisture(Double.parseDouble(entry.getValue()));
if (thirdKey.equals(entry.getKey()))
sensorDataDTO.setHumidity(Integer.parseInt(entry.getValue()));
}
return sensorDataDTO;
}
return null;
}
I appreciate all kind of info but not interesting to add some specific libraries or dependencies for this situation.