FasterXML / FasterXML/jackson-dataformats-text

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

Open
#77 3 comments 0 reactions 0 assignees View on GitHub
csv
Dominant language
Java
Stars
455
Forks
164
Avg merge
6d 10h
Merged PRs (30d)
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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.