基于WinCE的JSON 类库,可以将对象序列化成字符串和文件( CodeBetter.JsonCF_v0.3_Source_修改(添加字典的把序列化).)

 CodeBetter.JsonCF_v0.3_Source_修改(添加字典的把序列化).zip。

提示,其在反序列化时有一个BUG:

如果对象的某个字段值为 null,将其序列化成字符串,然后将该字符串反序列化成对象时会报异常。

这个通常影响不大,在序列化时为对象的字段都提供一个非 null 的默认值即可。

using System;
    using System.Collections.Generic;

    internal class Program
    {
        private static void Main(string[] args)
        {            
            string json = Converter.Serialize(new User("name", "password", AccountStatus.Enabled));
            Converter.Serialize("out.txt", new int[] { 1, 2, 3, -4 }, "_");
            Console.WriteLine(json);


            User user = Converter.Deserialize<User>(json, "_");
            int[] values = Converter.DeserializeFromFile<int[]>("out.txt", "_");
            Console.WriteLine(user.UserName);

            string jsonStr = "{\"goodsId\":\"123\",\"goodsName\":\"物资名称1\"}";
            //把Json字符串把序列化为一个对象
            Goods goods = Converter.Deserialize<Goods>(jsonStr);
            //把一个对象序列化为一个Json字符串
            string serializeGoodsJsonStr = Converter.Serialize(goods);

            string jsonArray = "[{\"goodsId\":\"123\",\"goodsName\":\"物资名称1\"},{\"goodsId\":\"234\",\"goodsName\":\"物资名称2\"}]";
            //把Json数组字符串把序列化为List
            List<Goods> goodsList = Converter.Deserialize<List<Goods>>(jsonArray);
            //把List序列化为Json字符串
            string serializeGoodsListJsonStr = Converter.Serialize(goodsList);

            //把Json字符串反序列化为字典
            Dictionary<string, string> dic = Converter.Deserialize<Dictionary<string, string>>(jsonStr);
            //把字典序列化为Json字符串
            string serializeDicJsonStr = Converter.Serialize(dic);

            Console.WriteLine("Done. Press enter to exit");
            Console.ReadLine();
        }
    }

    public class BaseUser
    {
        private int _id = 1;
    }

    [SerializeIncludingBase]
    public class User : BaseUser
    {
        private string _userName;
        private string _password;
        [NonSerialized]
        private readonly Role _role;
        private AccountStatus _status;    
        private Thing _think = new Thing();

        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }
        public AccountStatus Status
        {
            get { return _status; }
            set { _status = value; }
        }
        public Role Role
        {
            get { return _role; }
        }
        public Thing Thing
        {
            get { return new Thing(); }
        }

        public User(string userName, string password, AccountStatus status)
        {
            UserName = userName;
            Password = password;
            Status = status;
            _role = new Role(DateTime.Now, "Admin", this);
        }

        private User()
        {
        }
    }

    public class Role
    {
        public Role(DateTime expires, string name, User user)
        {
            Expires = expires;
            Name = name;
            User = user;
        }

        public DateTime Expires { get; set; }

        public string Name { get; set; }

        public User User { get; set; }

        public Thing Thing
        {
            get { return new Thing(); }
        }
    }

    public class Thing
    {
        private string _name = "ABC";

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }
    public class Goods 
    {
        private string goodsId;
        private string goodsName;

        public string GoodsId
        {
            get { return goodsId; }
            set { goodsId = value; }
        }
        public string GoodsName
        {
            get { return goodsName; }
            set { goodsName = value; }
        }
    }
    public enum AccountStatus
    {
        Enabled = 1,
        Disabled = 2,
    }

续:

强大的 Newtonsoft.Json

https://files.cnblogs.com/08shiyan/Newtonsoft.Json.Compact.zip

以上内容依据 http://www.cnblogs.com/08shiyan/p/3198048.html 进行修改发布的。

posted @ 2014-05-22 11:11  黄豆芽  阅读(553)  评论(0编辑  收藏  举报