FasterXML / FasterXML/jackson-dataformats-text
`@JsonIgnoreProperties` ignores value but not the resulting CSV column
- 主要言語
- Java
- スター
- 455
- フォーク
- 164
- 平均マージ
- 6日 10時間
- マージ済み PR(30日)
- 2
説明
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
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
評価
この issue はまだ評価されていません。