42BV / CSVeed

Light-weight, easy-to-use Java-based CSV utility
Apache License 2.0
100 stars 22 forks source link

Integer shouldn't be formatted by default on writing to CSV #83

Open subey opened 7 years ago

subey commented 7 years ago

Example: Bean: Integer has value 6666 Generated CSV: "6,666"

hazendaz commented 7 years ago

Can you provide a sample app showing this? I don't recall ever seeing that situation myself and it doesn't make sense.

subey commented 7 years ago
public class CsveedTest {

    public class Bean {
        private Integer first;
        private Long second;

        public Bean(Integer first, Long second) {
            this.first = first;
            this.second = second;
        }

        public Integer getFirst() {
            return first;
        }

        public void setFirst(Integer first) {
            this.first = first;
        }

        public Long getSecond() {
            return second;
        }

        public void setSecond(Long second) {
            this.second = second;
        }
    }

    @Test
    public void bugReason(){
        NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
        String value = numberFormat.format(6666);
        assertEquals("6,666", value);
    }
    @Test
    public void bugSolution(){
        NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
        numberFormat.setGroupingUsed(false);
        String value = numberFormat.format(6666);
        assertEquals("6666", value);
    }

    @Test
    public void testLocale() throws IOException {

        List<Bean> beans = new ArrayList<>();
        beans.add(new Bean(2222,6666L));

        StringWriter writer = new StringWriter();

        BeanWriter<Bean> beanWriter = new BeanWriterImpl<Bean>(writer, Bean.class);
        beanWriter.writeBeans(beans);
        writer.close();

        System.out.print(writer.getBuffer().toString());
        assertEquals("\"first\";\"second\"\r\n" +
                        "\"2222\";\"6666\"\r\n",
                writer.getBuffer().toString());

    }
}
hazendaz commented 7 years ago

@subey I agree this is a issue in some use cases. However, not the easiest to fix in this library without a lot of changes. The reason behind that is that the library allows and is preferenced on locals outside the US. And a lot of those locals have their own settings. While it appears this library can handle this on reading, the writting portion seems to ignore overriding it. The fact though that it quotes it sort of tells me the output while not necessarily liked is proper.

I'm going to leave this open as it is valid concern that eventually needs addressed. If you want to take a crack at trying to fix this library that would be great.

Otherwise can you confirm if this ultimately is causing any real significant issue currently other than look and feel.