This repository was archived by the owner on Jan 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Serialization of single Float or Double value leads to incorrect CSV when schema is used #87
Copy link
Copy link
Closed
Milestone
Description
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.
Metadata
Metadata
Assignees
Labels
No labels