polenter / SharpSerializer

SharpSerializer can serialize types like: multidimensional array, nested array, array-of-arrays, polymorphic object (where value is inherited from the property type), generic type, generic listing (i.e. dictionary, collection) and many more, with a single line of code
https://www.sharpserializer.net
Other
114 stars 28 forks source link

NullReferenceException when serializing (using Version 3.0.1) #6

Open tobiasschittkowski opened 6 years ago

tobiasschittkowski commented 6 years ago

I get the following NullReferenceException when trying to serialize to binary (Burst mode):

bei Polenter.Serialization.Advanced.BinaryPropertySerializer.writeProperties(PropertyCollection properties, Type ownerType) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.SerializeComplexProperty(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.serializeReferenceTarget(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.SerializeCore(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.writeProperties(PropertyCollection properties, Type ownerType) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.SerializeComplexProperty(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.serializeReferenceTarget(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.SerializeCore(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.writeItems(ICollection1 items, Type defaultItemType) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.SerializeCollectionProperty(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.serializeReferenceTarget(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.SerializeCore(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.writeProperties(PropertyCollection properties, Type ownerType) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.SerializeComplexProperty(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.serializeReferenceTarget(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.SerializeCore(PropertyTypeInfo`1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.Serialize(Property property) bei Polenter.Serialization.SharpSerializer.Serialize(Object data, Stream stream)

gave92 commented 6 years ago

I get the same error. Using version 2.20.0 works fine.

caioaletroca commented 5 years ago

I'm getting the same error, no clue about whats is causing it. But I can't use 2.20 since I also need .NET 4.7 SharpSerializer really would save me :(

rjpramos commented 4 years ago

In our case the problem was due to the fact that we had private getters and setters (probably bad copy-paste from someone :D).

This caused such properties to be included in the PropertyCollection:

private void writeProperties(PropertyCollection properties, Type ownerType)
{
    // How many
    _writer.WriteNumber(Convert.ToInt16(properties.Count));

    // Serialize all of them
    foreach (Property property in properties)
    {
        PropertyInfo propertyInfo = ownerType.GetProperty(property.Name);
        SerializeCore(new PropertyTypeInfo<Property>(property, propertyInfo.PropertyType));
    }
}

Then in the part ownerType.GetProperty(property.Name) you will just get a null because GetProperty() can't find it.

The null pointer exception comes from propertyInfo.PropertyType.

Removing the private getters and setters solved our problem (but still it would be nice to improve the code of SharpSerializer).

Steph55 commented 4 years ago

Actually, the exception might be caused by a bug that seems to be in SharpSerializer since a long time. It will fail if you try to serialize an array of simple types. For instance, try to serialize this one and you will get the bug:

   public class Test
    {
        public int[] f { get; set; }
        public Test()
        {
            f = new int[] { 1, 2, 3, 4, 5, 6, 7 };
        }
    }