Example #1
0
        /// <summary> 获取缓存
        /// </summary>
        /// <param name="type">反射对象类型</param>
        /// <param name="ignoreCase">属性/字段名称是否忽略大小写</param>
        /// <exception cref="ArgumentException">缓存中的对象类型与参数type不一致</exception>
        /// <exception cref="ArgumentNullException">参数type为null</exception>
        public static Literacy Cache(Type type, bool ignoreCase)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            Literacy lit;
            Dictionary <Type, Literacy> item = ignoreCase ? IgnoreCaseItems : Items;

            if (item.TryGetValue(type, out lit))
            {
                if (lit.Type != type)
                {
                    throw new ArgumentException("缓存中的对象类型与参数type不一致!");
                }
            }
            else
            {
                lock (item) {
                    if (item.TryGetValue(type, out lit) == false)
                    {
                        lit = new Literacy(type, ignoreCase);
                        item.Add(type, lit);
                    }
                }
            }
            return(lit);
        }
Example #2
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)MemberInfo);
                    }
                    else
                    {
                        Getter = Literacy.CreateGetter((PropertyInfo)MemberInfo);
                    }
                }
            }
        }
Example #3
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);
                }
            }
Example #4
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);
            }
            if (type == typeof(object))
            {
                obj = new Dictionary <string, object>();
                FillDictionary((IDictionary)obj, typeof(string), typeof(object), reader);
                return(obj);
            }
            var lit = Literacy.Cache(type, true);

            obj = lit.NewObject();
            FillObject(obj, lit, reader);
            return(obj);
        }
Example #5
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;
             }
         }
     }
 }
Example #6
0
        /// <summary> 将json字符串转换为指定对象
        /// </summary>
        public Object ToObject(Type type, string json)
        {
            if (json == null)
            {
                return(null);
            }
            if (type == null)
            {
                object obj = null;
                FillObject(ref obj, null, json);
                return(obj);
            }
            if (type.IsArray)
            {
                object arr     = new ArrayList();
                var    eleType = type.GetElementType();
                FillObject(ref arr, Literacy.Cache(eleType, true), json);
                return(((ArrayList)arr).ToArray(eleType));
            }

            {
                var lit = Literacy.Cache(type, true);
                var obj = lit.NewObject();
                FillObject(ref obj, lit, json);
                return(obj);
            }
        }
Example #7
0
        private void FillObject(ref 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();
                        if (obj == null) {
                            obj = new Dictionary<string, object>();
                            lit = Literacy.Cache(typeof(Dictionary<string, object>), true);
                        }
                        FillObject(obj, lit, reader);
                        if (reader.Current != '}') {
                            ThrowMissingCharException('}');
                        }
                    } else if (reader.Current == '[') {
                        reader.MoveNext();

                        if (obj == null) {
                            obj = new ArrayList();
                            FillList((IList)obj, typeof(object), reader);
                        } else if (obj is ArrayList) {
                            FillList((IList)obj, lit.Type, reader);
                        } else {
                            var st = GenericCollection.GetList(lit.Type);
                            if (st == null) {
                                ThrowNoIList(lit.Type);
                            }
                            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);
                    }
                }
            }
        }
Example #8
0
 /// <summary> 将json字符串中的数据填充到指定对象
 /// </summary>
 public void FillObject(object obj, string json)
 {
     if (obj == null)
     {
         throw new ArgumentNullException("obj");
     }
     if (json != null)
     {
         FillObject(ref obj, Literacy.Cache(obj.GetType(), true), json);
     }
 }
Example #9
0
 /// <summary> 获取缓存
 /// </summary>
 /// <param name="type">反射对象类型</param>
 /// <param name="ignoreCase">属性/字段名称是否忽略大小写</param>
 /// <exception cref="ArgumentException">缓存中的对象类型与参数type不一致</exception>
 /// <exception cref="ArgumentNullException">参数type为null</exception>
 public static Literacy Cache(Type type, bool ignoreCase) {
     if (type == null) {
         throw new ArgumentNullException("type");
     }
     Literacy lit;
     Dictionary<Type, Literacy> item = ignoreCase ? IgnoreCaseItems : Items;
     if (item.TryGetValue(type, out lit)) {
         if (lit.Type != type) {
             throw new ArgumentException("缓存中的对象类型与参数type不一致!");
         }
     } else {
         lock (item) {
             if (item.TryGetValue(type, out lit) == false) {
                 lit = new Literacy(type, ignoreCase);
                 item.Add(type, lit);
             }
         }
     }
     return lit;
 }
Example #10
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);
                }
            }
Example #11
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)MemberInfo);
             }
             else
             {
                 Setter = Literacy.CreateSetter((PropertyInfo)MemberInfo);
             }
         }
     }
 }
Example #12
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();
            var fix = "";

            while (ee.MoveNext())
            {
                var p     = ee.Current;
                var value = p.GetValue(obj);
                if (value != null)
                {
                    UnsafeAppend(fix);
                    AppendKey(p.Name, false);
                    AppendObject(value);
                    fix = ",";
                }
            }

            UnsafeAppend('}');
        }
Example #13
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;
             }
         }
     }
 }
Example #14
0
        private void FillObject(ref 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();
                        if (obj == null)
                        {
                            obj = new Dictionary <string, object>();
                            lit = Literacy.Cache(typeof(Dictionary <string, object>), true);
                        }
                        FillObject(obj, lit, reader);
                        if (reader.Current != '}')
                        {
                            ThrowMissingCharException('}');
                        }
                    }
                    else if (reader.Current == '[')
                    {
                        reader.MoveNext();

                        if (obj == null)
                        {
                            obj = new ArrayList();
                            FillList((IList)obj, typeof(object), reader);
                        }
                        else if (obj is ArrayList)
                        {
                            FillList((IList)obj, lit.Type, reader);
                        }
                        else
                        {
                            var st = GenericCollection.GetList(lit.Type);
                            if (st == null)
                            {
                                ThrowNoIList(lit.Type);
                            }
                            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);
                    }
                }
            }
        }