You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
Hmmh. I can't say for sure, since there are two different aspects.
First is that CSV being columnar data format has to retain some information across values (for example). So features that use dynamic filtering (like not @JsonInclude) can be used to block value, but not column (i.e. must leave empty placeholder). So for those cases, this would be expected.
However: when ignoral is static (that is, based only on type), the whole property should be removed, and as a result, column too.
This is more what we have here.
So it seems to me that behavior is not expected. Problem may be implementation, as ignoral is on property for unwrapped value, and thereby base type (class) definition does contain property, and it is only missing via reference through property annotated with ignorable. It is possible this case is not currently supported.
For now I will consider this a bug that we hope to address. Eventually.
@cowtowncoder Hello, is there any solution to this problem yet? I'm having the same issue when trying to ignore a column in a CSV when using @JsonUnwrapped
@luismospinam@JsonUnwrapped and ignorals do not work very well together in general unfortunately, even in JSON. As to solution, no, I update issues with comments when there is related work.
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:
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
The text was updated successfully, but these errors were encountered: