コード例 #1
0
ファイル: XmlSerializer.cs プロジェクト: tfreyx/Cfg-NET
        void SerializeElements(IDictionary <string, CfgMetadata> meta, object node, StringBuilder builder, int level)
        {
            foreach (var pair in meta.Where(kv => kv.Value.ListType != null))
            {
                var m = meta[pair.Key];

                if (!m.Attribute.serialize)
                {
                    continue;
                }

                var items = (IList)m.Getter(node);
                if (items == null || items.Count == 0)
                {
                    continue;
                }
                var name = m.Attribute.name;

                Indent(builder, level);
                builder.Append("<");
                builder.Append(name);
                builder.AppendLine(">");

                foreach (var item in items)
                {
                    var metaData = CfgMetadataCache.GetMetadata(item.GetType());
                    Indent(builder, level + 1);
                    builder.Append("<add");
                    SerializeAttributes(metaData, item, builder);

                    if (JustAttributes(metaData, item))
                    {
                        builder.AppendLine(" />");
                    }
                    else
                    {
                        builder.AppendLine(">");
                        SerializeElements(metaData, item, builder, level + 2);
                        Indent(builder, level + 1);
                        builder.AppendLine("</add>");
                    }
                }

                Indent(builder, level);
                builder.Append("</");
                builder.Append(name);
                builder.AppendLine(">");
            }
        }
コード例 #2
0
ファイル: JsonSerializer.cs プロジェクト: tfreyx/Cfg-NET
        private void SerializeElements(IDictionary <string, CfgMetadata> meta, object node, StringBuilder sb, int level)
        {
            var pairs = meta.Where(kv => kv.Value.ListType != null && kv.Value.Attribute.serialize).ToArray();

            for (var y = 0; y < pairs.Length; y++)
            {
                var pair  = pairs[y];
                var nodes = (IList)meta[pair.Key].Getter(node);
                if (nodes == null || nodes.Count == 0)
                {
                    continue;
                }

                Indent(sb, level);
                sb.Append("\"");
                sb.Append(meta[pair.Key].Attribute.name);
                sb.AppendLine("\":[");

                var count = nodes.Count;
                var last  = count - 1;
                for (var i = 0; i < count; i++)
                {
                    var item     = nodes[i];
                    var metaData = CfgMetadataCache.GetMetadata(item.GetType());
                    Indent(sb, level + 1);
                    sb.Append("{");
                    SerializeAttributes(metaData, item, sb);
                    if (metaData.Any(kv => kv.Value.ListType != null))
                    {
                        SerializeElements(metaData, item, sb, level + 2);
                        Indent(sb, level + 1);
                        Next(sb, i, last);
                    }
                    else
                    {
                        Next(sb, i, last);
                    }
                }

                Indent(sb, level);
                sb.Append("]");

                if (y < pairs.Length - 1)
                {
                    sb.Append(",");
                }
            }
        }
コード例 #3
0
ファイル: XmlSerializer.cs プロジェクト: tfreyx/Cfg-NET
        string InnerSerialize(CfgNode node)
        {
            var type = node.GetType();

#if NETS
            var attribute = type.GetTypeInfo().GetCustomAttributes(typeof(CfgAttribute), true).FirstOrDefault() as CfgAttribute;
#else
            var attribute = type.GetCustomAttributes(typeof(CfgAttribute), true).FirstOrDefault() as CfgAttribute;
#endif
            if (attribute != null && !attribute.serialize)
            {
                return(string.Empty);
            }

            var name    = !string.IsNullOrEmpty(attribute?.name) ? attribute.name : type.Name;
            var meta    = CfgMetadataCache.GetMetadata(type);
            var builder = new StringBuilder();

            if (JustAttributes(meta, node))
            {
                builder.Append("<add");
                SerializeAttributes(meta, node, builder);
                builder.Append(" />");
            }
            else
            {
                builder.Append("<");
                builder.Append(name);
                SerializeAttributes(meta, node, builder);
                builder.AppendLine(">");
                SerializeElements(meta, node, builder, 1);
                builder.Append("</");
                builder.Append(name);
                builder.Append(">");
            }

            return(builder.ToString());
        }
コード例 #4
0
ファイル: JsonSerializer.cs プロジェクト: tfreyx/Cfg-NET
        private string InnerSerialize(CfgNode node)
        {
            var meta    = CfgMetadataCache.GetMetadata(node.GetType());
            var builder = new StringBuilder();

            if (meta.All(kv => kv.Value.ListType == null))
            {
                builder.Append("{");
                SerializeAttributes(meta, node, builder);
                builder.TrimEnd(", ");
                builder.Append(" }");
            }
            else
            {
                builder.AppendLine("{");
                SerializeAttributes(meta, node, builder);
                SerializeElements(meta, node, builder, 1);
                builder.TrimEnd(", ");
                builder.AppendLine();
                builder.Append("}");
            }

            return(builder.ToString());
        }
コード例 #5
0
ファイル: ObjectNode.cs プロジェクト: tfreyx/Cfg-NET
        public ObjectNode(object cfgNode, string name)
        {
            var type     = cfgNode.GetType();
            var metadata = CfgMetadataCache.GetMetadata(type);

            Name       = name;
            Attributes = new List <IAttribute>();
            SubNodes   = new List <INode>();

            foreach (var pair in metadata)
            {
                if (pair.Value.ListType == null)
                {
                    // get the value, create attribute
                    var value     = pair.Value.Getter(cfgNode);
                    var attribute = new NodeAttribute(pair.Value.Attribute.name, value);
                    _attributes[attribute.Name] = attribute;
                    Attributes.Add(attribute);
                }
                else
                {
                    // get the list, transfer it to collection node of sub nodes
                    var list = (IList)pair.Value.Getter(cfgNode);
                    if (list == null)
                    {
                        continue;
                    }
                    var collection = new CollectionNode(pair.Value.Attribute.name);
                    foreach (var item in list)
                    {
                        collection.SubNodes.Add(new ObjectNode(item, "add"));
                    }
                    SubNodes.Add(collection);
                }
            }
        }
コード例 #6
0
ファイル: Extensions.cs プロジェクト: tfreyx/Cfg-NET
 /// <summary>
 /// When you want to clone yourself
 /// </summary>
 /// <param name="node"></param>
 /// <returns></returns>
 public static T Clone <T>(this T node) where T : CfgNode
 {
     return(CfgMetadataCache.Clone(node));
 }