Serializing returns empty text and exception when serializing collections (and the solution)
Hi I've tried using the nuget, but there is some weird behavior with collections. So I downloaded the code, there I found two issues:
-
The
return truein line 175 is redundant, causing all properties to be ignored. and results in an empty serialization data. https://github.com/polenter/SharpSerializer/blob/0781e02fb7f5943cdc7ecffe1b5bc080c912837b/SharpSerializer/Advanced/PropertyProvider.cs#L173-L175 Removing these lines solved this issue. -
My issue with collections and references. When deserialized a collection that includes a reference to an object that was already added I got an exception with the following message:
Property type is not defined. Property: ""which is thrown here: https://github.com/polenter/SharpSerializer/blob/0781e02fb7f5943cdc7ecffe1b5bc080c912837b/SharpSerializer/Deserializing/ObjectFactory.cs#L82-L87 The reason for the exception is thatproperty.typeis not defined because in the xml string (I guess the same happens also with the binary serializer) there is no type defined for this property, and there shouldn't be one - because it was already defined for the reference object. So my solution was to move the reference part: https://github.com/polenter/SharpSerializer/blob/0781e02fb7f5943cdc7ecffe1b5bc080c912837b/SharpSerializer/Deserializing/ObjectFactory.cs#L96 and https://github.com/polenter/SharpSerializer/blob/0781e02fb7f5943cdc7ecffe1b5bc080c912837b/SharpSerializer/Deserializing/ObjectFactory.cs#L100-L108
to before line 82 - this solved this issue.
So the new code looks like this:
// Is it NullProperty?
var nullProperty = property as NullProperty;
if (nullProperty != null)
{
return null;
}
var referenceTarget = property as ReferenceTargetProperty;
if (referenceTarget!=null && referenceTarget.Reference != null)
{
if (!referenceTarget.Reference.IsProcessed)
{
// object was created already
// get object from cache
return _objectCache[referenceTarget.Reference.Id];
}
}
if (property.Type == null)
{
// there is no property type and no expected type defined. Give up!
throw new InvalidOperationException(string.Format("Property type is not defined. Property: \"{0}\"",
property.Name));
}
// Is it SimpleProperty?
var simpleProperty = property as SimpleProperty;
if (simpleProperty != null)
{
return createObjectFromSimpleProperty(simpleProperty);
}
if (referenceTarget == null)
return null;
object value = createObjectCore(referenceTarget);
if (value != null)
return value;
I don't know how to upload these changes to the code, so I simply outlining this here.