更多内容,可以参考这个文章:
http://www.360doc.com/content/12/0319/15/1542811_195663419.shtml
[java]
view plain
copy
-
publicclassComplexPropertyPreFilterimplementsPropertyFilter{
- privateMap<Class<?>,Set<String>>includeMap=newHashMap<Class<?>,Set<String>>();
- //@Override
- booleanapply(Objectsource,Stringname,Objectvalue){
- for(Entry<Class<?>,Set<String>>entry:includeMap.entrySet()){
- Class<?>class1=entry.getKey();
- if(source.getClass()==class1){
- Set<String>fields=entry.getValue();
- for(Stringfield:fields){
- if(field.equals(name)){
- returnfalse;
- }
- }
- true;
-
- publicComplexPropertyPreFilter(Map<Class<?>,Set<String>>includeMap){
- this.includeMap=includeMap;
- }
测试类:
classWheel{
- Stringname;
- intsize;
- Stringcolor;
- publicStringgetName(){
- returnname;
- voidsetName(Stringname){
- this.name=name;
- intgetSize(){
- returnsize;
- voidsetSize(intsize){
- this.size=size;
- publicStringgetColor(){
- returncolor;
- voidsetColor(Stringcolor){
- this.color=color;
- publicWheel(){
- super();
- this.color="black";
- this.name="miqilin";
- this.size=17;
-
- classSofa{
- Stringcolor;
- intcount;
- Stringtexture;
- publicStringgetColor(){
- returncolor;
- voidsetColor(Stringcolor){
- this.color=color;
- intgetCount(){
- returncount;
- voidsetCount(intcount){
- this.count=count;
- publicStringgetTexture(){
- returntexture;
- voidsetTexture(Stringtexture){
- this.texture=texture;
- publicSofa(){
- super();
- this.color="white";
- this.count=4;
- this.texture="fur";
- classCar{
- privateWheelwheel;
- privateSofasofa;
- publicWheelgetWheel(){
- returnwheel;
- voidsetWheel(Wheelwheel){
- this.wheel=wheel;
- publicSofagetSofa(){
- returnsofa;
- voidsetSofa(Sofasofa){
- this.sofa=sofa;
- publicCar(){
- this.wheel=newWheel();
- this.sofa=newSofa();
- classTest{
- staticvoidmain(Stringargs[]){
- Map<String,Car>map=newHashMap<String,Car>();
- map.put("car1",newCar());
- map.put("car2",153); font-weight:bold; background-color:inherit">newCar());
-
- Map<Class<?>,Set<String>>includeMap= Set<String>set=newHashSet<String>();
- set.add("color");
- includeMap.put(Wheel.class,set);
- set=newHashSet<String>();
- set.add("texture");
- includeMap.put(Sofa. ComplexPropertyPreFiltercfilter=newComplexPropertyPreFilter(includeMap);
- SerializeWritersw=newSerializeWriter();
- JSONSerializerserializer=newJSONSerializer(sw);
- serializer.getPropertyFilters().add(cfilter);
- serializer.write(map);
- System.out.println(sw.toString());
- }
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|