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

Commit

Permalink
Fix #87
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 9, 2015
1 parent c05026d commit 3f912af
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstractions.

<properties>
<jackson.version.annotations>2.6.0</jackson.version.annotations>
<jackson.version.core>2.6.0-rc4</jackson.version.core>
<jackson.version.core>2.6.1-SNAPSHOT</jackson.version.core>
<!-- Generate PackageVersion.java into this directory. -->
<packageVersion.dir>com/fasterxml/jackson/dataformat/csv</packageVersion.dir>
<packageVersion.package>${project.groupId}.csv</packageVersion.package>
Expand Down
5 changes: 5 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Project: jackson-dataformat-csv
=== Releases ===
------------------------------------------------------------------------

2.6.1 (not yet released)

#87: Serialization of single Float or Double value leads to incorrect CSV when schema is used
(reported by sothmann@github)

2.6.0 (20-Jul-2015)

#72: Recognize the configured "null value" (String) also in reader-infrastructure.
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,44 @@ protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<JavaType,CsvSchema> sch
return result;
}

protected boolean _nonPojoType(JavaType t)
{
if (t.isPrimitive() || t.isEnumType()) {
return true;
}
Class<?> raw = t.getRawClass();
// Wrapper types for numbers
if (Number.class.isAssignableFrom(raw)) {
if ((raw == Byte.class)
|| (raw == Short.class)
|| (raw == Character.class)
|| (raw == Integer.class)
|| (raw == Long.class)
|| (raw == Float.class)
|| (raw == Double.class)
) {
return true;
}
}
// Some other well-known non-POJO types
if ((raw == Boolean.class)
|| (raw == String.class)
) {
return true;
}
return false;
}

protected void _addSchemaProperties(CsvSchema.Builder builder, AnnotationIntrospector intr,
boolean typed,
JavaType pojoType, NameTransformer unwrapper)
{
// 09-Aug-2015, tatu: From [dataformat-csv#87], realized that one can not have
// real schemas for primitive/wrapper
if (_nonPojoType(pojoType)) {
return;
}

BeanDescription beanDesc = getSerializationConfig().introspect(pojoType);
for (BeanPropertyDefinition prop : beanDesc.findProperties()) {
// ignore setter-only properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ public String toString()
sb.append(']');
sb.append(", header? ").append(usesHeader());
sb.append(", skipFirst? ").append(skipsFirstDataRow());
sb.append(", comments?? ").append(allowsComments());
sb.append(", comments? ").append(allowsComments());

sb.append(']');
return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.csv.CsvFactory;
import com.fasterxml.jackson.dataformat.csv.CsvGenerator;
Expand Down Expand Up @@ -243,6 +244,31 @@ public void testRawWrites() throws Exception
assertEquals("a,b,foobar\n", w.toString());
}

// for [dataformat-csv#87]
public void testSerializationOfPrimitivesToCsv() throws Exception
{
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, Double.class, 42.33d, "42.33\n");
testSerializationOfPrimitiveToCsv(mapper, Float.class, 42.33f, "42.33\n");
}

private <T> void testSerializationOfPrimitiveToCsv(final CsvMapper mapper,
final Class<T> type, final T value, final String expectedCsv) throws Exception
{
CsvSchema schema = mapper.schemaFor(type);
System.err.println("Schema for "+type+" == "+schema);
ObjectWriter writer = mapper.writer(schema);
String csv = writer.writeValueAsString(value);
assertEquals(expectedCsv, csv);
}

/*
/**********************************************************************
/* Secondary test methods
Expand Down

0 comments on commit 3f912af

Please sign in to comment.