Exemplo n.º 1
0
        public void Parse()
        {
            while (m_reader.Read())
            {
                XmlNodeType nType = m_reader.NodeType;

                if (nType == XmlNodeType.Element)
                {
                    string name           = XmlConvert.DecodeName(m_reader.Name);
                    bool   isEmptyElement = m_reader.IsEmptyElement;

                    VariantBase.EnumType elementType = GetVariantTypeFromSchema();
                    Variant attributes = new Variant(VariantBase.EnumType.Bag);
                    for (int i = 0; i < m_reader.AttributeCount; ++i)
                    {
                        m_reader.MoveToAttribute(i);
                        VariantBase.EnumType attrType = GetVariantTypeFromSchema();
                        if (attrType != VariantBase.EnumType.Buffer)
                        {
                            attributes.Add(XmlConvert.DecodeName(m_reader.Name), new Variant(attrType, m_reader.Value));
                        }
                        else
                        {
                            attributes.Add(XmlConvert.DecodeName(m_reader.Name), new Variant(Convert.FromBase64String(m_reader.Value)));
                        }
                    }

                    StartElement(name, attributes, elementType);

                    if (isEmptyElement)
                    {
                        EndElement();
                    }
                }
                else if (nType == XmlNodeType.Text)
                {
                    Characters(m_reader.Value);
                }
                else if (nType == XmlNodeType.Comment)
                {
                    Comment(m_reader.Value);
                }
                else if (nType == XmlNodeType.ProcessingInstruction)
                {
                    ProcessingInstruction(XmlConvert.DecodeName(m_reader.Name), m_reader.Value);
                }
                else if (nType == XmlNodeType.EndElement)
                {
                    EndElement();
                }
                else if (nType == XmlNodeType.CDATA)
                {
                    Characters(m_reader.Value);
                }
            }
        }
Exemplo n.º 2
0
        private static CountDelegate GetWriteCounterDelegate(VariantBase.EnumType type)
        {
            CountDelegate counter;

            switch (type)
            {
            case VariantBase.EnumType.Float:
                counter = o => sizeof(float);
                break;

            case VariantBase.EnumType.Double:
                counter = o => sizeof(double);
                break;

            case VariantBase.EnumType.String:
                counter = o => GetByteCountString((string)o);
                break;

            case VariantBase.EnumType.Boolean:
                counter = o => sizeof(bool);
                break;

            case VariantBase.EnumType.Int32:
                counter = o => sizeof(int);
                break;

            case VariantBase.EnumType.UInt32:
                counter = o => sizeof(uint);
                break;

            case VariantBase.EnumType.Int64:
                counter = o => sizeof(long);
                break;

            case VariantBase.EnumType.UInt64:
                counter = o => sizeof(ulong);
                break;

            case VariantBase.EnumType.Time:
                counter = o => sizeof(long);
                break;

            case VariantBase.EnumType.DateTime:
                counter = o => sizeof(long);
                break;

            default:
                throw new VariantException("Case exhaustion: " + type);
            }

            return(counter);
        }
Exemplo n.º 3
0
        private WriteDelegate GetWriterDelegate(VariantBase.EnumType type)
        {
            WriteDelegate writer;

            switch (type)
            {
            case VariantBase.EnumType.Float:
                writer = o => WriteFloat((float)o);
                break;

            case VariantBase.EnumType.Double:
                writer = o => WriteDouble((double)o);
                break;

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

            case VariantBase.EnumType.Boolean:
                writer = o => WriteBool((bool)o);
                break;

            case VariantBase.EnumType.Int32:
                writer = o => WriteInt((int)o);
                break;

            case VariantBase.EnumType.UInt32:
                writer = o => WriteUInt32((uint)o);
                break;

            case VariantBase.EnumType.Int64:
                writer = o => WriteInt64((long)o);
                break;

            case VariantBase.EnumType.UInt64:
                writer = o => WriteUInt64((ulong)o);
                break;

            case VariantBase.EnumType.Time:
                writer = o => WriteTimeSpan((TimeSpan)o);
                break;

            case VariantBase.EnumType.DateTime:
                writer = o => WriteDateTime((DateTime)o);
                break;

            default:
                throw new VariantException("Case exhaustion: " + type);
            }

            return(writer);
        }
