Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zevele opened this issue Aug 31, 2022 · 0 comments

Comments

@zevele
Copy link

zevele commented Aug 31, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant