1

this is a simple code that I'm using to create a JSON but i want it to ignore some fields just here. so i don't want to use @JsonIgnore annotation. but the problem is withoutAttribute doesn't work and all of the fields remain in JSON string. and i have another question. how can i format value of some fields with objectMapper. for example format the value of username field to **8t on changing to JSON without any annotation.

TestObject testObject = new TestObject("858t","444hg", "mina");
ObjectWriter ow = new   ObjectMapper().writer().withDefaultPrettyPrinter().withoutAttribute("username").withoutAttribute("password");
String s = ow.writeValueAsString(testObject);
System.out.println(s);

I expect output

{"name":"mina"} 

but i get below result

{
"username" : "858t",
"password" : "444hg",
"name" : "mina"
}

this is the maven dependency that I'm using. i also tried other versions like 2.8.7

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.4</version>
 </dependency> 
<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.8</version>
</dependency>

update: i used ExclusionStrategy of GSON library and it worked. but when i use it in server i have a wired heap space growth and just because of the bellow ExclusionStrategy used in my logger:

public class SecurityJsonExclusion implements ExclusionStrategy {

private Class<?> c;
ArrayList<String> fieldNames = new ArrayList<String>(Arrays.asList("username","password"));

public SecurityJsonExclusion(String... fieldNames) throws SecurityException {

    if (fieldNames != null) {
        Collections.addAll(this.fieldNames, fieldNames);
    }
}

public SecurityJsonExclusion() {

}
public boolean shouldSkipClass(Class<?> arg0) {
    return false;
}

public boolean shouldSkipField(FieldAttributes f) {

    if (fieldNames == null || fieldNames.size() == 0) {
        return false;
    } else {

        String name = f.getName();
        for (String s : fieldNames) {

            if (name.equalsIgnoreCase(s)) {
                return true;
            }
        }
        return false;

    }
}

}

using it like this:

  Gson gson = new GsonBuilder()
                .setExclusionStrategies(new SecurityJsonExclusion()).setPrettyPrinting()
                .create();
        return gson.toJson(o);

even when i remove setExclusion part, gsonBuilder itself consumes a large amount of heap space.

Mina Kh
  • 127
  • 1
  • 12
  • so you want to ignore the numbers ? – Ryuzaki L Jul 27 '19 at 12:03
  • no any string. i put numbers accidentally – Mina Kh Jul 27 '19 at 12:07
  • try this `ObjectWriter ow = new ObjectMapper().writer().withoutAttribute("username").withoutAttribute("password");` – Ryuzaki L Jul 27 '19 at 12:33
  • 2
    tried it. doesn't work. thank you – Mina Kh Jul 27 '19 at 12:42
  • You can write custom serialiser and it should help you to specify which fields to serialise and allow to format values if needed. Also take a look on [How do I exclude fields with Jackson not using annotations?](https://stackoverflow.com/questions/13764280/how-do-i-exclude-fields-with-jackson-not-using-annotations), [Ignore Serialize field based on another field value](https://stackoverflow.com/questions/54989412/ignore-serialize-field-based-on-another-field-value), – Michał Ziober Jul 27 '19 at 20:15
  • thanks for your help. but my original problem is, i don't know the other fields of my object and i just want to remove fields with specific names like username and password(turning object type to JSON). so the only way might be excluding it by name. – Mina Kh Jul 28 '19 at 04:35

0 Answers0