internal void WriteTo(XElement xe, TextWriter writer)
        {
            switch (XUtils.GetTypeAttr(xe))
            {
            case "object":
                if (this.version >= TomlVersion.V04 && XUtils.GetTomlAttr(xe) == TomlItemType.InlineTable)
                {
                    throw new SerializationException("Invalid 'type' attribute.");
                }
                this.SerializeObject(xe, writer);
                break;

            case "array":
                if (IsArrayOfTable(xe))
                {
                    this.SerializeArrayOfTable(xe, writer);
                }
                else
                {
                    this.SerializeArray(xe, writer);
                }
                break;

            default:
                throw new SerializationException("Invalid 'type' attribute.");
            }
        }
示例#2
0
        private static object ToValue(XElement xe)
        {
            switch (XUtils.GetTomlAttr(xe))
            {
            case TomlItemType.BasicString:
            case TomlItemType.MultilineBasicString:
            case TomlItemType.LiteralString:
            case TomlItemType.MultilineLiteralString:
                return(xe.Value);

            case TomlItemType.Integer:
                return((long)xe);

            case TomlItemType.Float:
                return((double)xe);

            case TomlItemType.Boolean:
                return((bool)xe);

            case TomlItemType.Datetime:
                return((DateTimeOffset)xe);

            default:
                return(new DynamicToml(xe));
            }
        }
        private static void WriteString(XElement xe, TextWriter writer)
        {
            switch (XUtils.GetTomlAttr(xe))
            {
            case TomlItemType.BasicString:
                WriteBasicString(xe.Value, writer);
                break;

            case TomlItemType.MultilineBasicString:
                writer.Write("\"\"\"");
                writer.Write(BasicEscape(xe.Value));
                writer.Write("\"\"\"");
                break;

            case TomlItemType.LiteralString:
                foreach (var c in xe.Value)
                {
                    if (c == '\r' || c == '\n')
                    {
                        throw new SerializationException("A literal string cannot contain newlines.");
                    }
                    else if (c == '\'')
                    {
                        throw new SerializationException("A literal string cannot contain single quotes.");
                    }
                }
                writer.Write('\'');
                writer.Write(xe.Value);
                writer.Write('\'');
                break;

            case TomlItemType.MultilineLiteralString:
                if (xe.Value.Contains("'''"))
                {
                    throw new SerializationException("A multi-line literal string cannot contain \"'''\"");
                }
                break;

            case TomlItemType.Datetime:
                WriteDateTime((DateTimeOffset)xe, writer);
                break;

            default:     // BasicString or Datetime
                try
                {
                    WriteDateTime((DateTimeOffset)xe, writer);
                }
                catch (FormatException)
                {
                    WriteBasicString(xe.Value, writer);
                }
                break;
            }
        }
示例#4
0
 private DynamicToml(XElement elm)
 {
     this.element = elm;
     this.isArray = XUtils.GetTomlAttr(elm) == TomlItemType.Array;
     if (this.isArray && elm.HasElements)
     {
         this.arrayType = elm.Elements().Aggregate(TomlItemType.None, (a, xe) =>
         {
             var type = XUtils.GetTomlAttr(xe).Value.Normalize();
             if (a == TomlItemType.None || a == type)
             {
                 return(type);
             }
             throw new InvalidDataException("The values' type are not uniform.");
         });
     }
 }
        private static void WriteNumber(XElement xe, TextWriter writer)
        {
            switch (XUtils.GetTomlAttr(xe))
            {
            case TomlItemType.Integer:
                WriteInteger((long)xe, writer);
                break;

            case TomlItemType.Float:
                WriteFloat((double)xe, writer);
                break;

            default:
                try
                {
                    WriteInteger((long)xe, writer);
                }
                catch (FormatException)
                {
                    WriteFloat((double)xe, writer);
                }
                break;
            }
        }
        private void SerializeObject(XElement xe, TextWriter writer)
        {
            // Table と Array of Table は後に
            var ordered = xe.Nodes().OrderBy(n =>
            {
                var e = n as XElement;
                if (e != null)
                {
                    var type = XUtils.GetTypeAttr(e);
                    if (type == "object")
                    {
                        return(true);
                    }
                    if (type == "array" && IsArrayOfTable(e))
                    {
                        return(true);
                    }
                }
                return(false);
            });

            foreach (var n in ordered)
            {
                var e = n as XElement;
                if (e != null)
                {
                    switch (XUtils.GetTypeAttr(e))
                    {
                    case "string":
                        WriteKey(e, writer);
                        WriteString(e, writer);
                        writer.WriteLine();
                        break;

                    case "number":
                        WriteKey(e, writer);
                        WriteNumber(e, writer);
                        writer.WriteLine();
                        break;

                    case "boolean":
                        WriteKey(e, writer);
                        WriteBoolean(e, writer);
                        writer.WriteLine();
                        break;

                    case "object":
                        if (this.version >= TomlVersion.V04 && XUtils.GetTomlAttr(e) == TomlItemType.InlineTable)
                        {
                            WriteKey(e, writer);
                            WriteInlineTable(e, writer);
                            writer.WriteLine();
                        }
                        else
                        {
                            writer.WriteLine("[{0}]", GetFullName(e));
                            SerializeObject(e, writer);
                        }
                        break;

                    case "array":
                        if (IsArrayOfTable(e))
                        {
                            SerializeArrayOfTable(e, writer);
                        }
                        else
                        {
                            WriteKey(e, writer);
                            SerializeArray(e, writer);
                            writer.WriteLine();
                            break;
                        }
                        break;

                    default:
                        // ignore null
                        break;
                    }
                }
                else
                {
                    var c = n as XComment;
                    if (c != null)
                    {
                        WriteComment(c, writer);
                    }
                    else
                    {
                        throw new SerializationException("Unknown XNode.");
                    }
                }
            }
        }