Example #1
0
        public static T Parse <T>(string value)
        {
            var type = typeof(T);

            if (type == typeof(TimeSpan))
            {
                return((T)Convert.ChangeType(VariantBase.ParseTime(value), type));
            }
            else
            {
                if (type == typeof(float))
                {
                    return((T)Convert.ChangeType(VariantBase.ParseSingle(value), type));
                }
                else if (type == typeof(double))
                {
                    return((T)Convert.ChangeType(VariantBase.ParseDouble(value), type));
                }
                else if (type == typeof(bool))
                {
                    return((T)Convert.ChangeType(VariantBase.ParseBoolean(value), type));
                }
                else if (type == typeof(DateTime))
                {
                    return((T)Convert.ChangeType(VariantBase.ParseDateTime(value), type));
                }
                else
                {
                    return((T)Convert.ChangeType(value, type));
                }
            }
        }
Example #2
0
        public static string ToString <T>(T value)
        {
            var type = typeof(T);

            if (type == typeof(TimeSpan))
            {
                return(VariantBase.ToString((TimeSpan)Convert.ChangeType(value, type)));
            }
            else
            {
                if (type == typeof(double))
                {
                    return(VariantBase.ToString((double)Convert.ChangeType(value, type)));
                }
                else if (type == typeof(bool))
                {
                    return(VariantBase.ToString((bool)Convert.ChangeType(value, type)));
                }
                else if (type == typeof(DateTime))
                {
                    return(VariantBase.ToString((DateTime)Convert.ChangeType(value, type)));
                }
                else
                {
                    return(value.ToString());
                }
            }
        }
Example #3
0
        public static T Parse <T>(string value)
        {
            if (typeof(T) == typeof(TimeSpan))
            {
                return((T)Convert.ChangeType(VariantBase.ParseTime(value), typeof(T)));
            }
            else
            {
                TypeCode typeCode = System.Type.GetTypeCode(typeof(T));
                switch (typeCode)
                {
                case TypeCode.Single:
                    return((T)Convert.ChangeType(VariantBase.ParseSingle(value), typeof(T)));

                case TypeCode.Double:
                    return((T)Convert.ChangeType(VariantBase.ParseDouble(value), typeof(T)));

                case TypeCode.Boolean:
                    return((T)Convert.ChangeType(VariantBase.ParseBoolean(value), typeof(T)));

                case TypeCode.DateTime:
                    return((T)Convert.ChangeType(VariantBase.ParseDateTime(value), typeof(T)));

                default:
                    return((T)Convert.ChangeType(value, typeof(T)));
                }
            }
        }
Example #4
0
        public static string ToString <T>(T value)
        {
            if (typeof(T) == typeof(TimeSpan))
            {
                return(VariantBase.ToString((TimeSpan)Convert.ChangeType(value, typeof(TimeSpan))));
            }
            else
            {
                TypeCode typeCode = System.Type.GetTypeCode(typeof(T));
                switch (typeCode)
                {
                case TypeCode.Double:
                    return(VariantBase.ToString((double)Convert.ChangeType(value, typeof(double))));

                case TypeCode.Boolean:
                    return(VariantBase.ToString((bool)Convert.ChangeType(value, typeof(bool))));

                case TypeCode.DateTime:
                    return(VariantBase.ToString((DateTime)Convert.ChangeType(value, typeof(DateTime))));

                default:
                    return(value.ToString());
                }
            }
        }
Example #5
0
        private void WriteElement(Variant element)
        {
            WriteStartTag(m_stack.Peek().m_name);

            switch (element.Type)
            {
            case VariantBase.EnumType.None:
            {
                WriteAttributes(m_stack.Peek().m_attributes);
                break;
            }

            case VariantBase.EnumType.Any:
            case VariantBase.EnumType.String:
            case VariantBase.EnumType.Float:
            case VariantBase.EnumType.Double:
            case VariantBase.EnumType.Int32:
            case VariantBase.EnumType.UInt32:
            case VariantBase.EnumType.Int64:
            case VariantBase.EnumType.UInt64:
            case VariantBase.EnumType.Boolean:
            case VariantBase.EnumType.Date:
            case VariantBase.EnumType.Time:
            case VariantBase.EnumType.DateTime:
            {
                WriteAttributes(m_stack.Peek().m_attributes);
                WriteText(element);
                break;
            }

            case VariantBase.EnumType.Dictionary:
            case VariantBase.EnumType.Bag:
            {
                WriteAttributes(m_stack.Peek().m_attributes);
                if ((m_mode & XmlMode.Preserve) != 0)
                {
                    if (element.ContainsKey(XmlConst.Attributes))
                    {
                        WriteAttributes(element[XmlConst.Attributes]);
                    }

                    foreach (VariantItem item in element)
                    {
                        if (item.Key == XmlConst.Attributes)
                        {
                            continue;
                        }
                        else if (item.Key == XmlConst.Text)
                        {
                            WriteText(item.Value);
                        }
                        else if (item.Key == XmlConst.Instruction)
                        {
                            WriteInstruction(item.Value);
                        }
                        else if (item.Key == XmlConst.Comment)
                        {
                            WriteComment(item.Value);
                        }
                        else
                        {
                            Push(item.Key);
                            WriteElement(item.Value);
                            Pop();
                        }
                    }
                }
                else
                {
                    foreach (VariantItem item in element)
                    {
                        Push(item.Key);
                        WriteVariant(item.Value);
                        Pop();
                    }
                }

                break;
            }

            case VariantBase.EnumType.List:
            {
                WriteAttributes(m_stack.Peek().m_attributes);
                foreach (VariantItem item in element)
                {
                    Push();
                    WriteVariant(item.Value);
                    Pop();
                }

                break;
            }

            case VariantBase.EnumType.Tuple:
            {
                m_stack.Peek().m_attributes.Add("size", new Variant(element.Count));

                WriteAttributes(m_stack.Peek().m_attributes);

                foreach (VariantItem item in element)
                {
                    Push();
                    WriteVariant(item.Value);
                    Pop();
                }

                break;
            }

            case VariantBase.EnumType.TimeSeries:
            {
                WriteAttributes(m_stack.Peek().m_attributes);

                foreach (VariantItem item in element)
                {
                    Push().Add("time", new Variant(item.Time));
                    WriteVariant(item.Value);
                    Pop();
                }

                break;
            }

            case VariantBase.EnumType.Buffer:
            {
                WriteAttributes(m_stack.Peek().m_attributes);

                m_writer.WriteBase64(element.AsBuffer(), 0, element.AsBuffer().Length);
                break;
            }

            case VariantBase.EnumType.Object:
            {
                IVariantObject obj = element.AsObject();

                // write class name
                m_stack.Peek().m_attributes.Add("class", new Variant(obj.Class));
                m_stack.Peek().m_attributes.Add("version", new Variant(obj.Version));

                WriteAttributes(m_stack.Peek().m_attributes);

                // write parameter dictionary
                Push("params");
                WriteVariant(obj.Deflate());
                Pop();

                break;
            }

            case VariantBase.EnumType.Exception:
            {
                WriteAttributes(m_stack.Peek().m_attributes);

                VariantExceptionInfo e = element.AsException();

                Push("type");
                WriteElement(new Variant(e.Class));
                Pop();

                Push("message");
                WriteElement(new Variant(e.Message));
                Pop();

                if (e.Source.Length != 0)
                {
                    Push("source");
                    WriteElement(new Variant(e.Source));
                    Pop();
                }

                if (e.Stack.Length != 0)
                {
                    Push("stack");
                    WriteElement(new Variant(e.Stack));
                    Pop();
                }
                break;
            }

            case VariantBase.EnumType.Array:
            {
                TypedArray array = element.AsArray();

                m_stack.Peek().m_attributes.Add("size", new Variant(array.Count));
                m_stack.Peek().m_attributes.Add("elementType", new Variant(array.ElementType.ToString()));

                WriteAttributes(m_stack.Peek().m_attributes);

                WriteDelegate writer = null;
                switch (array.ElementType)
                {
                case VariantBase.EnumType.Float:
                    writer = o => m_writer.WriteString(VariantBase.ToString((float)o));
                    break;

                case VariantBase.EnumType.Double:
                    writer = o => m_writer.WriteString(VariantBase.ToString((double)o));
                    break;

                case VariantBase.EnumType.String:
                    writer = o => m_writer.WriteString((string)o);
                    break;

                case VariantBase.EnumType.Boolean:
                    writer = o => m_writer.WriteString(VariantBase.ToString((bool)o));
                    break;

                case VariantBase.EnumType.Int32:
                    writer = o => m_writer.WriteString(VariantBase.ToString((int)o));
                    break;

                case VariantBase.EnumType.UInt32:
                    writer = o => m_writer.WriteString(VariantBase.ToString((uint)o));
                    break;

                case VariantBase.EnumType.Int64:
                    writer = o => m_writer.WriteString(VariantBase.ToString((long)o));
                    break;

                case VariantBase.EnumType.UInt64:
                    writer = o => m_writer.WriteString(VariantBase.ToString((ulong)o));
                    break;

                case VariantBase.EnumType.Time:
                    writer = o => m_writer.WriteString(VariantBase.ToString((TimeSpan)o));
                    break;

                case VariantBase.EnumType.DateTime:
                    writer = o => m_writer.WriteString(VariantBase.ToString((DateTime)o));
                    break;

                default:
                    throw new VariantException("Case exhaustion: " + array.ElementType);
                }

                for (int i = 0; i < array.Count; ++i)
                {
                    m_writer.WriteStartElement(XmlConst.Default);
                    writer(array[i]);
                    m_writer.WriteEndElement();
                }
                break;
            }

            case VariantBase.EnumType.DataTable:
            {
#if !DISABLE_DATATABLE
                DataTable dataTable = element.AsDataTable();

                m_stack.Peek().m_attributes.Add("rows", new Variant(dataTable.Rows.Count));
                m_stack.Peek().m_attributes.Add("columns", new Variant(dataTable.Columns.Count));

                WriteAttributes(m_stack.Peek().m_attributes);

                foreach (DataColumn col in dataTable.Columns)
                {
                    Push("Column");
                    m_stack.Peek().m_attributes.Add("name", new Variant(col.ColumnName));
                    m_stack.Peek().m_attributes.Add("type", new Variant(VariantPrimitiveBase.TypeToEnum(col.DataType).ToString()));

                    WriteElement(new Variant());
                    Pop();
                }

                foreach (DataColumn col in dataTable.Columns)
                {
                    Action <object> writer;
                    switch (VariantPrimitiveBase.TypeToEnum(col.DataType))
                    {
                    case VariantBase.EnumType.Float:
                        writer = o => m_writer.WriteString(VariantBase.ToString((float)o));
                        break;

                    case VariantBase.EnumType.Double:
                        writer = o => m_writer.WriteString(VariantBase.ToString((double)o));
                        break;

                    case VariantBase.EnumType.String:
                        writer = o => m_writer.WriteString((string)o);
                        break;

                    case VariantBase.EnumType.Boolean:
                        writer = o => m_writer.WriteString(VariantBase.ToString((bool)o));
                        break;

                    case VariantBase.EnumType.Int32:
                        writer = o => m_writer.WriteString(VariantBase.ToString((int)o));
                        break;

                    case VariantBase.EnumType.UInt32:
                        writer = o => m_writer.WriteString(VariantBase.ToString((uint)o));
                        break;

                    case VariantBase.EnumType.Int64:
                        writer = o => m_writer.WriteString(VariantBase.ToString((long)o));
                        break;

                    case VariantBase.EnumType.UInt64:
                        writer = o => m_writer.WriteString(VariantBase.ToString((ulong)o));
                        break;

                    case VariantBase.EnumType.Time:
                        writer = o => m_writer.WriteString(VariantBase.ToString((TimeSpan)o));
                        break;

                    case VariantBase.EnumType.DateTime:
                        writer = o => m_writer.WriteString(VariantBase.ToString((DateTime)o));
                        break;

                    default:
                        throw new VariantException("Case exhaustion: " + VariantPrimitiveBase.TypeToEnum(col.DataType));
                    }

                    Push("Column").Add("name", new Variant(col.ColumnName));
                    WriteStartTag("s");
                    WriteAttributes(m_stack.Peek().m_attributes);
                    foreach (DataRow row in dataTable.Rows)
                    {
                        m_writer.WriteStartElement(XmlConst.Default);
                        writer(row[col]);
                        m_writer.WriteEndElement();
                    }
                    WriteEndTag();
                    Pop();
                }
                break;
#else
                throw new NotSupportedException("Datatables are not supported on this platform.");
#endif
            }

            default:
                throw new VariantException("Case exhaustion: " + element.Type);
            }

            WriteEndTag();
        }
