FasterXML / FasterXML/jackson-dataformats-text

`@JsonIgnoreProperties` ignores value but not the resulting CSV column

Ouverte
#77 3 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
csv
Langage dominant
Java
Étoiles
455
Forks
164
Merge moyen
6 j 10 h
PR mergées (30 j)
2

Description

Having a POJO with a sub-POJO as field, I want to ignore a specific field of this sub-POJO. `JsonIgnoreProperties` is useful ignoring the value but column for field remains.

Here's a full code example:

import static com.google.common.truth.Truth.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SequenceWriter;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

public class FooTest {

class Foo {
String something = "value";
@JsonUnwrapped(prefix = "bar.")
@JsonIgnoreProperties({ "toBeIgnored" })
Bar bar;

public String getSomething() {
return something;
}

public Bar getBar() {
return bar;
}
}

// In my actual scenario, I cannot edit this piece of code
class Bar {
String toBeIgnored;

public String getToBeIgnored() {
return toBeIgnored;
}
}

@Test
public void fooTest() throws IOException {
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(Foo.class).withHeader();
ObjectWriter csvWriter = mapper.writer(schema);
OutputStream outputStream = new ByteArrayOutputStream();
SequenceWriter writer = csvWriter.writeValues(outputStream);

Foo foo = new Foo();
foo.bar = new Bar();
String toBeIgnored = "should not be parsed";
foo.bar.toBeIgnored = toBeIgnored;
writer.write(foo);
String result = outputStream.toString();

assertThat(result).doesNotContain(toBeIgnored);
assertThat(result).contains("something");
assertThat(result).doesNotContain("bar.toBeIgnored");
}
}

Note that this is a very simplified example. In my actual scenario, I cannot edit class `Bar` in anyway.

Is there a way to achieve that without having to write a wrapper class around `Bar`? Is it a bug?

Thank you

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.