8 Ocak 2020 Çarşamba

XStream API

Converter Arayüzü
Şu satırı dahil ederiz.
import com.thoughtworks.xstream.converters.Converter;
unmarshal metodu
Şöyle yaparız
public class CustomerConverter implements Converter {

  DateTimeFormatter DT_FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
  
  @Override
  public boolean canConvert(Class type) {
    return type.equals(Customer.class);
  }
 
  @Override
  public void marshal(Object source, HierarchicalStreamWriter writer,
    MarshallingContext context) {
    // Don't do anything
  }

  @Override
  public Object unmarshal(HierarchicalStreamReader reader,
    UnmarshallingContext context) {
    reader.moveDown();

    Customer customer = new Customer();
    customer.setId(Long.valueOf(reader.getValue()));

    reader.moveUp();
    reader.moveDown();
    customer.setFirstName(reader.getValue());

    reader.moveUp();
    reader.moveDown();
    customer.setLastName(reader.getValue());

    reader.moveUp();
    reader.moveDown();
    customer.setBirthdate(LocalDateTime.parse(reader.getValue(), DT_FORMATTER));

    return customer;

  }
}
Xstream Sınıfı
XStream Sınıfı yazısına taşıdım.

XStreamAlias Anotasyonu 
Örnek
Şöyle yaparız.
@XStreamAlias("Foo")
class MyClass {
  ...
}


Hiç yorum yok:

Yorum Gönder