把grail服务端传递过来的json map数据转换成可以初始化客户端模型的map的工具类

自己写的工具类:

    /*
     * 简单的根据类中的属性过滤掉map中的多余的键值对
     * 主要是用来过滤掉服务端传递过来的json代表的map对象中多余的属性,比如class
     */
    public static Map getFieldValueMap(Map map,Class cls){
        //[field.name:field.value,..]
        def classMap = [:]
        if(map && cls){
            cls.declaredFields.each {
                if(map.containsKey(it.name) && map[it.name] != null){
                    classMap.put(it.name, map[it.name])
                }
            }
        }
        return classMap
    }
    /*
     * excludeList:所有的map中要排除的key的List
     * (客户端的Model基本上是跟服务端一一对应的,但是服务端还会传送一个key为class的键值对,
     * 这是每个Model中都会传送的,因此设置该字段,就是要统一排除那些不要的字段)
     * ***
     * 功能:
     * 1.过滤掉map中多余的键值对,目前只有class这个键值对需要过滤掉
     * 2.如果value本身也是一个Model,则要调用该方法
     * 服务端传递过来的数据:newAgreement:[id:5, class:RoomPriceAgreement]
     * 要转换成的数据:newAgreement:[id:5]
     * 3.格式化时间,服务器传递过来的数据格式如下:
     * yyyy-MM-ddTHH:mm:ssZ
     * 这种格式的字符串直接传递给Model的构造函数是不能够初始化的
     * 服务端传递过来的数据:dateCreated:1999-11-29T16:00:00Z
     * 要转换成的数据:dateCreated:Mon Nov 29 16:00:00 CST 1999
     * 4.处理枚举类型的数据,枚举类型的class是空,如下:
     * 服务器传递过来的数据:defaultOffDay:[name:C, enumType:aspic.hotel.domain.config.DefaultOffDay]
     * 要转换成如下格式的数据:defaultOffDay:C
     * 
     */
    public static Map getFieldValueMap(Map map,List<String> excludeList){
        def classMap = [:]
        map.each {key,value->
            //如果excludeList中存在key,则不添加进classMap
            boolean flag = true
            //判断excludeList中是否包含key
            excludeList.each {
                if(it == key){
                    flag = false
                }
            }
            if(flag && value){
                if(value.class instanceof String){
                    classMap.put(key, getFieldValueMap(value,excludeList))
                }else if(value.class == null){
                    if(value.containsKey("enumType") && value.containsKey('name')){
                        classMap.put(key, "${value['name']}")
                    }
                }else{
                    if(value ==~ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/){
                        classMap.put(key,DateUtils.parse(value,"yyyy-MM-dd'T'HH:mm:ss'Z'"))
                    }else{
                        classMap.put(key, value)
                    }
                }
            }
        }
        return classMap
    }
    
    /**
     * getFieldValueMap(Map map,List<String> excludeList)方法的简化版
     */
    public static Map getFieldValueMap(Map map){
        return getFieldValueMap(map,['class'])
    }

 

服务端是采用grail做的,grail传递过来的数据如下:

