/src/main/java/com/alibaba/fastjson/serializer/SimplePropertyPreFilter.java

https://bitbucket.org/xiejuntao/xdesktop · Java · 58 lines · 44 code · 14 blank · 0 comment · 12 complexity · 102819f9f44b5e75ed611a4404e82ed0 MD5 · raw file

  1. package com.alibaba.fastjson.serializer;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. public class SimplePropertyPreFilter implements PropertyPreFilter {
  5. private final Class<?> clazz;
  6. private final Set<String> includes = new HashSet<String>();
  7. private final Set<String> excludes = new HashSet<String>();
  8. public SimplePropertyPreFilter(String... properties){
  9. this(null, properties);
  10. }
  11. public SimplePropertyPreFilter(Class<?> clazz, String... properties){
  12. super();
  13. this.clazz = clazz;
  14. for (String item : properties) {
  15. if (item != null) {
  16. this.includes.add(item);
  17. }
  18. }
  19. }
  20. public Class<?> getClazz() {
  21. return clazz;
  22. }
  23. public Set<String> getIncludes() {
  24. return includes;
  25. }
  26. public Set<String> getExcludes() {
  27. return excludes;
  28. }
  29. public boolean apply(JSONSerializer serializer, Object source, String name) {
  30. if (source == null) {
  31. return true;
  32. }
  33. if (clazz != null && !clazz.isInstance(source)) {
  34. return true;
  35. }
  36. if (this.excludes.contains(name)) {
  37. return false;
  38. }
  39. if (includes.size() == 0 || includes.contains(name)) {
  40. return true;
  41. }
  42. return false;
  43. }
  44. }