private static KeyValuePair <Tuple <Type, string>, Dictionary <PacketIndexAttribute, PropertyInfo> > GenerateSerializationInformations(Type serializationType) { string header = serializationType.GetCustomAttribute <PacketHeaderAttribute>()?.Identification; if (string.IsNullOrEmpty(header)) { throw new Exception($"Packet header cannot be empty. PacketType: {serializationType.Name}"); } Dictionary <PacketIndexAttribute, PropertyInfo> packetsForPacketDefinition = new Dictionary <PacketIndexAttribute, PropertyInfo>(); foreach (PropertyInfo packetBasePropertyInfo in serializationType.GetProperties().Where(x => x.GetCustomAttributes(false).OfType <PacketIndexAttribute>().Any())) { PacketIndexAttribute indexAttribute = packetBasePropertyInfo.GetCustomAttributes(false).OfType <PacketIndexAttribute>().FirstOrDefault(); if (indexAttribute != null) { packetsForPacketDefinition.Add(indexAttribute, packetBasePropertyInfo); } } // order by index IOrderedEnumerable <KeyValuePair <PacketIndexAttribute, PropertyInfo> > keyValuePairs = packetsForPacketDefinition.OrderBy(p => p.Key.Index); KeyValuePair <Tuple <Type, string>, Dictionary <PacketIndexAttribute, PropertyInfo> > serializationInformatin = new KeyValuePair <Tuple <Type, string>, Dictionary <PacketIndexAttribute, PropertyInfo> >(new Tuple <Type, string>(serializationType, header), packetsForPacketDefinition); _packetSerializationInformations.Add(serializationInformatin.Key, serializationInformatin.Value); return(serializationInformatin); }
private static string SerializeValue(Type propertyType, object value, PacketIndexAttribute packetIndexAttribute = null) { if (propertyType != null) { if (packetIndexAttribute != null && packetIndexAttribute.IsOptional && string.IsNullOrEmpty(Convert.ToString(value))) { return(string.Empty); } // check for nullable without value or string if (propertyType.Equals(typeof(string)) && (string.IsNullOrEmpty(Convert.ToString(value)))) { return(" -"); } if (Nullable.GetUnderlyingType(propertyType) != null && string.IsNullOrEmpty(Convert.ToString(value))) { return(" -1"); } // enum should be casted to number if (propertyType.BaseType != null && propertyType.BaseType.Equals(typeof(System.Enum))) { return($" {Convert.ToInt16(value)}"); } if (propertyType.Equals(typeof(bool))) { // bool is 0 or 1 not True or False return(Convert.ToBoolean(value) ? " 1" : " 0"); } if (propertyType.BaseType != null && propertyType.BaseType.Equals(typeof(PacketDefinition))) { KeyValuePair <Tuple <Type, string>, Dictionary <PacketIndexAttribute, PropertyInfo> > subpacketSerializationInfo = GetSerializationInformation(propertyType); return(SerializeSubpacket(value, subpacketSerializationInfo, packetIndexAttribute?.IsReturnPacket ?? false, packetIndexAttribute?.RemoveSeparator ?? false)); } if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>)) && propertyType.GenericTypeArguments[0].BaseType.Equals(typeof(PacketDefinition))) { return(SerializeSubpackets((IList)value, propertyType, packetIndexAttribute?.RemoveSeparator ?? false)); } if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>))) //simple list { return(SerializeSimpleList((IList)value, propertyType)); } return($" {value}"); } return(string.Empty); }
private static string SerializeValue(Type propertyType, object value, IEnumerable <ValidationAttribute> validationAttributes, PacketIndexAttribute packetIndexAttribute) { if (propertyType == null || !validationAttributes.All(a => a.IsValid(value))) { return(string.Empty); } if ((packetIndexAttribute?.IsOptional ?? false) && string.IsNullOrEmpty(Convert.ToString(value))) { return(string.Empty); } // check for nullable without value or string if (propertyType == typeof(string) && string.IsNullOrEmpty(Convert.ToString(value))) { return(" -"); } if (Nullable.GetUnderlyingType(propertyType) != null && string.IsNullOrEmpty(Convert.ToString(value))) { return(" -1"); } // enum should be casted to number if ((propertyType.BaseType?.Equals(typeof(Enum)) ?? false) || (Nullable.GetUnderlyingType(propertyType)?.IsEnum ?? false)) { return($" {Convert.ToInt16(value)}"); } if (propertyType == typeof(bool)) { // bool is 0 or 1 not True or False return(Convert.ToBoolean(value) ? " 1" : " 0"); } if (propertyType.BaseType?.Equals(typeof(PacketDefinition)) ?? false) { var subpacketSerializationInfo = GetSerializationInformation(propertyType); return(SerializeSubpacket(value, subpacketSerializationInfo, packetIndexAttribute?.IsReturnPacket ?? false, packetIndexAttribute?.SpecialSeparator)); } if (value is PacketDefinition) { var subpacketSerializationInfo = GetSerializationInformation(value.GetType()); return(SerializeSubpacket(value, subpacketSerializationInfo, packetIndexAttribute?.IsReturnPacket ?? false, packetIndexAttribute?.SpecialSeparator)); } if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>)) && propertyType.GenericTypeArguments[0].BaseType == typeof(PacketDefinition)) { return(SerializeSubpackets((IList)value, propertyType, packetIndexAttribute?.SpecialSeparator)); } if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>))) //simple list { return(SerializeSimpleList((IList)value, propertyType)); } return($" {value}"); }
private static object DeserializeValue(Type packetPropertyType, string currentValue, PacketIndexAttribute packetIndexAttribute, IEnumerable <ValidationAttribute> validationAttributes, MatchCollection packetMatches, bool includesKeepAliveIdentity = false) { if (string.IsNullOrEmpty(currentValue) && packetIndexAttribute.IsOptional) { return(null); } var value = currentValue; validationAttributes.ToList().ForEach(s => { if (!s.IsValid(value)) { throw new ValidationException(s.ErrorMessage); } }); // check for empty value and cast it to null if (currentValue == "-1" || currentValue == "-" || currentValue == "NONE") { currentValue = null; } // enum should be casted to number if (packetPropertyType.BaseType?.Equals(typeof(Enum)) ?? false) { object convertedValue = null; try { if (currentValue != null && packetPropertyType.IsEnumDefined(Enum.Parse(packetPropertyType, currentValue))) { convertedValue = Enum.Parse(packetPropertyType, currentValue); } } catch (Exception ex) { _logger.Warning(LogLanguage.Instance.GetMessageFromKey(LanguageKey.NOT_CONVERT_VALUE), currentValue, packetPropertyType.Name, ex); } return(convertedValue); } if (packetPropertyType == typeof(bool)) // handle boolean values { return(currentValue != "0"); } if (packetPropertyType.BaseType?.Equals(typeof(PacketDefinition)) ?? false) // subpacket { var subpacketSerializationInfo = GetSerializationInformation(packetPropertyType); return(DeserializeSubpacket(currentValue, packetPropertyType, subpacketSerializationInfo, packetIndexAttribute?.IsReturnPacket ?? false)); } if (packetPropertyType.IsGenericType && packetPropertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>)) && // subpacket list packetPropertyType.GenericTypeArguments[0].BaseType == typeof(PacketDefinition)) { return(DeserializeSubpackets(currentValue, packetPropertyType, packetMatches, packetIndexAttribute?.Index, includesKeepAliveIdentity, packetIndexAttribute?.SpecialSeparator)); } if (packetPropertyType.IsGenericType && packetPropertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>))) // simple list { return(DeserializeSimpleList(currentValue, packetPropertyType)); } if (Nullable.GetUnderlyingType(packetPropertyType) != null && string.IsNullOrEmpty(currentValue) ) // empty nullable value { return(null); } if (Nullable.GetUnderlyingType(packetPropertyType) != null) // nullable value { if (packetPropertyType.GenericTypeArguments[0]?.BaseType == typeof(Enum)) { return(Enum.Parse(packetPropertyType.GenericTypeArguments[0], currentValue)); } return(TypeDescriptor.GetConverter(packetPropertyType.GenericTypeArguments[0]) .ConvertFromInvariantString(currentValue)); } if (packetPropertyType == typeof(string) && string.IsNullOrEmpty(currentValue) && !packetIndexAttribute.SerializeToEnd) { throw new ArgumentNullException(nameof(currentValue)); } if (packetPropertyType == typeof(string) && currentValue == null) { currentValue = string.Empty; } return(Convert.ChangeType(currentValue, packetPropertyType)); // cast to specified type }
private static object DeserializeValue(Type packetPropertyType, string currentValue, PacketIndexAttribute packetIndexAttribute, IEnumerable <ValidationAttribute> validationAttributes, MatchCollection packetMatches, bool includesKeepAliveIdentity = false) { var value = currentValue; validationAttributes.ToList().ForEach(s => { if (!s.IsValid(value)) { throw new ValidationException(s.ErrorMessage); } }); // check for empty value and cast it to null if (currentValue == "-1" || currentValue == "-") { currentValue = null; } // enum should be casted to number if (packetPropertyType.BaseType?.Equals(typeof(Enum)) == true) { object convertedValue = null; try { if (currentValue != null && packetPropertyType.IsEnumDefined(Enum.Parse(packetPropertyType, currentValue))) { convertedValue = Enum.Parse(packetPropertyType, currentValue); } } catch (Exception) { Logger.Log.Warn($"Could not convert value {currentValue} to type {packetPropertyType.Name}"); } return(convertedValue); } if (packetPropertyType.Equals(typeof(bool))) // handle boolean values { return(currentValue != "0"); } if (packetPropertyType.BaseType?.Equals(typeof(PacketDefinition)) == true) // subpacket { var subpacketSerializationInfo = GetSerializationInformation(packetPropertyType); return(DeserializeSubpacket(currentValue, packetPropertyType, subpacketSerializationInfo, packetIndexAttribute?.IsReturnPacket ?? false)); } if (packetPropertyType.IsGenericType && packetPropertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>)) && // subpacket list packetPropertyType.GenericTypeArguments[0].BaseType.Equals(typeof(PacketDefinition))) { return(DeserializeSubpackets(currentValue, packetPropertyType, packetIndexAttribute?.RemoveSeparator ?? false, packetMatches, packetIndexAttribute?.Index, includesKeepAliveIdentity)); } if (packetPropertyType.IsGenericType && packetPropertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>))) // simple list { return(DeserializeSimpleList(currentValue, packetPropertyType)); } if (Nullable.GetUnderlyingType(packetPropertyType) != null && string.IsNullOrEmpty(currentValue) ) // empty nullable value { return(null); } if (Nullable.GetUnderlyingType(packetPropertyType) != null) // nullable value { if (packetPropertyType.GenericTypeArguments[0]?.BaseType.Equals(typeof(Enum)) == true) { return(Enum.Parse(packetPropertyType.GenericTypeArguments[0], currentValue)); } return(Convert.ChangeType(currentValue, packetPropertyType.GenericTypeArguments[0])); } if (packetPropertyType == typeof(string) && string.IsNullOrEmpty(currentValue) && !packetIndexAttribute.SerializeToEnd) { throw new NullReferenceException(); } if (packetPropertyType == typeof(string) && currentValue == null) { currentValue = string.Empty; } return(Convert.ChangeType(currentValue, packetPropertyType)); // cast to specified type }