Java 类com.alibaba.fastjson.serializer.PropertyPreFilter 实例源码

项目:GitHub    文件:PrePropertyFilterTest.java   
public void test_0() throws Exception {
    class VO {
        public int getId() { throw new RuntimeException(); }
    }

    PropertyPreFilter filter = new PropertyPreFilter () {
        public boolean apply(JSONSerializer serializer, Object source, String name) {
            return false;
        }
    };

    VO vo = new VO();

    String text = JSON.toJSONString(vo, filter);
    Assert.assertEquals("{}", text);
}
项目:boohee_v5.6    文件:JSON.java   
private static void setFilter(JSONSerializer serializer, SerializeFilter filter) {
    if (filter != null) {
        if (filter instanceof PropertyPreFilter) {
            serializer.getPropertyPreFilters().add((PropertyPreFilter) filter);
        }
        if (filter instanceof NameFilter) {
            serializer.getNameFilters().add((NameFilter) filter);
        }
        if (filter instanceof ValueFilter) {
            serializer.getValueFilters().add((ValueFilter) filter);
        }
        if (filter instanceof PropertyFilter) {
            serializer.getPropertyFilters().add((PropertyFilter) filter);
        }
        if (filter instanceof BeforeFilter) {
            serializer.getBeforeFilters().add((BeforeFilter) filter);
        }
        if (filter instanceof AfterFilter) {
            serializer.getAfterFilters().add((AfterFilter) filter);
        }
    }
}
项目:uavstack    文件:JSON.java   
private static void setFilter(JSONSerializer serializer, SerializeFilter filter) {
    if (filter == null) {
        return;
    }

    if (filter instanceof PropertyPreFilter) {
        serializer.getPropertyPreFilters().add((PropertyPreFilter) filter);
    }

    if (filter instanceof NameFilter) {
        serializer.getNameFilters().add((NameFilter) filter);
    }

    if (filter instanceof ValueFilter) {
        serializer.getValueFilters().add((ValueFilter) filter);
    }

    if (filter instanceof PropertyFilter) {
        serializer.getPropertyFilters().add((PropertyFilter) filter);
    }

    if (filter instanceof BeforeFilter) {
        serializer.getBeforeFilters().add((BeforeFilter) filter);
    }

    if (filter instanceof AfterFilter) {
        serializer.getAfterFilters().add((AfterFilter) filter);
    }
}
项目:java-platform    文件:FastjsonHttpMessageConverter.java   
@Override
protected void writeInternal(Object obj, Type type, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    Charset charset = getCharset(outputMessage.getHeaders());
    OutputStreamWriter out = new OutputStreamWriter(outputMessage.getBody(), charset);
    SerializeWriter writer = new SerializeWriter(out);
    JSONSerializer serializer = new JSONSerializer(writer, this.serializeConfig);
    serializer.config(SerializerFeature.DisableCircularReferenceDetect, true);

    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes instanceof ServletRequestAttributes) {
        Class<?> rootClass = filterClass(obj);
        if (rootClass != null) {
            String[] paths = ((ServletRequestAttributes) requestAttributes).getRequest()
                    .getParameterValues("paths");
            if (paths != null && paths.length > 0) {
                rootClass = ClassUtils.getUserClass(rootClass);
                Map<Class<?>, PropertyPreFilter> propertyFilters = new HashMap<>();
                pathVisit(propertyFilters, Sets.newHashSet(paths), rootClass);

                for (Entry<Class<?>, PropertyPreFilter> entry : propertyFilters.entrySet()) {
                    serializer.getPropertyPreFilters().add(entry.getValue());
                }
            } else {

                serializer.getPropertyFilters().add(DEFAULT_PROPERTY_FILTER);
            }
        } else {
            serializer.getPropertyFilters().add(DEFAULT_PROPERTY_FILTER);
        }
    }
    serializer.write(obj);
    writer.flush();
    out.close();
}
项目:GitHub    文件:PropertyPrefFilterTest_IntegerKey.java   
public void test_namefilter() throws Exception {
    PropertyPreFilter filter = new PropertyPreFilter() {

        public boolean apply(JSONSerializer serializer, Object object, String name) {
            return name.equals("1001");
        }

    };


    Map map = new HashMap();
    map.put(1001, 0);
    map.put(1002, 1);

    String text = JSON.toJSONString(map, filter);

    Assert.assertEquals("{1001:0}", text);
}
项目:java-platform    文件:FastjsonHttpMessageConverter.java   
private void pathVisit(final Map<Class<?>, PropertyPreFilter> propertyFilters, final Set<String> paths,
        Class<?> beanClass) {
    if (paths == null || beanClass == null) {
        return;
    }

    Set<String> revisePaths = new HashSet<>();
    Map<String, Set<String>> references = new HashMap<>();
    for (String path : paths) {
        if (StringUtils.contains(path, ".")) {
            String referenceProperty = StringUtils.substringBefore(path, ".");
            revisePaths.add(referenceProperty);

            Set<String> referencePaths = references.get(referenceProperty);
            if (referencePaths == null) {
                referencePaths = new HashSet<>();
                references.put(referenceProperty, referencePaths);
            }
            referencePaths.add(StringUtils.substringAfter(path, "."));
        } else {
            revisePaths.add(path);
        }
    }

    if (revisePaths.size() > 0) {
        PropertyPreFilter preFilter = propertyFilters.get(beanClass);
        if (preFilter != null) {
            if (preFilter instanceof IncludesPropertyPreFilter) {
                ((IncludesPropertyPreFilter) preFilter).getIncludes().addAll(revisePaths);
            }
        } else {
            propertyFilters.put(beanClass, new IncludesPropertyPreFilter(beanClass, revisePaths));
        }

    }

    if (references.size() > 0) {
        for (Entry<String, Set<String>> reference : references.entrySet()) {
            pathVisit(propertyFilters, reference.getValue(),
                    PropertyUtil.getPropertyType(beanClass, reference.getKey()));
        }
    }
}
项目:itmarry    文件:JSON.java   
public static final String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter();

    try {
        JSONSerializer serializer = new JSONSerializer(out);
        for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
            serializer.config(feature, true);
        }

        serializer.config(SerializerFeature.WriteDateUseDateFormat, true);

        if (filter != null) {
            if (filter instanceof PropertyPreFilter) {
                serializer.getPropertyPreFilters().add((PropertyPreFilter) filter);
            }

            if (filter instanceof NameFilter) {
                serializer.getNameFilters().add((NameFilter) filter);
            }

            if (filter instanceof ValueFilter) {
                serializer.getValueFilters().add((ValueFilter) filter);
            }

            if (filter instanceof PropertyFilter) {
                serializer.getPropertyFilters().add((PropertyFilter) filter);
            }

            if (filter instanceof BeforeFilter) {
                serializer.getBeforeFilters().add((BeforeFilter) filter);
            }

            if (filter instanceof AfterFilter) {
                serializer.getAfterFilters().add((AfterFilter) filter);
            }
        }

        serializer.write(object);

        return out.toString();
    } finally {
        out.close();
    }
}