Ejemplo n.º 1
0
            public static GenericCollection GetList(Type type)
            {
                GenericCollection list;

                if (Cache.TryGetValue(type, out list))
                {
                    return(list);
                }
                if (type.GetInterface("System.Collections.IList") != typeof(IList))
                {
                    return(null);
                }
                lock (Cache)
                {
                    if (Cache.TryGetValue(type, out list))
                    {
                        return(list);
                    }
                    list      = new GenericCollection();
                    list.Init = Literacy.CreateNewObject(type);
                    if (type.IsGenericType)
                    {
                        list.ElementType = type.GetGenericArguments()[0];
                        list.ElementType = Nullable.GetUnderlyingType(list.ElementType) ?? list.ElementType;
                    }
                    else
                    {
                        list.ElementType = typeof(object);
                    }
                    Cache.Add(type, list);
                    return(list);
                }
            }
Ejemplo n.º 2
0
        /// <summary> 获取缓存
        /// </summary>
        /// <param name="type">反射对象类型</param>
        /// <param name="ignoreCase">属性/字段名称是否忽略大小写</param>
        /// <returns></returns>
        public static Literacy Cache(Type type, bool ignoreCase)
        {
            Literacy lit;
            Dictionary <Type, Literacy> item = ignoreCase ? _IgnoreCaseItems : _Items;

            if (item.TryGetValue(type, out lit))
            {
                if (lit._Type != type)
                {
                    throw new Exception("缓存中的对象反射类型与参数type不一致!");
                }
            }
            else
            {
                lock (item)
                {
                    if (item.TryGetValue(type, out lit) == false)
                    {
                        lit = new Literacy(type);
                        item.Add(type, lit);
                    }
                }
            }
            return(lit);
        }
Ejemplo n.º 3
0
        /// <summary> 读取对象
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private object ReadObject(UnsafeJsonReader reader, Type type)
        {
            object obj;

            if (type.GetInterface("System.Collections.IDictionary") == typeof(IDictionary))
            {
                var st = GenericCollection.GetDict(type);
                if (st.Init == null)
                {
                    ThrowNoConstructor(type);
                }
                obj = st.Init();
                FillDictionary((IDictionary)obj, st.KeyType, st.ElementType, reader);
                return(obj);
            }
            else if (type == typeof(object))
            {
                obj = new Dictionary <string, object>();
                FillDictionary((IDictionary)obj, typeof(string), typeof(object), reader);
                return(obj);
            }
            else
            {
                var lit = Literacy.Cache(type, true);
                obj = lit.NewObject();
                FillObject(obj, lit, reader);
                return(obj);
            }
        }
Ejemplo n.º 4
0
 private void FillObject(object obj, Literacy lit, UnsafeJsonReader reader)
 {
     if (reader.Current == '}')
     {
         return;
     }
     if (obj is IDictionary)
     {
         var st = GenericCollection.GetDict(obj.GetType());
         FillDictionary((IDictionary)obj, st.KeyType, st.ElementType, reader);
     }
     else
     {
         while (true)
         {
             var key  = ReadKey(reader);                 //获取Key
             var prop = lit.Property[key];               //得到对象属性
             if (prop == null || prop.CanWrite == false) //如果属性不存在或不可写
             {
                 SkipValue(reader);                      //跳过Json中的值
             }
             else
             {
                 object val = ReadValue(reader, prop.MemberType); //得到值
                 prop.TrySetValue(obj, val);                      //赋值
             }
             if (reader.SkipChar(',') == false)
             {
                 return;
             }
         }
     }
 }
Ejemplo n.º 5
0
 /// <summary> 加载Setter
 /// </summary>
 public void LoadSetter()
 {
     if (Setter != PreSetter)
     {
         return;
     }
     lock (this)
     {
         if (Setter == PreSetter) //Setter未编译
         {
             if (!CanWrite)       //当前成员可读
             {
                 Setter = ErrorSetter;
             }
             else if (Field)
             {
                 Setter = Literacy.CreateSetter((FieldInfo)_Member);
             }
             else
             {
                 Setter = Literacy.CreateSetter((PropertyInfo)_Member);
             }
         }
     }
 }
Ejemplo n.º 6
0
        /// <summary> 加载Getter
        /// </summary>
        public void LoadGetter()
        {
            if (Getter != PreGetter)
            {
                return;
            }

            lock (this)
            {
                if (Getter == PreGetter) //Getter未编译
                {
                    if (!CanRead)        //当前对象不可读
                    {
                        Getter = ErrorGetter;
                    }
                    else if (Field)
                    {
                        Getter = Literacy.CreateGetter((FieldInfo)_Member);
                    }
                    else
                    {
                        Getter = Literacy.CreateGetter((PropertyInfo)_Member);
                    }
                }
            }
        }
