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

GetClosestMatchProperty makes deserialization of backingfield with getter fail #16

Closed
asgerhallas opened this issue Apr 27, 2012 · 5 comments

Comments

@asgerhallas
Copy link

Hi

The following test shows what might be a bug:

[TestFixture]
public class FallbackToCaseInsensitiveFailsForThisCaseTest
{
    [Test]
    public void CanSerializerAndDeserializeToAndFromFieldWithGetterOfOtherType()
    {
        var serializer = new JsonSerializer()
        {
            ContractResolver = new CR(),
            TypeNameHandling = TypeNameHandling.All
        };

        var stringBuilder = new StringBuilder();
        var textWriter = new StringWriter(stringBuilder);
        serializer.Serialize(textWriter, new SomethingWithAList());

        var stringReader = new StringReader(textWriter.ToString());
        var jsonTextReader = new JsonTextReader(stringReader);
        serializer.Deserialize<SomethingWithAList>(jsonTextReader);
    }

    public class CR : DefaultContractResolver
    {
        public CR()
            : base(true)
        {
            DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
        }
    }

    public class SomethingWithAList
    {
        readonly List<int> ints = new List<int>();
        public IEnumerable<int> Ints
        {
            get { return ints.Where(x => x > 10); }
        }
    }
}      

Would it be possible to make an option to disable the case insensitive property name match?

Regards, Asger

@asgerhallas
Copy link
Author

Ok, it does not look like it has anything to do with GetClosestMatchProperty()... Hmm... I'm investegting further, it looks like two different, but similar behaviours I've encountered.

@asgerhallas
Copy link
Author

Ok. I've encountered two different issues.

The Second issue is actually related to GetClosestMatchProperty, when you have a json document with a property that is no longer present in the new representation of the .net class (migrating documents from a Raven database).

The test for this is:

[TestFixture]
public class FallbackToCaseInsensitiveFailsForThisCaseTest
{
    [Test]
    public void CanDeserializeFromJsonWhereTwoPropertyAreSimilarlyNamed()
    {
        var settings = new JsonSerializerSettings
                                         {
                                             ContractResolver = new PrivatesOnly(), 
                                             TypeNameHandling = TypeNameHandling.All
                                         };

        JsonConvert.DeserializeObject<SomethingWithAList>(
        @"
        {      
        ""ints"": {
                  ""$id"": ""469"",
                  ""$type"": ""System.Collections.Generic.List`1[[System.Int32, mscorlib]], mscorlib"",
                  ""$values"": []
                },
                ""Ints"": {
                  ""$id"": ""470"",
                  ""$type"": ""System.Int32[], mscorlib"",
                  ""$values"": []
                }
        }
        ", settings);
    }

    public class PrivatesOnly : DefaultContractResolver
    {
        public PrivatesOnly()
            : base(false)
        {
            DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.NonPublic;
        }
    }

    public class SomethingWithAList
    {
        readonly List<int> ints = new List<int>();
        public IEnumerable<int> Ints
        {
            get { return ints.Where(x => x > 10); }
        }
    }
}

Would you like a pull request with the failing tests? Or is here ok?

@asgerhallas
Copy link
Author

If you even consider them bugs, that is :)

The first one seems to be solvable by some settings - when I get the serializer through RavenDb the issue goes away. But I can't figure out why.

The second one is happening, because I try to deserialize with another contract resolver than I serialized with, so it might not be such a valid case. But it would still be nice to be able to disable the fallback to case insensitive properties and just ignore if a property is missing.

@JamesNK
Copy link
Owner

JamesNK commented May 27, 2012

I believe you could override this by creating your own contract resolver.

@JamesNK JamesNK closed this as completed May 27, 2012
@andresmoschini
Copy link

@asgerhallas did you implement your own contract resolver? (I need case sensitive deserialization)

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

3 participants