Ignore property during deserialization but retain in serialization not working
See original GitHub issueUsing com.fasterxml.jackson.core:jackson-{core,annotations,databind} 2.1.0
, I think the code below to ignore a computed property (i.e., no field) during deserialization (property zip
), but include it in serialization used to work, but now it doesn’t. I also tried removing class level @JsonIgnoreProperties({"zip"})
and adding @JsonIgnore
along with @JsonProperty("zip")
but that didn’t work either.
The variant with a field still works: https://gist.github.com/2510887 .
@JsonIgnoreProperties({"zip"})
public static class Foo {
private final int bar;
public Foo(@JsonProperty("bar") final int bar) {
this.bar = bar;
}
public int getBar() {
return bar;
}
@JsonProperty("zip")
public int getZip() {
return bar * 2;
}
static <T> T fromJson(final String json, final Class<T> clazz) {
try {
return new ObjectMapper().readValue(json, clazz);
} catch (final IOException ioe) {
throw Throwables.propagate(ioe);
}
}
static String asJson(final Object obj) {
try {
return new ObjectMapper().writeValueAsString(obj);
} catch (final IOException ioe) {
throw Throwables.propagate(ioe);
}
}
}
@Test
public void testIgnoreDuringDeserializationButNotSerialization() {
final Foo foo = new Foo(1);
final String json = Foo.asJson(foo);
// json does not include zip computed property
LG.info("foo: {}", json);
final Foo reconstituted = Foo.fromJson(json, Foo.class);
}
Issue Analytics
- State:
- Created 11 years ago
- Comments:26 (9 by maintainers)
Top Results From Across the Web
Ignore property during deserialization but retain in ... - GitHub
What I'm seeing is @JsonIgnoreProperties({"zip"}) seems to cause property zip to not be required during deserialization (desired), but also ...
Read more >Ignoring property when deserializing - java - Stack Overflow
My problem is that I want to serialize the money property but ignore while deserializing it i.e., dont accept any values from the...
Read more >4 ways to make a property deserializable but not serializable ...
Approach 1: Use attributes JsonProperty together with JsonIgnore · We still use [JsonIgnore] attribute, adding it to the property which we do not ......
Read more >How to ignore properties with System.Text.Json | Microsoft Learn
To ignore individual properties, use the [JsonIgnore] attribute. The following example shows a type to serialize. It also shows the JSON ...
Read more >Jackson Ignore Properties on Marshalling - Baeldung
This is very useful when the Jackson defaults aren't enough and we need to control exactly what gets serialized to JSON — and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have tried
@JsonIgnoreProperties(value = "fieldName", allowGetters=true)
. This worked for me. But if I try@JsonProperty(access = JsonProperty.Access.READ_ONLY)
on getter method, didn’t work.@vkrishna17 This is a closed issue to if have remaining issues it makes sense to file a new issue with specific remaining problem(s).