首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

flexjson的运用技巧和扩展

2012-09-25 
flexjson的使用技巧和扩展?? ? 个人真心喜欢flexjson,虽然在效率上比较不上一些以效率鼓吹的json序列化工

flexjson的使用技巧和扩展

?

? ? 个人真心喜欢flexjson,虽然在效率上比较不上一些以效率鼓吹的json序列化工具,但是它在json序列化的灵活上,整个软件的结构上和扩展的方便性上是无与伦比的。

?

flexjson的使用技巧:

? ? flexjson在反序列化json时

如果是ArrayList<Bean>

new JSONSerialize.use("values",Bean.class).deserialize(list);

?

如果只需要一个bean的个别属性,而不需要其它属性,不用一个个的去排除

可以使用

new JSONSerializer().include("propA").exclude("*")

new JSONSerializer().include("a.b").exclude("a.*")

?

?

另外有时候序列化的时候需要特殊需求,由其是在hibernate做对象关联的时候,flexjson在序列化时,1对多已经完善的解决了,但是在多对一个时候没有默认解决,需要一个个排除。

同时有时json在序列化时,需要多它的关联的类中获取某个元素,或子类在序列化时需要父类的元素等。

?

可以通过重写flexjson的ObjectTransformer进行解决。

因为里面的TypeTransformerMap后来注册的对象类型只能是child,所以用了反射改变了它的方法,并在ObjectTransformer加入一些事件。通过json中的path来进行了过滤处理等。

?

?

public void transform(Object object) {        JSONContext context = getContext();        Path path = context.getPath();        ChainedSet visits = context.getVisits();        String pathStr = "";        List<String> pathList = path.getPath();        if (!pathList.isEmpty()) {            pathStr = StringUtils.join(pathList, ".");        }        try {            if (!visits.contains(object)) {                context.setVisits(new ChainedSet(visits));                context.getVisits().add(object);                // traverse object                BeanAnalyzer analyzer = BeanAnalyzer.analyze(resolveClass(object));                TypeContext typeContext = context.writeOpenObject();                for (BeanProperty prop : analyzer.getProperties()) {                    String name = prop.getName();                    path.enqueue(name);                    //添加了属性过滤                    String currPropPath = pathStr + name;                    PropertyFilter propertyFilter = propertyFilterMap.get(currPropPath);                    if (propertyFilter == null && propertyProcesserMap.get(currPropPath) == null) {                        propertyFilter = propertyFilterMap.get("*");                    }                    if (propertyFilter != null && propertyFilter.isFilter(prop, path, object, context, typeContext)) {                        path.pop();                        continue;                    }                    if (context.isIncluded(prop)) {                        Object value = prop.getValue(object);                        //添加了属性值的处理                        PropertyProcesser propertyProcesser = propertyProcesserMap.get(currPropPath);                        if (propertyProcesser != null) {                            value = propertyProcesser.propertyProcesser(prop, value, path, object, context, typeContext);                        }                        if (!context.getVisits().contains(value)) {                            TransformerWrapper transformer = (TransformerWrapper) context.getTransformer(value);                            if (!transformer.isInline()) {                                if (!typeContext.isFirst()) context.writeComma();                                typeContext.setFirst(false);                                context.writeName(name);                            }                            typeContext.setPropertyName(name);                            transformer.transform(value);                        }                    }                    path.pop();                }                //添加了对于当前对象的处理                                                       if (!objectProcesserMap.isEmpty()) {                    ObjectProcesser objectProcesser = objectProcesserMap.get(pathStr);                    if (objectProcesser != null) {                        objectProcesser.objectProcesser(object, path, context, typeContext);                    }                }                context.writeCloseObject();                context.setVisits((ChainedSet) context.getVisits().getParent());            }        } catch (JSONException e) {            throw e;        } catch (Exception e) {            e.printStackTrace();//            throw new JSONException("Error trying to deepSerialize", e);        }    }
?

?

?

具体代码在https://github.com/huanwuji/springbase/tree/master/src/main/java/com/huanwuji/utils/flexJson中

测试类:

只有写法,测试类不能用在

https://github.com/huanwuji/springbase/blob/master/src/test/com/huanwuji/tools/codeBatchCreate/FlexJsonTest.java中。

热点排行