Exemplo n.º 1
0
        public static PropertyInfo[] GetJsonProperties <T>(DealComplexity complexity = DealComplexity.Standard)
        {
            Type   type  = typeof(T);
            string name  = type.FullName;
            string cname = "";

            if (complexity != DealComplexity.Standard)
            {
                cname = name + "_" + complexity.ToString();
            }
            else
            {
                cname = name;
            }

            if (Deck.ContainsKey(cname))
            {
                return(Deck[cname].Select(t => type.GetProperty(t)).ToArray());
            }
            else if (Deck.ContainsKey(name))
            {
                return(Deck[name].Select(t => type.GetProperty(t)).ToArray());
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        internal static void SerializeArray(object item, StringBuilder sb, DealComplexity complexity = DealComplexity.Standard)
        {
            Type type = item.GetType();

            if (type.IsDefined(typeof(JsonObjectAttribute), false))
            {
                var bag = GetBagForObject(item.GetType(), item, complexity);
                SerializeItem(sb, bag, null, complexity);
            }
            else
            {
                ICollection array = (ICollection)item;

                sb.Append("[");
                var count = 0;

                var total = array.Cast <object>().Count();
                foreach (object element in array)
                {
                    SerializeItem(sb, element, null, complexity);
                    count++;
                    if (count < total)
                    {
                        sb.Append(",");
                    }
                }
                sb.Append("]");
            }
        }
Exemplo n.º 3
0
        public static string ToJson(IDictionary <int, object> bag, DealComplexity complexity = DealComplexity.Standard)
        {
            var sb = new StringBuilder(0);

            SerializeItem(sb, bag, null, complexity);

            return(sb.ToString());
        }
Exemplo n.º 4
0
        internal static void CacheReflection(Type item, DealComplexity complexity = DealComplexity.Standard)
        {
            if (_cache[complexity.ToString()].ContainsKey(item))
            {
                return;
            }

            PropertyInfo[] verified = new PropertyInfo[0];
            PropertyInfo[] prepare  = item.GetJsonProperties(complexity);
            if (prepare != null)
            {
                verified = prepare;
            }

            _cache[complexity.ToString()].Add(item, verified);
        }
Exemplo n.º 5
0
        internal static void SerializeObject(object item, StringBuilder sb, bool intAsKey = false, DealComplexity complexity = DealComplexity.Standard)
        {
            sb.Append("{");

            IDictionary nested = (IDictionary)item;
            int         i      = 0;
            int         count  = nested.Count;

            foreach (DictionaryEntry entry in nested)
            {
                sb.Append("\"" + entry.Key + "\"");
                sb.Append(":");

                object value = entry.Value;
                if (value is string)
                {
                    SerializeString(sb, value);
                }
                else
                {
                    SerializeItem(sb, entry.Value, entry.Key.ToString(), complexity);
                }
                if (i < count - 1)
                {
                    sb.Append(",");
                }
                i++;
            }

            sb.Append("}");
        }
Exemplo n.º 6
0
        internal static void SerializeItem(StringBuilder sb, object item, string key = null, DealComplexity complexity = DealComplexity.Standard)
        {
            if (item == null)
            {
                sb.Append("null");
                return;
            }

            if (item is IDictionary)
            {
                SerializeObject(item, sb, false, complexity);
                return;
            }

            if (item is ICollection && !(item is string))
            {
                SerializeArray(item, sb, complexity);
                return;
            }

            if (item is Usid)
            {
                sb.Append("\"" + ((Usid)item).ToString() + "\"");
                return;
            }

            if (item is Ussn)
            {
                sb.Append("\"" + ((Ussn)item).ToString() + "\"");
                return;
            }

            if (item is DateTime)
            {
                sb.Append("\"" + ((DateTime)item).ToString("yyyy-MM-dd HH:mm:dd") + "\"");
                return;
            }

            if (item is Enum)
            {
                sb.Append("\"" + item.ToString() + "\"");
                return;
            }

            if (item is Type)
            {
                sb.Append("\"" + ((Type)item).FullName + "\"");
                return;
            }

            if (item is bool)
            {
                sb.Append(((bool)item).ToString().ToLower());
                return;
            }

            if (item is ValueType)
            {
                sb.Append(item.ToString().Replace(',', '.'));
                return;
            }

            IDictionary <string, object>
            bag = GetBagForObject(item.GetType(), item, complexity);

            SerializeItem(sb, bag, key, complexity);
        }
Exemplo n.º 7
0
 internal static IDictionary <string, object> GetBagForObject <T>(T instance, DealComplexity complexity = DealComplexity.Standard)
 {
     return(GetBagForObject(typeof(T), instance, complexity));
 }
Exemplo n.º 8
0
        internal static IDictionary <string, object> GetBagForObject(Type type, object instance, DealComplexity complexity = DealComplexity.Standard)
        {
            CacheReflection(type, complexity);

            if (type.FullName == null)
            {
                return(null);
            }

            bool anonymous = type.FullName.Contains("__AnonymousType");

            PropertyInfo[] map = _cache[complexity.ToString()][type];

            IDictionary <string, object> bag = InitializeBag();

            foreach (PropertyInfo info in map)
            {
                if (info != null)
                {
                    var readWrite = (info.CanWrite && info.CanRead);
                    if (!readWrite && !anonymous)
                    {
                        continue;
                    }
                    object value = null;
                    try
                    {
                        value = info.GetValue(instance, null);
                    }
                    catch (Exception ex)
                    {
                        //ex.();
                    }
                    bag.Add(info.Name, value);
                }
            }

            return(bag);
        }