4

JsonIgnore annotation doesn't seem to work for me. Any ideas why?

public class JsonTest implements Serializable {

    @JsonIgnore
private static JsonTest instance = null;

@JsonIgnore
private transient Set<String> set = new CopyOnWriteArraySet<String>();

private JsonTest() {}

@JsonIgnore
public static JsonTest getInstance() {
    if (instance == null)
        instance = new JsonTest();
    return instance;
}

public void setSet(Set<String> set) {
    this.set = set;
}

@JsonIgnore
public Set<String> getSet() {
    return set;
}

public String toString() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
    try {
        return mapper.writeValueAsString(this);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

public static void main(String[] args) {
    HashSet<String> set = new HashSet<String>();
    set.add("test");
    JsonTest.getInstance().setSet(set);
    System.out.println(JsonTest.getInstance().toString());
}

}

Output: {"set":["test"]}

kaya3
  • 47,440
  • 4
  • 68
  • 97
hansi
  • 2,278
  • 6
  • 34
  • 42
  • I don't recall for certain, but is setSet() exposing it? I know it seems odd that a setter exposing something for serialization. – Programmer Bruce Feb 02 '13 at 18:03
  • 5
    Which Jackson version? Also: make sure that version of Jackson mapper (which has `ObjectMapper`) and version of annotations jar is same (1.x for both, or 2.x for both) -- if those are mixed up, annotations are not recognized. – StaxMan Feb 03 '13 at 22:04
  • Thanks StaxMan, I had two different versions of annotation and objectmapper. – hansi Feb 04 '13 at 05:17
  • 2
    @StaxMan - I want to add a static utility method inside a jackson annotated POJO. It works fine, but do I need to add any annotations such as "@JsonIgnore" to my static method ? – MasterJoe Aug 19 '18 at 20:58

1 Answers1

2

Transient means that that field will not be serialized. You do not need to add @JsonIgnore annotation there because of that field will be excluded anyway.

You can locate @JsonIgnore annotation at least in org.codehaus.jackson:jackson-mapper-asl:1.9.13 and com.fasterxml.jackson.core:jackson-annotations:2.4.3 (this is what I used). Where ObjectMapper is in jackson-mapper-asl artifact. The interesting part here is that if I use @JsonIgnore from jackson-annotations (com.fasterxml.jackson.annotation.JsonIgnore) -- it doesn't work ('set' is in response) even if I configure ObjectMapper to use only properties. Probably it is a bug in fasterxml implementation but I didn't find it.

So, it is working fine if you will use codehaus rather then fasterxml (I added configuration to use only fields):

import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

public class JsonTest implements Serializable {
    @JsonIgnore
    private static JsonTest instance = null;

    private transient Set<String> set = new CopyOnWriteArraySet<String>();

    private JsonTest() {}

    @JsonIgnore
    public static JsonTest getInstance() {
        if (instance == null)
            instance = new JsonTest();
        return instance;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    @JsonIgnore
    public Set<String> getSet() {
        return set;
    }

    public String toString() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
        mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
        try {
            return mapper.writeValueAsString(this);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        HashSet<String> set = new HashSet<String>();
        set.add("test");
        JsonTest.getInstance().setSet(set);
        System.out.println(JsonTest.getInstance().toString());
    }
}
Ilya Ovesnov
  • 4,079
  • 1
  • 27
  • 31