Ejemplo n.º 7
0
        private void FillObject(object obj, Literacy lit, string json)
        {
            if (json == null || json.Length == 0)
            {
                return;
            }

            unsafe
            {
                fixed(char *p = json)
                {
                    UnsafeJsonReader reader = new UnsafeJsonReader(p, json.Length);

                    if (reader.IsEnd())
                    {
                        return;
                    }

                    if (reader.Current == '{')
                    {
                        reader.MoveNext();
                        FillObject(obj, lit, reader);
                        if (reader.Current != '}')
                        {
                            ThrowMissingCharException('}');
                        }
                    }
                    else if (reader.Current == '[')
                    {
                        reader.MoveNext();
                        var st = GenericCollection.GetList(obj.GetType());
                        if (st == null)
                        {
                            ThrowNoIList(obj.GetType());
                        }
                        FillList((IList)obj, st.ElementType, reader);

                        if (reader.Current != ']')
                        {
                            ThrowMissingCharException(']');
                        }
                    }
                    else
                    {
                        ThrowException("起始字符:" + reader.Current);
                    }
                    reader.MoveNext();
                    if (reader.IsEnd())
                    {
                        reader.Dispose();
                    }
                    else
                    {
                        ThrowException("错误的结束字符:" + reader.Current);
                    }
                }
            }
        }
Ejemplo n.º 8
0
        public List <object> ToList(string json)
        {
            if (json == null)
            {
                return(null);
            }
            var obj = new List <object>();

            FillObject(obj, Literacy.Cache(typeof(List <object>), true), json);
            return(obj);
        }
Ejemplo n.º 9
0
        public T[] ToArray <T>(string json)
        {
            if (json == null)
            {
                return(null);
            }
            var obj = new ArrayList();

            FillObject(obj, Literacy.Cache(typeof(T[]), true), json);
            return((T[])obj.ToArray(typeof(T)));
        }
Ejemplo n.º 10
0
        public Dictionary <string, object> ToDictionary(string json)
        {
            if (json == null)
            {
                return(null);
            }
            var obj = new Dictionary <string, object>();

            FillObject(obj, Literacy.Cache(typeof(Dictionary <string, object>), true), json);
            return(obj);
        }
Ejemplo n.º 11
0
 /// <summary> 将json字符串中的数据填充到指定对象
 /// </summary>
 public void FillObject(object obj, string json)
 {
     if (obj == null)
     {
         throw new ArgumentNullException("obj");
     }
     if (json != null)
     {
         FillObject(obj, Literacy.Cache(obj.GetType(), true), json);
     }
 }
Ejemplo n.º 12
0
        /// <summary> 将json字符串转换为指定对象
        /// </summary>
        public T ToObject <T>(string json)
        {
            if (json == null)
            {
                return(default(T));
            }
            var lit = Literacy.Cache(typeof(T), true);
            var obj = lit.NewObject();

            FillObject(obj, lit, json);
            return((T)obj);
        }
Ejemplo n.º 13
0
        /// <summary> 将json字符串转换为指定对象
        /// </summary>
        public Object ToObject(Type type, string json)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (json == null)
            {
                return(null);
            }
            var lit = Literacy.Cache(type, true);
            var obj = lit.NewObject();

            FillObject(obj, lit, json);
            return(obj);
        }
Ejemplo n.º 14
0
            public static GenericCollection GetDict(Type type)
            {
                GenericCollection dict;

                if (Cache.TryGetValue(type, out dict))
                {
                    return(dict);
                }
                lock (Cache)
                {
                    if (Cache.TryGetValue(type, out dict))
                    {
                        return(dict);
                    }
                    dict      = new GenericCollection();
                    dict.Init = Literacy.CreateNewObject(type);
                    if (type.IsGenericType)
                    {
                        var ga = type.GetGenericArguments();
                        if (ga.Length > 1)
                        {
                            dict.KeyType     = type.GetGenericArguments()[0];
                            dict.ElementType = type.GetGenericArguments()[1];
                            dict.ElementType = Nullable.GetUnderlyingType(dict.ElementType) ?? dict.ElementType;
                        }
                        else
                        {
                            dict.ElementType = typeof(object);
                        }
                    }
                    else
                    {
                        dict.ElementType = typeof(object);
                    }
                    Cache.Add(type, dict);
                    return(dict);
                }
            }
Ejemplo n.º 15
0
        /// <summary> 将未知对象按属性名和值转换为Json中的键值字符串写入Buffer
        /// </summary>
        /// <param name="obj">非null的位置对象</param>
        protected override void AppendOther(object obj)
        {
            Type     type = obj.GetType();
            Literacy lit  = Literacy.Cache(type, true);

            UnsafeAppend('{');
            var ee = lit.Property.GetEnumerator();

            if (ee.MoveNext())
            {
                var p = ee.Current;
                AppendKey(p.Name, false);
                AppendObject(p.GetValue(obj));
                while (ee.MoveNext())
                {
                    p = ee.Current;
                    UnsafeAppend(',');
                    AppendKey(p.Name, false);
                    AppendObject(p.GetValue(obj));
                }
            }

            UnsafeAppend('}');
        }