[[orderValue:1, isIntegral:true, isAutoBilling:true, id:1, defaultOffTime:1, isDefault:true, name:19, hourRoomTime:1, newAgreement:[id:5, class:RoomPriceAgreement], isLimit:true, preferentialPrice:100, agreementType:[name:M, enumType:aspic.hotel.domain.config.AgreementType], isPreferential:true, autoBillingBeginHour:56, preferentialStartWeek:1, class:aspic.hotel.domain.config.RoomPriceAgreement, lastUpdated:1999-11-29T16:00:00Z, defaultOffDay:[name:C, enumType:aspic.hotel.domain.config.DefaultOffDay], preferentialEndWeek:1, limitEndTime:1, autoBillingBeginTimingRate:1, autoBillingFullPriceHour:1, price:10, limitBeginTime:1, upTime:11, roomType:[id:0, class:RoomType], dateCreated:1999-11-29T16:00:00Z], [orderValue:2, isIntegral:false, isAutoBilling:false, id:4, defaultOffTime:, isDefault:false, name:24, hourRoomTime:2, newAgreement:[id:4, class:RoomPriceAgreement], isLimit:false, preferentialPrice:200, agreementType:[name:M, enumType:aspic.hotel.domain.config.AgreementType], isPreferential:false, autoBillingBeginHour:46, preferentialStartWeek:2, class:aspic.hotel.domain.config.RoomPriceAgreement, lastUpdated:1999-11-29T16:00:00Z, defaultOffDay:[name:S, enumType:aspic.hotel.domain.config.DefaultOffDay], preferentialEndWeek:2, limitEndTime:2, autoBillingBeginTimingRate:null, autoBillingFullPriceHour:null, price:20, limitBeginTime:null, upTime:12, roomType:[id:10, class:RoomType], dateCreated:1999-11-29T16:00:00Z], [orderValue:3, isIntegral:false, isAutoBilling:false, id:5, defaultOffTime:, isDefault:false, name:35, hourRoomTime:3, newAgreement:[id:6, class:RoomPriceAgreement], isLimit:false, preferentialPrice:300, agreementType:[name:M, enumType:aspic.hotel.domain.config.AgreementType], isPreferential:false, autoBillingBeginHour:35, preferentialStartWeek:3, class:aspic.hotel.domain.config.RoomPriceAgreement, lastUpdated:1999-11-29T16:00:00Z, defaultOffDay:[name:C, enumType:aspic.hotel.domain.config.DefaultOffDay], preferentialEndWeek:3, limitEndTime:3, autoBillingBeginTimingRate:null, autoBillingFullPriceHour:null, price:30, limitBeginTime:null, upTime:12, roomType:[id:10, class:RoomType], dateCreated:1999-11-29T16:00:00Z], [orderValue:4, isIntegral:false, isAutoBilling:false, id:6, defaultOffTime:0, isDefault:false, name:45, hourRoomTime:4, newAgreement:[id:1, class:RoomPriceAgreement], isLimit:false, preferentialPrice:400, agreementType:[name:M, enumType:aspic.hotel.domain.config.AgreementType], isPreferential:false, autoBillingBeginHour:25, preferentialStartWeek:4, class:aspic.hotel.domain.config.RoomPriceAgreement, lastUpdated:1999-11-29T16:00:00Z, defaultOffDay:[name:S, enumType:aspic.hotel.domain.config.DefaultOffDay], preferentialEndWeek:4, limitEndTime:4, autoBillingBeginTimingRate:0, autoBillingFullPriceHour:0, price:40, limitBeginTime:0, upTime:14, roomType:[id:0, class:RoomType], dateCreated:1999-11-29T16:00:00Z], [orderValue:5, isIntegral:true, isAutoBilling:true, id:7, defaultOffTime:1, isDefault:true, name:55, hourRoomTime:5, newAgreement:[id:5, class:RoomPriceAgreement], isLimit:true, preferentialPrice:500, agreementType:[name:M, enumType:aspic.hotel.domain.config.AgreementType], isPreferential:true, autoBillingBeginHour:10, preferentialStartWeek:5, class:aspic.hotel.domain.config.RoomPriceAgreement, lastUpdated:1999-11-29T16:00:00Z, defaultOffDay:[name:C, enumType:aspic.hotel.domain.config.DefaultOffDay], preferentialEndWeek:5, limitEndTime:5, autoBillingBeginTimingRate:1, autoBillingFullPriceHour:1, price:50, limitBeginTime:1, upTime:15, roomType:[id:0, class:RoomType], dateCreated:1999-11-29T16:00:00Z]]

在客户端的用法是:

new RoomPriceAgreement(MapUtil.getFieldValueMap(it))

 

posted on 2013-04-22 15:28  只愿软禁  阅读(267)  评论(0)    收藏  举报