Skip to content

Tips & Tricks

Excluding a field dynamically

Usually, to exclude a field you’d use an @Exclude annotation, or, alternatively, write a custom schema, but in some situations the custom schema would get too cluttered or complex and you can’t add the annotation directly.

In this case, you should override the default AutoJSON instance:

import dev.drtheo.autojson.AutoJSON;
import java.lang.reflect.Field;
class MyAutoJSON extends AutoJSON {
@Override
public boolean shouldExclude(Field field, int layer) {
if (field.getName().equals("token"))
return true;
return super.shouldExclude(field, layer);
}
}

Override instantiation parameters dynamically

Again, sometimes, you can’t add the required annotation. For example, the @Instantiate annotation.

In this case you’d override the safeInstancing method:

import dev.drtheo.autojson.AutoJSON;
class MyAutoJSON extends AutoJSON {
@Override
public boolean safeInstancing(Class<?> type) {
if (type.getName().equals("java.util.ArrayList"))
return true;
return super.safeInstancing(type);
}
}

Increasing performance

If you use custom schemas, make sure you’re caching the schemas of non-built-in types you’re serializing.

All (de)serialization contexts support passing the type and the schema.