Convert java.time.Ser to java.time.LocalDate

2

Hello,

I am reading Caelum's handbook on java testing jsf web services design patterns .

The problem is that I can not read the date (I replace Calendar with LocalDate in class Negociacao ) by having those tags ( byte and int ) that appear when I generate xml .

And give this error:

com.thoughtworks.xstream.converters.ConversionException: Cannot convert type java.time.Ser to type java.time.LocalDate

I tried to read this site , but it gives this error:

com.thoughtworks.xstream.converters.ConversionException: Text '' could not be parsed at index 0 : Text '' could not be parsed at index 0

This is the xml:

<negociacao>
    <preco>42.3</preco>
    <quantidade>100</quantidade>
    <data resolves-to=\"java.time.Ser\">
        <byte>3</byte>
        <int>2015</int>
        <byte>11</byte>
        <byte>8</byte>
    </data>
</negociacao>

This is the class that reads to read xml:

import java.io.InputStream;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import br.com.caelum.argentum.modelo.Negociacao;
import br.com.caelum.argentum.util.LocalDateConverter;

public class LeitorXML {

    public List<Negociacao> carrega(InputStream inputStream){

        if(inputStream == null){
            throw new NullPointerException("a lista esta vazia");
        }

        LocalDateConverter conv = new LocalDateConverter();
        XStream stream = new XStream(new DomDriver());
        stream.registerConverter(conv);
        stream.alias("negociacao", Negociacao.class);
        return (List<Negociacao>) stream.fromXML(inputStream);
    }
}

This is the method of class LeitorXMLTest :

@Test
public void carregaXmlComUmaNegociacaoEmListaUnitaria() {
    String xmlDeTeste = "<list>" +
              "<negociacao>" +
                  "<preco>43.5</preco>" +
                  "<quantidade>1000</quantidade>" +
                  "<data resolves-to=\"java.time.Ser\">" +
                      "<byte>3</byte>" +
                      "<int>2015</int>" +
                      "<byte>11</byte>" +
                      "<byte>8</byte>" +
                  "</data>" +
                  "</negociacao>" +
              "</list>";

    LeitorXML leitor = new LeitorXML();
    InputStream xml = new ByteArrayInputStream(xmlDeTeste.getBytes());

    List<Negociacao> negociacoes = leitor.carrega(xml);

    for (Negociacao negociacao : negociacoes) {
        System.out.println(negociacao.getData());
    }

    assertEquals(1, negociacoes.size());
    assertEquals(43.5, negociacoes.get(0).getPreco(), 0.00001);
    assertEquals(1000, negociacoes.get(0).getQuantidade(), 0.00001);
}
    
asked by anonymous 09.11.2015 / 01:49

1 answer

1

As utluiz said in the comment, I would have to represent the date in textual format and so I did.

I changed the class Negociacao , instead of LocalDate another class that has a String :

import java.time.LocalDate;

public class Data {

    private String datastr;

    public Data(LocalDate data){
        this.datastr = data.toString();
    }

    public Data(String datastr){
        this.datastr = datastr;
    }

    public LocalDate getData() {
        return LocalDate.parse(datastr);
    }

    public String getDatastr() {
        return datastr;
    }

}

I made another Converter , based on the response of this question :

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class LocalDateConverter implements Converter {


    @Override
    public boolean canConvert(Class type) {
        return AbstractMap.class.isAssignableFrom(type);
    }

    @Override
    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {

        AbstractMap map = (AbstractMap) source;

        for(Object obj : map.entrySet()){
            Entry entry = (Entry) obj;

            writer.startNode(entry.getKey().toString());
           writer.setValue(entry.getValue().toString());
           writer.endNode();
        }

    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        reader.moveDown();
        Data data = new Data(reader.getValue());
        reader.moveUp();

        return data;
    }
}

And xml looks like this:

<negociacao>
    <preco>42.3</preco>
    <quantidade>100</quantidade>
    <data>
        <datastr>2015-11-12</datastr>
    </data>
</negociacao>

It took a while, but I did.

    
12.11.2015 / 19:04