Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context parameter to JsonInclude valueFilter and contentFilter #27

Open
carlduranleau opened this issue Apr 12, 2021 · 0 comments
Open

Comments

@carlduranleau
Copy link

carlduranleau commented Apr 12, 2021

Environment

Node.js 14.14.33

Description

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant