Skip to content

Serializing returns empty text and exception when serializing collections (and the solution) #28

@zevele

Description

@zevele

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:

  1. Thereturn true in line 175 is redundant, causing all properties to be ignored. and results in an empty serialization data.


    Removing these lines solved this issue.

  2. 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:

    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));
    }

    The reason for the exception is that property.type is 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:
    var referenceTarget = property as ReferenceTargetProperty;

    and
    if (referenceTarget.Reference != null)
    {
    if (!referenceTarget.Reference.IsProcessed)
    {
    // object was created already
    // get object from cache
    return _objectCache[referenceTarget.Reference.Id];
    }
    }

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions