protected Variant ReadVariant() { Variant.EnumType type = ReadType(); switch (type) { case Variant.EnumType.None: return(new Variant(Variant.EnumType.None)); case Variant.EnumType.String: return(new Variant(ReadString())); case Variant.EnumType.Any: return(new Variant(Variant.EnumType.Any, ReadString())); case Variant.EnumType.Float: return(new Variant(ReadFloat())); case Variant.EnumType.Double: return(new Variant(ReadDouble())); case Variant.EnumType.Int32: return(new Variant(ReadInt32())); case Variant.EnumType.UInt32: return(new Variant(ReadUInt32())); case Variant.EnumType.Int64: return(new Variant(ReadInt64())); case Variant.EnumType.UInt64: return(new Variant(ReadUInt64())); case Variant.EnumType.Boolean: return(new Variant(ReadBoolean())); case Variant.EnumType.Time: if ((m_mode & BinaryMode.DateTimeAsTicks) == 0) { throw new VariantException("Binary data has DateTimeAsTicks mode disabled which is not supported in the protean.NET BinaryReader"); } return(new Variant(ReadTime())); case Variant.EnumType.DateTime: if ((m_mode & BinaryMode.DateTimeAsTicks) == 0) { throw new VariantException("Binary data has DateTimeAsTicks mode disabled which is not supported in the protean.NET BinaryReader"); } return(new Variant(ReadDateTime())); case Variant.EnumType.Date: throw new VariantException("Attempt to read Date variant which is no longer supported"); case Variant.EnumType.Tuple: { int length = ReadInt32(); Variant result = new Variant(Variant.EnumType.Tuple, length); for (int i = 0; i < length; ++i) { result[i] = ReadVariant(); } return(result); } case Variant.EnumType.List: { int length = ReadInt32(); Variant result = new Variant(type, length); for (int i = 0; i < length; ++i) { result.Add(ReadVariant()); } return(result); } case Variant.EnumType.Dictionary: case Variant.EnumType.Bag: { int length = ReadInt32(); Variant result = new Variant(type); for (int i = 0; i < length; ++i) { String key = ReadString(); Variant value = ReadVariant(); result.Add(key, value); } return(result); } case Variant.EnumType.TimeSeries: { Variant result = new Variant(type); int length = ReadInt32(); for (int i = 0; i < length; ++i) { DateTime time = ReadDateTime(); Variant value = ReadVariant(); result.Add(time, value); } return(result); } case Variant.EnumType.Object: { string className = ReadString(); int version = ReadInt32(); Variant param = ReadVariant(); IVariantObject obj = null; if (m_factory != null) { obj = m_factory.Create(className); if (obj == null && (m_mode & BinaryMode.CreateProxy) == 0) { throw new VariantException("Object of class " + className + " is not regsistered in factory"); } } if (obj == null) { obj = new VariantObjectProxy(className); } obj.Inflate(param, version); return(new Variant(obj)); } case Variant.EnumType.Exception: { string xtype = ReadString(); string xmessage = ReadString(); string xsource = ReadString(); string xstack = ReadString(); return(new Variant(new VariantExceptionInfo(xtype, xmessage, xsource, xstack))); } case Variant.EnumType.Buffer: { int length = ReadInt32(); return(new Variant(ReadBytes(length, true))); } case Variant.EnumType.DataTable: { return(new Variant(ReadDataTable())); } case Variant.EnumType.Array: { return(new Variant(ReadArray())); } default: throw new VariantException("Case exhaustion: " + type.ToString()); } }
public override void EndElement() { if (m_stack.Count == 0) { throw new VariantException("End tag does not match start tag"); } ElementInfo context = m_stack.Peek(); if (!context.m_attributes.Empty) { if (context.m_element.Is(Variant.EnumType.Mapping)) { foreach (VariantItem item in context.m_attributes) { context.m_element.Add(item.Key, item.Value); } } else { throw new VariantException("No attributes allowed in XML element of " + context.m_type); } } switch (context.m_type) { case Variant.EnumType.None: context.m_element = new Variant(); break; case Variant.EnumType.Any: case Variant.EnumType.String: case Variant.EnumType.Int32: case Variant.EnumType.UInt32: case Variant.EnumType.Int64: case Variant.EnumType.UInt64: case Variant.EnumType.Float: case Variant.EnumType.Double: case Variant.EnumType.Boolean: case Variant.EnumType.Date: case Variant.EnumType.Time: case Variant.EnumType.DateTime: context.m_element = new Variant(context.m_type, context.m_data); break; case Variant.EnumType.Buffer: context.m_element = new Variant(System.Convert.FromBase64String(context.m_data)); break; case Variant.EnumType.Exception: { string typeStr = "", messageStr = "", sourceStr = "", stackStr = ""; if (context.m_element.ContainsKey("type")) { typeStr = context.m_element["type"].As <string>(); } else { throw new VariantException("Missing 'type' element in Exception variant"); } if (context.m_element.ContainsKey("message")) { messageStr = context.m_element["message"].As <string>(); } else { throw new VariantException("Missing 'message' element in Exception variant"); } if (context.m_element.ContainsKey("source")) { sourceStr = context.m_element["source"].As <string>(); } if (context.m_element.ContainsKey("stack")) { stackStr = context.m_element["stack"].As <string>(); } context.m_element = new Variant(new VariantExceptionInfo(typeStr, messageStr, sourceStr, stackStr)); break; } case Variant.EnumType.Object: { string className = ""; int version; Variant param; if (context.m_element.ContainsKey("class")) { className = context.m_element["class"].As <string>(); } else { throw new VariantException("Missing 'class' element in Object variant"); } if (context.m_element.ContainsKey("version")) { version = context.m_element["version"].As <int>(); } else { throw new VariantException("Missing 'version' element in Object variant"); } if (context.m_element.ContainsKey("params")) { param = context.m_element["params"]; } else { throw new VariantException("Missing 'params' element in Object variant"); } IVariantObject obj = null; if (m_factory != null) { obj = m_factory.Create(className); if (obj == null && (m_mode & XmlMode.CreateProxy) == 0) { throw new VariantException("Object of class " + className + " is not regsistered in factory"); } } if (obj == null) { obj = new VariantObjectProxy(className); } obj.Inflate(param, version); context.m_element = new Variant(obj); break; } } if (m_stack.Count > 0) { m_stack.Pop(); } if (m_stack.Count == 0) { m_result = context.m_element; } else { ElementInfo parentContext = m_stack.Peek(); switch (parentContext.m_type) { case Variant.EnumType.Exception: case Variant.EnumType.Object: case Variant.EnumType.Dictionary: case Variant.EnumType.Bag: { parentContext.m_element.Add(context.m_name, context.m_element); break; } case Variant.EnumType.List: { parentContext.m_element.Add(context.m_element); break; } case Variant.EnumType.Tuple: { parentContext.m_element[parentContext.m_rowIndex++] = context.m_element; break; } case Variant.EnumType.TimeSeries: { parentContext.m_element.Add(context.m_time, context.m_element); break; } } } }
public override void EndElement() { if (m_stack.Count==0) { throw new VariantException("End tag does not match start tag"); } ElementInfo context = m_stack.Peek(); if (!context.m_attributes.Empty) { if (context.m_element.Is(Variant.EnumType.Mapping)) { foreach(VariantItem item in context.m_attributes) { context.m_element.Add(item.Key, item.Value); } } else { throw new VariantException("No attributes allowed in XML element of "+ context.m_type); } } switch (context.m_type) { case Variant.EnumType.None: context.m_element = new Variant(); break; case Variant.EnumType.Any: case Variant.EnumType.String: case Variant.EnumType.Int32: case Variant.EnumType.UInt32: case Variant.EnumType.Int64: case Variant.EnumType.UInt64: case Variant.EnumType.Float: case Variant.EnumType.Double: case Variant.EnumType.Boolean: case Variant.EnumType.Date: case Variant.EnumType.Time: case Variant.EnumType.DateTime: context.m_element = new Variant(context.m_type, context.m_data); break; case Variant.EnumType.Buffer: context.m_element = new Variant(System.Convert.FromBase64String(context.m_data)); break; case Variant.EnumType.Exception: { string typeStr = "", messageStr = "", sourceStr = "", stackStr = ""; if (context.m_element.ContainsKey("type")) { typeStr = context.m_element["type"].As<string>(); } else { throw new VariantException("Missing 'type' element in Exception variant"); } if (context.m_element.ContainsKey("message")) { messageStr = context.m_element["message"].As<string>(); } else { throw new VariantException("Missing 'message' element in Exception variant"); } if (context.m_element.ContainsKey("source")) { sourceStr = context.m_element["source"].As<string>(); } if (context.m_element.ContainsKey("stack")) { stackStr = context.m_element["stack"].As<string>(); } context.m_element = new Variant(new VariantExceptionInfo(typeStr, messageStr, sourceStr, stackStr)); break; } case Variant.EnumType.Object: { string className = ""; int version; Variant param; if (context.m_element.ContainsKey("class")) { className = context.m_element["class"].As<string>(); } else { throw new VariantException("Missing 'class' element in Object variant"); } if (context.m_element.ContainsKey("version")) { version = context.m_element["version"].As<int>(); } else { throw new VariantException("Missing 'version' element in Object variant"); } if (context.m_element.ContainsKey("params")) { param = context.m_element["params"]; } else { throw new VariantException("Missing 'params' element in Object variant"); } IVariantObject obj = null; if (m_factory!=null) { obj = m_factory.Create(className); if (obj==null && (m_mode & XmlMode.CreateProxy)==0) { throw new VariantException("Object of class " + className + " is not regsistered in factory"); } } if (obj==null) { obj = new VariantObjectProxy(className); } obj.Inflate(param, version); context.m_element = new Variant(obj); break; } } if (m_stack.Count > 0) { m_stack.Pop(); } if (m_stack.Count == 0) { m_result = context.m_element; } else { ElementInfo parentContext = m_stack.Peek(); switch (parentContext.m_type) { case Variant.EnumType.Exception: case Variant.EnumType.Object: case Variant.EnumType.Dictionary: case Variant.EnumType.Bag: { parentContext.m_element.Add(context.m_name, context.m_element); break; } case Variant.EnumType.List: { parentContext.m_element.Add(context.m_element); break; } case Variant.EnumType.Tuple: { parentContext.m_element[parentContext.m_rowIndex++] = context.m_element; break; } case Variant.EnumType.TimeSeries: { parentContext.m_element.Add(context.m_time, context.m_element); break; } } } }
protected Variant ReadVariant() { Variant.EnumType type = ReadType(); switch (type) { case Variant.EnumType.None: return new Variant(Variant.EnumType.None); case Variant.EnumType.String: return new Variant(ReadString()); case Variant.EnumType.Any: return new Variant(Variant.EnumType.Any, ReadString()); case Variant.EnumType.Float: return new Variant(ReadFloat()); case Variant.EnumType.Double: return new Variant(ReadDouble()); case Variant.EnumType.Int32: return new Variant(ReadInt32()); case Variant.EnumType.UInt32: return new Variant(ReadUInt32()); case Variant.EnumType.Int64: return new Variant(ReadInt64()); case Variant.EnumType.UInt64: return new Variant(ReadUInt64()); case Variant.EnumType.Boolean: return new Variant(ReadBoolean()); case Variant.EnumType.Time: if ((m_mode & BinaryMode.DateTimeAsTicks) == 0) { throw new VariantException("Binary data has DateTimeAsTicks mode disabled which is not supported in the protean.NET BinaryReader"); } return new Variant(ReadTime()); case Variant.EnumType.DateTime: if ((m_mode & BinaryMode.DateTimeAsTicks) == 0) { throw new VariantException("Binary data has DateTimeAsTicks mode disabled which is not supported in the protean.NET BinaryReader"); } return new Variant(ReadDateTime()); case Variant.EnumType.Date: throw new VariantException("Attempt to read Date variant which is no longer supported"); case Variant.EnumType.Tuple: { int length = ReadInt32(); Variant result = new Variant(Variant.EnumType.Tuple, length); for (int i = 0; i < length; ++i) { result[i] = ReadVariant(); } return result; } case Variant.EnumType.List: { int length = ReadInt32(); Variant result = new Variant(type, length); for (int i=0; i<length; ++i) { result.Add(ReadVariant()); } return result; } case Variant.EnumType.Dictionary: case Variant.EnumType.Bag: { int length = ReadInt32(); Variant result = new Variant(type); for (int i=0; i<length; ++i) { String key = ReadString(); Variant value = ReadVariant(); result.Add(key, value); } return result; } case Variant.EnumType.TimeSeries: { Variant result = new Variant(type); int length = ReadInt32(); for (int i=0; i<length; ++i) { DateTime time = ReadDateTime(); Variant value = ReadVariant(); result.Add(time, value); } return result; } case Variant.EnumType.Object: { string className = ReadString(); int version = ReadInt32(); Variant param = ReadVariant(); IVariantObject obj = null; if (m_factory != null) { obj = m_factory.Create(className); if (obj == null && (m_mode & BinaryMode.CreateProxy) == 0) { throw new VariantException("Object of class " + className + " is not regsistered in factory"); } } if (obj == null) { obj = new VariantObjectProxy(className); } obj.Inflate(param, version); return new Variant(obj); } case Variant.EnumType.Exception: { string xtype = ReadString(); string xmessage = ReadString(); string xsource = ReadString(); string xstack = ReadString(); return new Variant(new VariantExceptionInfo(xtype, xmessage, xsource, xstack)); } case Variant.EnumType.Buffer: { int length = ReadInt32(); return new Variant(ReadBytes(length, true)); } case Variant.EnumType.DataTable: { return new Variant(ReadDataTable()); } case Variant.EnumType.Array: { return new Variant(ReadArray()); } default: throw new VariantException("Case exhaustion: " + type.ToString()); } }