2

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.

2 Answers 2

0

You can use Java's reflection mechanism.

public SensorDataDTO parseAndMapSensorData() {
    SensorDataDTO sensorDataDTO = new SensorDataDTO();
    Map<String, Object> mapMessage = new HashMap<>();
    mapMessage.put("serial", "AA12");
    mapMessage.put("moisture", 3.14);
    mapMessage.put("humidity", 75);

    //conver map to javabean
    Field[] f = sensorDataDTO.getClass().getDeclaredFields();
    for (int i = 0; i < f.length; i++) {
        f[i].setAccessible(true);
        try {
            Object value = mapMessage.get(f[i].getName());
            f[i].set(sensorDataDTO, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return sensorDataDTO;
}
0

I prefer this algorithm, because it doesn't have any redundant loops.

SensorDataDTO parseAndMapSensorData(String rawMessage) {
        if (rawMessage.contains(";")) {
            SensorDataDTO sensorDataDTO = new SensorDataDTO();
            Map<String, String> mapMessage = Splitter.on(';')
                .omitEmptyStrings()
                .trimResults()
                .withKeyValueSeparator(
                    Splitter.on('=')
                        .limit(2)
                        .trimResults())
                .split(rawMessage);
            sensorDataDTO.setSerial(parse(mapMessage, serial));
            sensorDataDTO.setMoisture(parseInt(parse(mapMessage, secondKey)));
            sensorDataDTO.setHumidity(parseDouble(parse(mapMessage, thirdKey)));
            return sensorDataDTO;
        }
        return null;
    }
  public static String parse(Map<String, String> mapMessage, String key) {
        try {
            return String.valueOf(mapMessage.get(key));
        } catch (Exception e) {
            return null;
        }
    }
@Getter
@Setter
public class SensorDataDTO {
    private String serial;
    private Double moisture;
    private Integer humidity;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.