Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Serialization of single Float or Double value leads to incorrect CSV when schema is used #87

Closed
sothmann opened this issue Jul 22, 2015 · 3 comments
Milestone

Comments

@sothmann
Copy link

If schema is created for Float or Double class (mapper.schemaFor(Float.class) or mapper.schemaFor(Double.class)) and used to write single floating point numbers

ObjectWriter writer = mapper.writer(schema);
String csv = writer.writeValueAsString(42.33f); // or 42.33d

the resultung CSV is 42.33, instead of 42.33 so an additional column separator is added at the end of the line.
For all other primitive data types, this works correctly.
Additionally, if writer is created with ObjectWriter writer = mapper.writerFor(Float.class); without explicit schema use, this works correctly (no column separator is added).

Here is a full JUnit test case for the problem:

public class CsvPrimitiveSerializationTest {

  @Test
  public void testSerializationOfPrimitivesToCsv() throws JsonProcessingException {
    CsvMapper mapper = new CsvMapper();
    testSerializationOfPrimitiveToCsv(mapper, String.class, "hello world", "\"hello world\"\n");
    testSerializationOfPrimitiveToCsv(mapper, Boolean.class, true, "true\n");
    testSerializationOfPrimitiveToCsv(mapper, Integer.class, 42, "42\n");
    testSerializationOfPrimitiveToCsv(mapper, Long.class, 42L, "42\n");
    testSerializationOfPrimitiveToCsv(mapper, Short.class, (short)42, "42\n");
    testSerializationOfPrimitiveToCsv(mapper, Float.class, 42.33f, "42.33\n");
    testSerializationOfPrimitiveToCsv(mapper, Double.class, 42.33d, "42.33\n");
  }

  private <T> void testSerializationOfPrimitiveToCsv(final CsvMapper mapper, final Class<T> type, final T value, final String expectedCsv) throws JsonProcessingException {
    CsvSchema schema = mapper.schemaFor(type);
    ObjectWriter writer = mapper.writer(schema);
    String csv = writer.writeValueAsString(value);
    assertEquals(expectedCsv, csv);
  }
}

In this test case, only the last two tests for Float and Double fail.

@cowtowncoder
Copy link
Member

Thank you for reporting this.

Interesting. Not sure why this would occur, but is a bug obviously. :)

@cowtowncoder
Copy link
Member

Ok. One problem with primitives, CsvSchema really is that latter is not designed for non-POJO types.
And come to think to that, I am not completely sure if and how they should be handled. Maybe they should not be serializable at all? But if they are, there would need to be some sort of pseudo-schema defined, perhaps a bogus column or something.

So: I don't know if this is a straight bug, as much as unsupported use case, at this point.

@cowtowncoder
Copy link
Member

Ok, regardless, I managed to patch things to avoid interpreting primitives/wrappers as POJOs, and this makes given test pass. So while I am not 100% sure if handling works and makes sense, at least this should be an improvement.

@cowtowncoder cowtowncoder added this to the 2.3.0 milestone Aug 9, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants