You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since there's only static filters, passing the current context to JsonInclude JsonIncludeOptions.valueFilter and JsonIncludeOptions.contentFilter would allow to recreate almost the same as the Java Jackson PropertyFilter. Currently, the only parameter passed to valueFilter and contentFilter is the value of the property to be inspected. This only allows simple validations like null or empty value detection and or global constants comparison. Having the context object as a parameter would allow real dynamic filtering.
Current Code
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;
}
Suggested update
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.
Having all core methods set to private makes any little overrides impossible. Being able to simply extend the JsonStringifier class to override few methods would have been an easy solution to easily implement dynamic filtering.
The text was updated successfully, but these errors were encountered:
Environment
Node.js 14.14.33
Description
Since there's only static filters, passing the current context to JsonInclude
JsonIncludeOptions.valueFilter
andJsonIncludeOptions.contentFilter
would allow to recreate almost the same as the Java JacksonPropertyFilter
. Currently, the only parameter passed tovalueFilter
andcontentFilter
is the value of the property to be inspected. This only allows simple validations like null or empty value detection and or global constants comparison. Having the context object as a parameter would allow real dynamic filtering.Current Code
Suggested update
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:For now, it seems impossible to filter a property based on a context value or on a custom validation against other instance properties.
Having all core methods set to
private
makes any little overrides impossible. Being able to simply extend the JsonStringifier class to override few methods would have been an easy solution to easily implement dynamic filtering.The text was updated successfully, but these errors were encountered: