public static void WriteTypeMetaInfo(IStreamWriter writer, ITypeStructure typeItem, bool writeTypeMetaPrefix = true) { if (writeTypeMetaPrefix) { writer.WriteUInt8(ValueItem.TypeMetaInfo); } writer.WriteUInt8(SingleTypeDescr); Type type = typeItem.RuntimeType; writer.WriteString(type.Assembly.GetName().Name); writer.WriteString(type.FullName); short itemCount = (short)typeItem.Items.Count; writer.WriteInt16(itemCount); for (int i = 0; i < itemCount; i++) { var memberItem = typeItem.Items[i]; writer.WriteInt16((short)memberItem.Type); // ItemType Enum writer.WriteUInt32(memberItem.TypeId); // TypeId (Type Hash) writer.WriteString(memberItem.Name); // Property Name writer.WriteBool(memberItem.IsNullable); // Nullable Flag } }
/// <summary> /// Write enum type meta infos /// </summary> /// <param name="writer"></param> /// <param name="type"></param> public static void WriteTypeMetaInfo(IStreamWriter writer, Type type) { writer.WriteUInt8(ValueItem.TypeMetaInfo); writer.WriteUInt8(SingleTypeDescr); writer.WriteString(type.Assembly.GetName().Name); writer.WriteString(type.FullName); // Write 0 items (only type specification in case of enum) writer.WriteInt16(0); }
public override void WriteValue(IStreamWriter writer, ISerializeContext context, object value) { NullableWrite(writer, value, () => { writer.WriteUInt8((byte)value); }); }
public static void WriteTypeMetaInfo(IStreamWriter writer, CollectionItems collection) { writer.WriteUInt8(ValueItem.TypeMetaInfo); writer.WriteUInt8(CollectionTypeDescr); Type type = collection.RuntimeType; writer.WriteString(type.Assembly.GetName().Name); writer.WriteString(type.FullName); //writer.WriteBool(collection.IsArray); //if (collection.IsArray) //{ //} }
protected void NullableWrite(IStreamWriter writer, object value, Action writeFunc) { if (IsNullable) { if (value == null) { writer.WriteUInt8(ValueItem.NullValueIdent); } else { writer.WriteUInt8(ValueItem.SingleValueIdent); writeFunc(); } } else { writeFunc(); } }
public override void WriteValue(IStreamWriter writer, ISerializeContext context, object value) { // Write ItemType.TypeRef ID //writer.WriteUInt32(this.TypeId); if (value == null) { writer.WriteUInt8(ValueItem.NullValueIdent); } else { Type typeRef = (Type)value; IValueItem item = context.GetByType(typeRef); bool typeMetaInfo = false; if (item is ITypeStructure) { ITypeStructure tStructure = (ITypeStructure)item; if (context.IsWriteTypeMetaInfoRequired(item.TypeId)) { writer.WriteUInt8(ValueItem.TypeMetaInfo); typeMetaInfo = true; } } if (!typeMetaInfo) { writer.WriteUInt8(ValueItem.SingleValueIdent); } writer.WriteUInt32(item.TypeId); if (typeMetaInfo) { // serialize type meta info TypeMetaStructure.WriteTypeMetaInfo(writer, (ITypeStructure)item, false); } } }
/// <summary> /// Writes the value. /// </summary> /// <param name="writer">The writer.</param> /// <param name="value">The value.</param> public override void WriteValue(IStreamWriter writer, ISerializeContext context, object value) { if (value == null) { writer.WriteUInt8(ValueItem.NullValueIdent); } else { string stringValue = (string)value; uint stringHashCode; if (context.Serializer.IsWriteHashStringRequired(stringValue, out stringHashCode)) { writer.WriteUInt8(ValueItem.HashCodeString); writer.WriteString(stringValue); } else { writer.WriteUInt8(ValueItem.SingleValueIdent); } writer.WriteUInt32(stringHashCode); } }
public override void WriteValue(IStreamWriter writer, ISerializeContext context, object value) { // Enum starts with the actual type id writer.WriteUInt32(this.TypeId); if (context.IsWriteTypeMetaInfoRequired(this.TypeId)) { // serialize type meta info at the first time TypeMetaStructure.WriteTypeMetaInfo(writer, this.enumType); } writer.WriteUInt8(ValueItem.SingleValueIdent); NullableWrite(writer, value, () => { writer.WriteInt32((int)value); }); }
public void WriteValue(IStreamWriter writer, ISerializeContext context, object value) { // Complex structure // TypeID UInt32 // ContentType Byte context.Key = this.Name; if (isObject) { // property or collection does not specify a target type // determine object type if (value != null) { Type valType = value.GetType(); IValueItem item; var differentTargetStructure = context.DetermineSpecialInterfaceType(value.GetType(), type); if (differentTargetStructure != null) { item = differentTargetStructure; } else { item = context.GetByType(valType); } if (!item.IsTypePrefixExpected) { // Write type id for value types as well (unknown structure) writer.WriteUInt32(item.TypeId); writer.WriteUInt8(ValueItem.SingleValueIdent); } item.WriteValue(writer, context, value); } else { // write complex object type id for null value writer.WriteInt32((int)ItemType.ComplexObject); writer.WriteUInt8(ValueItem.NullValueIdent); } } else if (value == null) { // Write null writer.WriteUInt32(this.TypeId); writer.WriteUInt8(ValueItem.NullValueIdent); } else { IValueItem differentTargetStructure = null; if (CheckDifferentType) { differentTargetStructure = context.DetermineSpecialInterfaceType(value.GetType(), type); } if (differentTargetStructure != null) { // Special target interface type serialization differentTargetStructure.WriteValue(writer, context, value); } else { // Just write type properties writer.WriteUInt32(this.TypeId); if (context.IsWriteTypeMetaInfoRequired(this.TypeId)) { // serialize type meta info at the first time TypeMetaStructure.WriteTypeMetaInfo(writer, this); } writer.WriteUInt8(ValueItem.SingleObjectIdent); object oldParentObj = context.ParentObject; context.ParentObject = value; for (int itemIndex = 0; itemIndex < items.Count; itemIndex++) { var item = items[itemIndex]; var valueItem = item.GetItemValue(value); item.WriteValue(writer, context, valueItem); } context.ParentObject = oldParentObj; } } }
public void WriteValue(IStreamWriter writer, ISerializeContext context, object value) { // Collection structure: // TypeID UInt32 // ContentType Byte // Count Int32 // Items... if (getter == null) { writer.WriteUInt32(this.TypeId); // only write collection type if element is root object if (context.IsWriteTypeMetaInfoRequired(this.TypeId)) { // serialize type meta info at the first time TypeMetaStructure.WriteTypeMetaInfo(writer, this); } } if (value == null) { writer.WriteUInt8(ValueItem.NullValueIdent); } else { writer.WriteUInt8(ValueItem.CollectionObjectIdent); IEnumerable items; if (isArray) { Array array = (Array)value; writer.WriteInt32(array.Length); items = array; if (isByteArray) { writer.WriteBytes(new ArraySegment <byte>((byte[])array)); return; } } else if (value is ICollection) { ICollection collection = (ICollection)value; writer.WriteInt32(collection.Count); items = collection; } else { items = (IEnumerable)value; int count = 0; foreach (var item in items) { count++; } writer.WriteInt32(count); } context.Key = this.Name; int index = 0; foreach (var item in items) { context.ArrayIndex = index; itemStructure.WriteValue(writer, context, item); index++; } context.ArrayIndex = null; } }