Example #1
0
        public static string ToJSON(EntityCollection collection, Formatting format)
        {
            var           space = format == Formatting.Indented ? " " : "";
            StringBuilder sb    = new StringBuilder();

            sb.Append("{" + EntitySerializer.Sep(format, 1) + "\"entities\":" + space + "[");
            List <string> entities = new List <string>();

            foreach (Entity entity in collection.Entities)
            {
                entities.Add(EntitySerializer.ToJSON(entity, format, 2));
            }
            sb.Append(string.Join(",", entities));
            sb.Append(EntitySerializer.Sep(format, 1) + "]" + EntitySerializer.Sep(format, 0) + "}");
            return(sb.ToString());
        }
        /// <summary>
        /// Helper method to return the Type of a value
        /// </summary>
        /// <param name="value"></param>
        /// <param name="showFriendlyNames"></param>
        /// <returns></returns>
        public static Type GetValueType(object value, bool showFriendlyNames)
        {
            if (value == null)
            {
                return(typeof(string));
            }
            if (showFriendlyNames && !ValueTypeIsFriendly(value))
            {
                return(typeof(string));
            }
            var basevalue = EntitySerializer.AttributeToBaseType(value);

            if (basevalue == null)
            {
                return(typeof(string));
            }
            return(basevalue.GetType());
        }
        public static EntityCollection Deserialize(XmlDocument serializedEntities)
        {
            var ec = new EntityCollection();

            if (serializedEntities != null && serializedEntities.ChildNodes.Count > 0)
            {
                if (serializedEntities.ChildNodes[0].Name == "Entities")
                {
                    var entityName = string.Empty;
                    foreach (XmlNode xEntity in serializedEntities.ChildNodes[0].ChildNodes)
                    {
                        var entity = EntitySerializer.Deserialize(xEntity);
                        ec.Entities.Add(entity);
                        if (string.IsNullOrEmpty(entityName))
                        {
                            entityName = entity.LogicalName;
                        }
                        if (!entityName.Equals(entity.LogicalName))
                        {
                            entityName = "[multipleentities]";
                        }
                    }
                    if (!entityName.Equals("[multipleentities]"))
                    {
                        ec.EntityName = entityName;
                    }
                }
                else
                {
                    var serializer = new DataContractSerializer(typeof(EntityCollection), new List <Type> {
                        typeof(Entity)
                    });
                    var sr = new StringReader(serializedEntities.OuterXml);
                    using (var reader = new XmlTextReader(sr))
                    {
                        ec = (EntityCollection)serializer.ReadObject(reader);
                    }
                    sr.Close();
                }
            }
            return(ec);
        }