Exemplo n.º 4
0
        public void WriteDataTable(DataTable arg)
        {
#if !DISABLE_DATATABLE
            int numCols = arg.Columns.Count;
            int numRows = arg.Rows.Count;

            VariantBase.EnumType[] colTypes = new VariantBase.EnumType[numCols];
            string[] colNames = new string[numCols];

            for (int i = 0; i < numCols; ++i)
            {
                DataColumn col = arg.Columns[i];
                colTypes[i] = VariantPrimitiveBase.TypeToEnum(col.DataType);
                colNames[i] = col.ColumnName;
            }

            // Write number of columns
            WriteInt(numCols);

            // Write number of rows
            WriteInt(numRows);

            // Write column types
            foreach (VariantBase.EnumType colType in colTypes)
            {
                WriteInt((int)colType);
            }

            // Write column names
            foreach (string colName in colNames)
            {
                WriteString(colName);
            }

            // Write columns
            for (int i = 0; i < numCols; ++i)
            {
                WriteDelegate colWriter = GetWriterDelegate(colTypes[i]);

                foreach (DataRow item in arg.Rows)
                {
                    if (item.IsNull(i))
                    {
                        throw new VariantException("Cannot serialise DataTables containing null elements.");
                    }
                    colWriter(item[i]);
                }
            }
#else
            throw new NotSupportedException("Datatables are not supported on this platform.");
#endif
        }
Exemplo n.º 5
0
 public abstract void StartElement(string name, Variant attributes, VariantBase.EnumType variantType);
Exemplo n.º 6
0
        protected void WriteVariant(Variant v)
        {
            VariantBase.EnumType type = v.Type;
            Write((Int32)type);

            switch (type)
            {
            case VariantBase.EnumType.None:
                break;

            case VariantBase.EnumType.String:
            case VariantBase.EnumType.Any:
                Write(v.As <string>());
                break;

            case VariantBase.EnumType.Float:
                Write(v.As <float>());
                break;

            case VariantBase.EnumType.Double:
                Write(v.As <double>());
                break;

            case VariantBase.EnumType.Int32:
                Write(v.As <Int32>());
                break;

            case VariantBase.EnumType.UInt32:
                Write(v.As <UInt32>());
                break;

            case VariantBase.EnumType.Int64:
                Write(v.As <Int64>());
                break;

            case VariantBase.EnumType.UInt64:
                Write(v.As <UInt64>());
                break;

            case VariantBase.EnumType.Boolean:
                Write(v.As <bool>());
                break;

            case VariantBase.EnumType.Time:
                Write(v.As <TimeSpan>());
                break;

            case VariantBase.EnumType.DateTime:
                Write(v.As <DateTime>());
                break;

            case VariantBase.EnumType.List:
            case VariantBase.EnumType.Tuple:
                Write(v.Count);
                foreach (VariantItem item in v)
                {
                    WriteVariant(item.Value);
                }
                break;

            case VariantBase.EnumType.Dictionary:
            case VariantBase.EnumType.Bag:
                Write(v.Count);
                foreach (VariantItem item in v)
                {
                    Write(item.Key);
                    WriteVariant(item.Value);
                }
                break;

            case VariantBase.EnumType.TimeSeries:
                Write(v.Count);
                foreach (VariantItem item in v)
                {
                    Write(item.Time);
                    WriteVariant(item.Value);
                }
                break;

            case VariantBase.EnumType.Object:
                IVariantObject o = v.AsObject();
                Write(o.Class);
                Write(o.Version);
                WriteVariant(o.Deflate());
                break;

            case VariantBase.EnumType.Exception:
                VariantExceptionInfo x = v.AsException();
                Write(x.Class);
                Write(x.Message);
                Write(x.Source);
                Write(x.Stack);
                break;

            case VariantBase.EnumType.Buffer:
                Write(v.AsBuffer().Length);
                Write(v.AsBuffer(), true);
                break;

            case VariantBase.EnumType.DataTable:
                Write(v.AsDataTable());
                break;

            case VariantBase.EnumType.Array:
                Write(v.AsArray());
                break;

            default:
                throw new VariantException("Case exhaustion: " + type.ToString());
            }
        }
Exemplo n.º 7
0
        protected void Write(DataTable arg)
        {
#if !DISABLE_DATATABLE
            int numCols = arg.Columns.Count;
            int numRows = arg.Rows.Count;

            VariantBase.EnumType[] colTypes = new VariantBase.EnumType[numCols];
            string[] colNames = new string[numCols];

            for (int i = 0; i < numCols; ++i)
            {
                DataColumn col = arg.Columns[i];
                colTypes[i] = VariantPrimitiveBase.TypeToEnum(col.DataType);
                colNames[i] = col.ColumnName;
            }

            // Write number of columns
            Write(numCols);

            // Write number of rows
            Write(numRows);

            // Write column types
            foreach (VariantBase.EnumType colType in colTypes)
            {
                Write((int)colType);
            }

            // Write column names
            foreach (string colName in colNames)
            {
                Write(colName);
            }

            // Write columns
            for (int i = 0; i < numCols; ++i)
            {
                WriteDelegate colWriter;

                switch (colTypes[i])
                {
                case VariantBase.EnumType.Float:
                    colWriter = o => Write((float)o);
                    break;

                case VariantBase.EnumType.Double:
                    colWriter = o => Write((double)o);
                    break;

                case VariantBase.EnumType.String:
                    colWriter = o => Write((string)o);
                    break;

                case VariantBase.EnumType.Boolean:
                    colWriter = o => Write((bool)o);
                    break;

                case VariantBase.EnumType.Int32:
                    colWriter = o => Write((Int32)o);
                    break;

                case VariantBase.EnumType.UInt32:
                    colWriter = o => Write((UInt32)o);
                    break;

                case VariantBase.EnumType.Int64:
                    colWriter = o => Write((Int64)o);
                    break;

                case VariantBase.EnumType.UInt64:
                    colWriter = o => Write((UInt64)o);
                    break;

                case VariantBase.EnumType.Time:
                    colWriter = o => Write((TimeSpan)o);
                    break;

                case VariantBase.EnumType.DateTime:
                    colWriter = o => Write((DateTime)o);
                    break;

                default:
                    throw new VariantException("Case exhaustion: " + colTypes[i]);
                }

                foreach (DataRow item in arg.Rows)
                {
                    if (item.IsNull(i))
                    {
                        throw new VariantException("Cannot serialise DataTables containing null elements.");
                    }
                    colWriter(item[i]);
                }
            }
#else
            throw new NotSupportedException("Datatables are not supported on this platform.");
#endif
        }
Exemplo n.º 8
0
        public static int GetByteCountVariant(Variant v)
        {
            VariantBase.EnumType type = v.Type;
            var writeCount            = sizeof(Int32);

            switch (type)
            {
            case VariantBase.EnumType.None:
                break;

            case VariantBase.EnumType.String:
            case VariantBase.EnumType.Any:
                writeCount += GetByteCountString(v.As <string>());
                break;

            case VariantBase.EnumType.Float:
                writeCount += sizeof(float);
                break;

            case VariantBase.EnumType.Double:
                writeCount += sizeof(double);
                break;

            case VariantBase.EnumType.Int32:
                writeCount += sizeof(Int32);
                break;

            case VariantBase.EnumType.UInt32:
                writeCount += sizeof(UInt32);
                break;

            case VariantBase.EnumType.Int64:
                writeCount += sizeof(Int64);
                break;

            case VariantBase.EnumType.UInt64:
                writeCount += sizeof(UInt64);
                break;

            case VariantBase.EnumType.Boolean:
                writeCount += sizeof(Int32);
                break;

            case VariantBase.EnumType.Time:
                writeCount += sizeof(Int64);
                break;

            case VariantBase.EnumType.DateTime:
                writeCount += sizeof(Int64);
                break;

            case VariantBase.EnumType.List:
            case VariantBase.EnumType.Tuple:
                writeCount += sizeof(Int32);
                foreach (VariantItem item in v)
                {
                    writeCount += GetByteCountVariant(item.Value);
                }
                break;

            case VariantBase.EnumType.Dictionary:
            case VariantBase.EnumType.Bag:
                writeCount += sizeof(Int32);
                foreach (VariantItem item in v)
                {
                    writeCount += GetByteCountString(item.Key);
                    writeCount += GetByteCountVariant(item.Value);
                }
                break;

            case VariantBase.EnumType.TimeSeries:
                writeCount += sizeof(Int32);
                foreach (VariantItem item in v)
                {
                    writeCount += sizeof(Int64);
                    writeCount += GetByteCountVariant(item.Value);
                }
                break;

            case VariantBase.EnumType.Object:
                IVariantObject o = v.AsObject();
                writeCount += GetByteCountString(o.Class);
                writeCount += sizeof(Int32);
                writeCount += GetByteCountVariant(o.Deflate());
                break;

            case VariantBase.EnumType.Exception:
                VariantExceptionInfo x = v.AsException();
                writeCount += GetByteCountString(x.Class);
                writeCount += GetByteCountString(x.Message);
                writeCount += GetByteCountString(x.Source);
                writeCount += GetByteCountString(x.Stack);
                break;

            case VariantBase.EnumType.Buffer:
                writeCount += sizeof(Int32);
                writeCount += GetByteCountBytes(v.AsBuffer().Length, true);
                break;

            case VariantBase.EnumType.DataTable:
                writeCount += GetByteCountDataTable(v.AsDataTable());
                break;

            case VariantBase.EnumType.Array:
                writeCount += GetByteCountArray(v.AsArray());
                break;

            default:
                throw new VariantException("Case exhaustion: " + type.ToString());
            }

            return(writeCount);
        }
Exemplo n.º 9
0
 public TypedArray(VariantBase.EnumType elementType, int size)
 {
     ElementType = elementType;
     Value       = new Object[size];
 }
Exemplo n.º 10
0
        protected void Write(DataTable arg)
        {
            #if !DISABLE_DATATABLE
            int numCols = arg.Columns.Count;
            int numRows = arg.Rows.Count;

            VariantBase.EnumType[] colTypes = new VariantBase.EnumType[numCols];
            string[] colNames = new string[numCols];

            for (int i = 0; i < numCols; ++i)
            {
                DataColumn col = arg.Columns[i];
                colTypes[i] = VariantPrimitiveBase.TypeToEnum(col.DataType);
                colNames[i] = col.ColumnName;
            }

            // Write number of columns
            Write(numCols);

            // Write number of rows
            Write(numRows);

            // Write column types
            foreach (VariantBase.EnumType colType in colTypes)
            {
                Write((int)colType);
            }

            // Write column names
            foreach (string colName in colNames)
            {
                Write(colName);
            }

            // Write columns
            for (int i = 0; i < numCols; ++i)
            {
                WriteDelegate colWriter;

                switch (colTypes[i])
                {
                    case VariantBase.EnumType.Float:
                        colWriter = o => Write((float)o);
                        break;
                    case VariantBase.EnumType.Double:
                        colWriter = o => Write((double)o);
                        break;
                    case VariantBase.EnumType.String:
                        colWriter = o => Write((string)o);
                        break;
                    case VariantBase.EnumType.Boolean:
                        colWriter = o => Write((bool)o);
                        break;
                    case VariantBase.EnumType.Int32:
                        colWriter = o => Write((Int32)o);
                        break;
                    case VariantBase.EnumType.UInt32:
                        colWriter = o => Write((UInt32)o);
                        break;
                    case VariantBase.EnumType.Int64:
                        colWriter = o => Write((Int64)o);
                        break;
                    case VariantBase.EnumType.UInt64:
                        colWriter = o => Write((UInt64)o);
                        break;
                    case VariantBase.EnumType.Time:
                        colWriter = o => Write((TimeSpan)o);
                        break;
                    case VariantBase.EnumType.DateTime:
                        colWriter = o => Write((DateTime)o);
                        break;
                    default:
                        throw new VariantException("Case exhaustion: " + colTypes[i]);
                }

                foreach (DataRow item in arg.Rows)
                {
                    if (item.IsNull(i))
                    {
                        throw new VariantException("Cannot serialise DataTables containing null elements.");
                    }
                    colWriter(item[i]);
                }
            }
            #else
            throw new NotSupportedException("Datatables are not supported on this platform.");
            #endif
        }