private object DeserializeValue(SerializationContext context, RosBinaryReader reader, RosType rosType, Type type) { if (rosType.IsBuiltIn) { return(reader.ReadBuiltInType(rosType, type)); } else { var typeInfo = context.MessageTypeRegistry.GetOrCreateMessageTypeInfo(type); IRosMessageFormatter formatter = this; // If this serializer cannot serialize the object search for serializer who can do it if (!CanSerialize(typeInfo)) { formatter = context.MessageFormatters.FindFormatterFor(typeInfo); if (formatter == null) { throw new NotSupportedException($"No formatter for message {typeInfo} found."); } } return(formatter.Deserialize(context, reader, typeInfo)); } }
public object Deserialize(SerializationContext context, RosBinaryReader reader, IRosMessageTypeInfo messageTypeInfo) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (messageTypeInfo == null) { throw new ArgumentNullException(nameof(messageTypeInfo)); } if (!(messageTypeInfo is DescriptorBasedMessageTypeInfo messageInfo)) { throw new NotSupportedException(); } var result = Activator.CreateInstance(messageInfo.Type); var fields = messageInfo.MessageDescriptor.Fields; foreach (var field in fields) { try { object fieldValue; if (field.RosType.IsArray) { fieldValue = DeserializeArray(context, reader, field.RosType, field.Type); } else { fieldValue = DeserializeValue(context, reader, field.RosType, field.Type); } field.SetValue(result, fieldValue); } catch (Exception e) { if (e is RosFieldSerializationException rosException) { rosException.AddLeadingRosIdentifier(field.RosIdentifier); throw; } else { throw new RosFieldSerializationException(RosFieldSerializationException.SerializationOperation.Deserialize, field.RosIdentifier, e); } } } return(result); }
public object Deserialize(Type messageType, Stream input) { if (messageType == null) { throw new ArgumentNullException(nameof(messageType)); } if (input == null) { throw new ArgumentNullException(nameof(input)); } var messageTypeInfo = MessageTypeRegistry.GetOrCreateMessageTypeInfo(messageType); var formatter = GetFormatter(messageTypeInfo); var context = new SerializationContext(input, MessageFormatters, MessageTypeRegistry); var reader = new RosBinaryReader(input); return(formatter.Deserialize(context, reader, messageTypeInfo)); }
private object DeserializeArray(SerializationContext context, RosBinaryReader reader, RosType rosType, Type memberType) { int length; if (rosType.IsFixedSizeArray) { length = rosType.ArraySize; } else { length = reader.ReadInt32(); } var elementType = GetGenericElementType(memberType); var listType = typeof(List <>).MakeGenericType(elementType); var list = (IList)Activator.CreateInstance(listType); for (var i = 0; i < length; i++) { try { var item = DeserializeValue(context, reader, rosType, elementType); list.Add(item); } catch (Exception e) { var indexString = $"[{i}]"; if (e is RosFieldSerializationException rosException) { rosException.AddLeadingRosIdentifier(indexString); throw; } else { throw new RosFieldSerializationException(RosFieldSerializationException.SerializationOperation.Deserialize, indexString, e); } } } return(ConvertListToMemberType(list, memberType)); }