.net和java中不支持字典的序列化的变通方法

1.java代码
为了解决Map不支持序列化的问题,
特意定义了一个数组(里面用到了List来做转换)
迎合序列化的需要

        @Expose
        @XStreamAlias( "TheFroms")
        @SerializedName( "TheFroms")
         public OrderFromInfo[] theFroms;

        @Expose
        @XStreamAlias( "Froms")
        @SerializedName( "Froms")
         public Map<Integer, OrderFromInfo> Froms;

         private Map<Integer, OrderFromInfo> froms = new HashMap<Integer, OrderFromInfo>();

         /**
         * new一个List来做Map数组的转换
         *Author:shaorongfei
         * @return new
         */
         public OrderFromInfo[] getTheFroms() {
                List<OrderFromInfo> theItems = new ArrayList<OrderFromInfo>();
                 if (froms != null) {
                         for (OrderFromInfo orderFromInfo : froms.values()) {
                                theItems.add(orderFromInfo);
                        }
                }
                 final int size = theItems.size();
                theFroms = (OrderFromInfo[]) theItems.toArray( new OrderFromInfo[size]);
                 return theFroms;
        }

         /**
         * 迎合序列化需要,数组等同Froms属性。
         * Author:shaorongfei
         * @param theFroms 传入的OrderFromInfo[]数组
         */
         public void setTheFroms(OrderFromInfo[] theFroms) {
                froms = new HashMap<Integer, OrderFromInfo>();
                 for (OrderFromInfo item : theFroms) {
                        froms.put(item.getRftype(), item);
                }
        }

         public Map<Integer, OrderFromInfo> getFroms() {
                 return froms;
        }

         public void setFroms(Map<Integer, OrderFromInfo> froms) {
                 this.froms = froms;

        }

2.C#代码

     private Dictionary< int, OrderFromInfo> froms = new Dictionary< int, OrderFromInfo>();

     /// <summary>
     /// 来源信息
     /// </summary>
    [XmlIgnore]
     public Dictionary< int, OrderFromInfo> Froms
    {
      get { return froms; }
      set { froms = value; }
    }

     /// <summary>
     /// 迎合序列化需要,等同Coupons属性
     /// </summary>
     public OrderFromInfo[] TheFroms
    {
      get
      {
        List<OrderFromInfo> theItemss = new List<OrderFromInfo>();
         if ( froms != null )
        {
           foreach ( KeyValuePair< int, OrderFromInfo> kvp in froms )
          {
            theItemss.Add( kvp.Value );
          }
        }

         return theItemss.ToArray();
      }
      set
      {
        froms = new Dictionary< int, OrderFromInfo>();
         foreach ( OrderFromInfo item in value )
        {
          froms.Add( item.Rftype, item );
        }
      }
    }

两者的思想是一样的,都是为了解决字典(C#)或者Map(java)
对象序列化后取不到值所做的变通方法。

java中的get,set封装javabean比较好理解,C#的set{froms=value}可以这样来理解,如下:

通俗来讲..get是获取值 set是设置值

比如这句:get { return this._username; }
如果调用这个get方法 就能获得方法return的 this._username(返回的值)
调用方法:Console.Write(对象.UserName);(输出该属性的值)

比如这句:set { this._username = value; }
如果调用这个set方法 就可以重新设置username的值 value就是你新设置的值
调用方法:对象.UserName="新值";(为该属性附新值)

个人理解:
1.在封装数据库字段的时候非常有用,相信大家都这么用。
2.你可以吧UserName当成一个这个类的一个属性,这个属性由字段_username的值确定,也可能不是_username,而是经过_username变化的。你可以在GET、SET中作相应的处理。比如这样:

public string UserName
{
get {
if(string.IsNullOrEmpty(_username))
return ERRORCODE;
return this._username;
}
set {
if(string.IsNullOrEmpty(_username))
this._username = defaultusername;
this._username = value;
}
}



以上需要的朋友可以借此来试一试,在做Xml序列化时项目里经常会用到。

你可能感兴趣的:(java,.net,字典序列化)