-
-
Notifications
You must be signed in to change notification settings - Fork 151
Closed
Description
When I use a Set (but I think a Collection) of objects with feature JsonGenerator.Feature.IGNORE_UNKNOWN
extra line separators are added.
I paste the code to reproduce it:
protected static class Person
{
private String name;
private String surname;
public Person()
{
}
public Person( String name, String surname )
{
this.name = name;
this.surname = surname;
}
public String getName()
{
return name;
}
public void setName( String name )
{
this.name = name;
}
public String getSurname()
{
return surname;
}
public void setSurname( String surname )
{
this.surname = surname;
}
}
protected static class MyClass
{
private Set<Person> people;
private String address;
private String phoneNumber;
public MyClass()
{
}
public Set<Person> getPeople()
{
return people;
}
public void setPeople( Set<Person> people )
{
this.people = people;
}
public String getAddress()
{
return address;
}
public void setAddress( String address )
{
this.address = address;
}
public String getPhoneNumber()
{
return phoneNumber;
}
public void setPhoneNumber( String phoneNumber )
{
this.phoneNumber = phoneNumber;
}
}
public void testEmbeddedObjects() throws Exception
{
CsvMapper mapper = mapperForCsv();
mapper.configure( JsonGenerator.Feature.IGNORE_UNKNOWN, true );
CsvSchema schema = CsvSchema.builder()
.addColumn( "people" ) // here I'm skipping phoneNumber so I need to use IGNORE_UNKNOWN feature
.addColumn( "address" )
.build()
.withHeader();
Person firstPerson = new Person( "Barbie", "Benton" );
Person secondPerson = new Person( "Veltto", "Virtanen" );
Set<Person> people = new HashSet<>();
people.add( firstPerson );
people.add( secondPerson );
MyClass myClass = new MyClass();
myClass.setPeople( people );
myClass.setAddress( "AAA" );
myClass.setPhoneNumber( "123" );
String result = mapper.writer( schema ).writeValueAsString( myClass );
int numberOfLines = result.split( "\n" ).length;
assertEquals( 2, numberOfLines ); // header and data (here fails with 3)
}
Version is 2.9.0
A workaround would be to write a custom serializer, but with a class with a lot of fields it was very hard to find which field has broken silently the csv.
Is there any way to use the toString rappresentation for cases like this one?
If you prefer I can open a PR with that code instead of paste it here.