Пример #1
0
        public static string Serialize(object entity, string body, bool doNotSerializeBody = false)
        {
            if (entity == null)
            {
                return(null);
            }
            var type = entity.GetType();

            GetProperties(type);
            if (!propertyCache.ContainsKey(type.FullName))
            {
                return(null);
            }
            var propertyDictionary = propertyCache[type.FullName];
            var entityDictionary   = new SortedDictionary <string, object>();

            if (!doNotSerializeBody && JsonSerializerHelper.IsJson(body))
            {
                try
                {
                    entityDictionary.Add("body", JObject.Parse(body));
                }
                catch (Exception)
                {
                    try
                    {
                        entityDictionary.Add("body", JArray.Parse(body));
                    }
                    catch (Exception)
                    {
                        entityDictionary.Add("body", body);
                    }
                }
            }
            else
            {
                entityDictionary.Add("body", body);
            }
            foreach (var keyValuePair in propertyDictionary)
            {
                var camelCase = string.Format("{0}{1}",
                                              keyValuePair.Key.Substring(0, 1).ToLower(CultureInfo.InvariantCulture),
                                              keyValuePair.Key.Substring(1, keyValuePair.Key.Length - 1));
                entityDictionary.Add(camelCase, null);
                try
                {
                    var value = keyValuePair.Value.GetValue(entity, null);
                    entityDictionary[camelCase] = value;
                }
                // ReSharper disable once EmptyGeneralCatchClause
                catch (Exception)
                {
                }
            }
            return(JsonSerializerHelper.Serialize(entityDictionary, Formatting.Indented));
        }
Пример #2
0
        public static string Serialize(IEnumerable <object> entities, IEnumerable <string> bodies)
        {
            var entityEnumerable = entities as object[] ?? entities.ToArray();
            var bodyEnumerable   = bodies as string[] ?? bodies.ToArray();

            if (entityEnumerable.Length == 0 || bodyEnumerable.Length == 0 ||
                entityEnumerable.Length != bodyEnumerable.Length)
            {
                return(null);
            }
            var type = entityEnumerable[0].GetType();

            GetProperties(type);
            if (!propertyCache.ContainsKey(type.FullName))
            {
                return(null);
            }
            var propertyDictionary = propertyCache[type.FullName];
            var entityList         = new List <SortedDictionary <string, object> >(entityEnumerable.Length);

            for (var i = 0; i < entityEnumerable.Length; i++)
            {
                var entityDictionary = new SortedDictionary <string, object>();
                if (JsonSerializerHelper.IsJson(bodyEnumerable[i]))
                {
                    entityDictionary.Add("body", JObject.Parse(bodyEnumerable[i]));
                }
                else
                {
                    entityDictionary.Add("body", bodyEnumerable[i]);
                }
                foreach (var keyValuePair in propertyDictionary)
                {
                    var camelCase = string.Format("{0}{1}",
                                                  keyValuePair.Key.Substring(0, 1).ToLower(CultureInfo.InvariantCulture),
                                                  keyValuePair.Key.Substring(1, keyValuePair.Key.Length - 1));
                    entityDictionary.Add(camelCase, null);
                    try
                    {
                        var value = keyValuePair.Value.GetValue(entityEnumerable[i], null);
                        entityDictionary[camelCase] = value;
                    }
                    // ReSharper disable once EmptyGeneralCatchClause
                    catch (Exception)
                    {
                    }
                }
                entityList.Add(entityDictionary);
            }
            return(JsonSerializerHelper.Serialize(entityList.ToArray(), Formatting.Indented));
        }