private stringifyJsonInclude(obj: any, key: string, context: JsonStringifierTransformerContext): boolean {
const currentMainCreator = context.mainCreator[0];
let jsonInclude: JsonIncludeOptions =
getMetadata('JsonInclude', currentMainCreator, key, context);
if (!jsonInclude) {
jsonInclude = getMetadata('JsonInclude', currentMainCreator, null, context);
jsonInclude = jsonInclude ? jsonInclude : context.features.serialization.DEFAULT_PROPERTY_INCLUSION;
}
if (jsonInclude) {
const value = obj[key];
switch (jsonInclude.value) {
case JsonIncludeType.NON_EMPTY:
return !isValueEmpty(value);
case JsonIncludeType.NON_NULL:
return value != null;
case JsonIncludeType.NON_DEFAULT:
return value !== getDefaultValue(value) && !isValueEmpty(value);
case JsonIncludeType.CUSTOM:
return !jsonInclude.valueFilter(value);
}
}
return true;
}
private stringifyJsonInclude(obj: any, key: string, context: JsonStringifierTransformerContext): boolean {
const currentMainCreator = context.mainCreator[0];
let jsonInclude: JsonIncludeOptions =
getMetadata('JsonInclude', currentMainCreator, key, context);
if (!jsonInclude) {
jsonInclude = getMetadata('JsonInclude', currentMainCreator, null, context);
jsonInclude = jsonInclude ? jsonInclude : context.features.serialization.DEFAULT_PROPERTY_INCLUSION;
}
if (jsonInclude) {
const value = obj[key];
switch (jsonInclude.value) {
case JsonIncludeType.NON_EMPTY:
return !isValueEmpty(value);
case JsonIncludeType.NON_NULL:
return value != null;
case JsonIncludeType.NON_DEFAULT:
return value !== getDefaultValue(value) && !isValueEmpty(value);
case JsonIncludeType.CUSTOM:
return !jsonInclude.valueFilter(value, context);
}
}
return true;
}
Another way to allow dynamic filtering would be to add a JsonFilterType.CUSTOM and a new JsonFilter property to specify a method, just like JsonIncludeOptions.valueFilter
. This would mimic the Java filter implementation:
SimpleFilterProvider filters = new SimpleFilterProvider();
filters.addFilter("myFilter", MyClass.staticFilterMethod());
For now, it seems impossible to filter a property based on a context value or on a custom validation against other instance properties.