BigDecimal值反序列化的问题

时间:2014-06-25 04:18:43

标签: java csv serialization deserialization

我正在使用jsefa 0.9.2对CSV数据进行反序列化。在此过程中,我遇到了BigeDecimal数据的问题。 当你在数字之间传递信件时。反序列化只是忽略字母后的值并返回数值不会引发任何错误。我期望不兼容错误。有什么建议来解决这个问题吗?代码示例如下。

FoEg" 123.3q21"变成123.3

 org.jsefa  jsefa  0.9.2

    package com.jsefa;

    import java.io.Serializable;
    import java.math.BigDecimal;
    import java.util.Date;

    import org.jsefa.common.validator.BigDecimalValidator;
    import org.jsefa.csv.annotation.CsvDataType;
    import org.jsefa.csv.annotation.CsvField;

    @SuppressWarnings("serial")
    @CsvDataType()
    public class Person implements Serializable {

        public static final String csvHeading = "Name,BirthDate,Weight";
        @CsvField(pos = 1)
        String name;

        @CsvField(pos = 2, format = "dd.MM.yyyy")
        Date birthDate;

        @CsvField(pos = 3, validatorType = BigDecimalValidator.class)
        BigDecimal weight;

        public Person() {

        }

        public Person(String name, Date birthDate, BigDecimal weight) {
            this.name = name;
            this.birthDate = birthDate;
            this.weight = weight;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Date getBirthDate() {
            return birthDate;
        }

        public void setBirthDate(Date birthDate) {
            this.birthDate = birthDate;
        }

        public BigDecimal getWeight() {
            return weight;
        }

        public void setWeight(BigDecimal weight) {
            this.weight = weight;
        }

        @Override
        public String toString() {
            return "Person [name=" + name + ", birthDate=" + birthDate + ", weight=" + weight + "]";
        }
    }




package com.jsefa;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.jsefa.csv.CsvDeserializer;
import org.jsefa.csv.CsvIOFactory;
import org.jsefa.csv.config.CsvConfiguration;

public class CSVDeSerTest {

    public CSVDeSerTest() {
    }

    public static void main(String[] args) {
        System.out.println("*********** CsvDeserializer *****************");
        String p = "Person,12.06.2014,123.3q21";
        List<Person> pList = deserializeSites(p.getBytes());
        System.out.println("Person:" + '\n' + pList);
    }

    private static List<Person> deserializeSites(byte[] buffer) {
        List<Person> returnList = new ArrayList<Person>();
        CsvConfiguration csvConfig = new CsvConfiguration();
        csvConfig.setFieldDelimiter(',');
        csvConfig.setLineBreak("\n");
        csvConfig.setQuoteCharacter('\"');

        CsvDeserializer deSerializer = (CsvDeserializer) CsvIOFactory.createFactory(csvConfig, Person.class).createDeserializer();
        String persons[] = new String(buffer).split("\n");
        for (int i = 0; i < persons.length; i++) {
            StringReader reader = new StringReader(persons[i] + "\n");
            System.out.println("ReaderValue:" + persons[i]);
            deSerializer.open(reader);
            if (deSerializer.hasNext()) {
                Person p = deSerializer.next();
                System.out.println(p);
                returnList.add(p);
            }
            deSerializer.close(true);
        }
        return returnList;
    }
}

1 个答案:

答案 0 :(得分:1)

您所描述的是Java的NumberFormat及相关类的已知行为。

通常,如果您尝试将String解析为Number,并且String包含有效数字格式后跟垃圾,则解析将成功,将使用有效部分,并且将忽略垃圾。< / p>

我不熟悉jsefa,但我认为这是一个很好的选择,它使用内置数字解析,例如用于解析数字的BigDecimal(char [])构造函数。所以,我猜你必须对一些jsefa类进行子类化,并添加你想要的格式检查。