question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ignore property during deserialization but retain in serialization not working

See original GitHub issue

Using 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:closed
  • Created 11 years ago
  • Comments:26 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
vkrishna17commented, Apr 14, 2017

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.

0reactions
cowtowncodercommented, Apr 14, 2017

@vkrishna17 This is a closed issue to if have remaining issues it makes sense to file a new issue with specific remaining problem(s).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found