Convert java.time.Ser to java.time.Localdate

Asked

Viewed 326 times

2

Hello.,

I’m reading Caelum’s booklet about java tests jsf web services design Patterns.

The problem is that I can’t read the date (I replace the Calendar for LocalDate in class Negociacao) for having these tags(byte and int) that appear when I use xml.

And makes that mistake:

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

I tried to read how this shows website, but makes that mistake:

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 you read to read the 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 class method 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);
}
  • 2

    It seems the code of convert that you picked up on that forum just won’t work because it tries to parse directly from a String. You would need a convertmore complex to deal with this case, but unfortunately I don’t have time to get into it. However, an alternative would simply represent the date using a textual format, such as 2015-12-30 and create a convert to always work in this format.`

1 answer

1


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

I changed my class Negociacao, in place 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 reply of 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 the xml was like this:

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

It took me a while, but I got it done.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.