示例#1
0
        internal static string GetJsonTypeString(TomlItemType type)
        {
            switch (type)
            {
            case TomlItemType.BasicString:
            case TomlItemType.MultilineBasicString:
            case TomlItemType.LiteralString:
            case TomlItemType.MultilineLiteralString:
            case TomlItemType.Datetime:
                return("string");

            case TomlItemType.Integer:
            case TomlItemType.Float:
                return("number");

            case TomlItemType.Boolean:
                return("boolean");

            case TomlItemType.Array:
                return("array");

            case TomlItemType.Table:
            case TomlItemType.InlineTable:
                return("object");

            default:
                return("null");
            }
        }
示例#2
0
 private void EnsureArrayType()
 {
     if (this.isArray && !this.element.HasElements)
     {
         this.arrayType = TomlItemType.None;
     }
 }
示例#3
0
 private static XAttribute[] CreateTypeAttr(TomlItemType nodeType)
 {
     return(new[]
     {
         new XAttribute("type", XUtils.GetJsonTypeString(nodeType)),
         new XAttribute("toml", nodeType.ToString())
     });
 }
 internal static TomlItemType Normalize(this TomlItemType source)
 {
     switch (source)
     {
     case TomlItemType.MultilineBasicString:
     case TomlItemType.LiteralString:
     case TomlItemType.MultilineLiteralString:
         return(TomlItemType.BasicString);
     }
     return(source);
 }
示例#5
0
        private static object ToXml(TomlItemType nodeType, object obj)
        {
            var dt = obj as DynamicToml;

            if (dt != null)
            {
                return(dt.element.Elements());
            }

            switch (nodeType)
            {
            case TomlItemType.Array:
                return(((IEnumerable)obj).Cast <object>().Select(o =>
                {
                    var type = GetTomlType(o);
                    return new XElement("item", CreateTypeAttr(type), ToXml(type, o));
                }));

            case TomlItemType.Table:
            case TomlItemType.InlineTable:
                var xd = new XDocument();
                using (var xw = xd.CreateWriter())
                {
                    var s = new DataContractJsonSerializer(obj.GetType());
                    s.WriteObject(xw, obj);
                }
                var root = new XElement(xd.Root);
                foreach (var xe in root.Descendants())
                {
                    var type = XUtils.GetTypeAttr(xe);
                    if (type == "null")
                    {
                        xe.Remove();
                    }
                    else
                    {
                        xe.SetAttributeValue("toml",
                                             (type == "string" ? TomlItemType.BasicString
                                : type == "number" ? (xe.Value.Any(c => c == '.' || c == 'e' || c == 'E')
                                    ? TomlItemType.Float : TomlItemType.Integer)
                                : type == "boolean" ? TomlItemType.Boolean
                                : type == "array" ? TomlItemType.Array
                                : TomlItemType.Table).ToString()
                                             );
                    }
                }
                return(root.Elements());

            default:
                return(obj);
            }
        }
示例#6
0
        private void EnsureArrayType(TomlItemType nodeType)
        {
            if (!this.isArray)
            {
                return;
            }

            var n = nodeType.Normalize();

            if (this.arrayType == TomlItemType.None)
            {
                this.arrayType = n;
            }
            else if (this.arrayType != n)
            {
                throw new ArgumentException("The value is unmatched for the type of this array.");
            }
        }
示例#7
0
 internal static string GetJsonTypeString(TomlItemType type)
 {
     switch (type)
     {
         case TomlItemType.BasicString:
         case TomlItemType.MultilineBasicString:
         case TomlItemType.LiteralString:
         case TomlItemType.MultilineLiteralString:
         case TomlItemType.Datetime:
             return "string";
         case TomlItemType.Integer:
         case TomlItemType.Float:
             return "number";
         case TomlItemType.Boolean:
             return "boolean";
         case TomlItemType.Array:
             return "array";
         case TomlItemType.Table:
         case TomlItemType.InlineTable:
             return "object";
         default:
             return "null";
     }
 }
 public TomlValue(TomlItemType type, object value)
 {
     this.Type  = type;
     this.Value = value;
 }