Example #6
0
        protected VariantBase(VariantBase rhs)
        {
            EnumType type = rhs.Type;

            switch (type)
            {
            case EnumType.None:
                Value = new VariantNone();
                break;

            case EnumType.Boolean:
                Value = new VariantPrimitive <bool>(rhs.Value as VariantPrimitive <bool>);
                break;

            case EnumType.Double:
                Value = new VariantPrimitive <double>(rhs.Value as VariantPrimitive <double>);
                break;

            case EnumType.Int32:
                Value = new VariantPrimitive <Int32>(rhs.Value as VariantPrimitive <Int32>);
                break;

            case EnumType.UInt32:
                Value = new VariantPrimitive <UInt32>(rhs.Value as VariantPrimitive <UInt32>);
                break;

            case EnumType.Int64:
                Value = new VariantPrimitive <Int64>(rhs.Value as VariantPrimitive <Int64>);
                break;

            case EnumType.UInt64:
                Value = new VariantPrimitive <UInt64>(rhs.Value as VariantPrimitive <UInt64>);
                break;

            case EnumType.DateTime:
                Value = new VariantPrimitive <DateTime>(rhs.Value as VariantPrimitive <DateTime>);
                break;

            case EnumType.Time:
                Value = new VariantPrimitive <TimeSpan>(rhs.Value as VariantPrimitive <TimeSpan>);
                break;

            case EnumType.String:
                Value = new VariantPrimitive <String>(rhs.Value as VariantPrimitive <String>);
                break;

            case EnumType.Any:
                Value = new VariantAny(rhs.Value as VariantAny);
                break;

            case EnumType.List:
                Value = new VariantList(rhs.Value as VariantList);
                break;

            case EnumType.Dictionary:
                Value = new VariantDictionary(rhs.Value as VariantDictionary);
                break;

            case EnumType.Bag:
                Value = new VariantBag(rhs.Value as VariantBag);
                break;

            case EnumType.TimeSeries:
                Value = new VariantTimeSeries(rhs.Value as VariantTimeSeries);
                break;

            case EnumType.Tuple:
                Value = new VariantTuple(rhs.Value as VariantTuple);
                break;

            case EnumType.Buffer:
                Value = new VariantBuffer(rhs.Value as VariantBuffer);
                break;

            case EnumType.Exception:
                Value = new VariantExceptionInfo(rhs.Value as VariantExceptionInfo);
                break;

            default:
                throw new VariantException("Cannot copy variant of type: " + type.ToString());
            }
        }