基於WinCE的JSON 類庫 源碼


基於WinCE的JSON 類庫,可以將對象序列化成字符串和文件。

 

提示,其在反序列化時有一個BUG:

如果對象的某個字段值為 null,將其序列化成字符串,然后將該字符串反序列化成對象時會報異常。

這個通常影響不大,在序列化時為對象的字段都提供一個非 null 的默認值即可。

 

測試代碼:

  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);
            
            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 enum AccountStatus
    {
        Enabled = 1,
        Disabled = 2,
    }
View Code

 

下載地址:

http://files.cnblogs.com/08shiyan/CodeBetter.JsonCF_v0.2_Source.zip 

 

————————————————

續:

強大的 Newtonsoft.Json

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

之前一直用基於 .NET 的,沒發現有基於 .NET CF 的,今天算是發現寶貝了。呵呵

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM