I'm working on project written on C#
with protobuf-net
. I have code which serializes/deserializes Dictionary<MyClass1, MyClass2>
to file . Also, i have a file with serialized data. When i try to deserialize it, i receive an exception key
can't be null
. I don't understand how is it possible since Dictionary
doesn't allows null
, it looks like i couldn't serialize such dictionary. I think the file was corrupted, but i'm not sure. I tried to debug it, and looks like few keys and values were deserialized correctly, but in the middle of the deserialization process, null
key occured and i see exception. I tried to use surrogate for ProductName
as mentioned here but it doesn't help.
How to deserialize this file?
May be there is a way to deserialize to some object instead of null
for ProductName
?
Exception:
System.ArgumentNullException: Value cannot be null. Parameter name: key at System.Collections.Generic.Dictionary
2.Insert(TKey key, TValue value, Boolean add) at System.Collections.Generic.Dictionary
2.System.Collections.Generic.ICollection>.Add(KeyValuePair`2 keyValuePair) at proto_10(Object , ProtoReader ) at ProtoBuf.Meta.TypeModel.DeserializeCore(ProtoReader reader, Type type, Object value, Boolean noAutoCreate) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 704 at ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type, SerializationContext context) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 588 at ProtoBuf.Serializer.Deserialize[T](Stream source) in c:\Dev\protobuf-net\protobuf-net\Serializer.cs:line 77
Code:
[ DataContract ]
public sealed class ProductName
{
[ DataMember( Order = 1 ) ]
public string Name { get; private set; }
public static readonly ProductName Undefined = Create( "Unknown" );
private ProductName()
{
}
private ProductName( string Name )
{
this.Name = Name.Trim();
}
public static ProductName Create( string Name )
{
Condition.Requires( Name, "Name" ).IsNotNullOrEmpty();
return new ProductName( Name );
}
public static ProductName TryCreate( string Name )
{
return Name.IsValidName() ? new ProductName( Name ) : null;
}
public override string ToString()
{
return this.Name;
}
public override int GetHashCode()
{
var stableHashCodeIgnoringCase = this.Name.GetStableHashCodeIgnoringCase();
return stableHashCodeIgnoringCase;
}
#region Equality members
public bool Equals( ProductName other )
{
if( ReferenceEquals( null, other ) )
return false;
if( ReferenceEquals( this, other ) )
return true;
return string.Equals( this.Name, other.Name, StringComparison.InvariantCultureIgnoreCase );
}
public override bool Equals( object obj )
{
if( ReferenceEquals( null, obj ) )
return false;
if( ReferenceEquals( this, obj ) )
return true;
if( obj.GetType() != this.GetType() )
return false;
return this.Equals( ( ProductName )obj );
}
#endregion
}
[ DataContract ]
public class ProductNameIndex
{
[ DataMember( Order = 1 ) ]
public IDictionary< ProductName, ProductId > Products{ get; private set; }
public ProductNameIndex()
{
this.Products = new Dictionary< ProductName, ProductId >();
}
}