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

ContractResolvers can alter mapped properties #314

Closed
MisinformedDNA opened this issue Jun 23, 2014 · 1 comment
Closed

ContractResolvers can alter mapped properties #314

MisinformedDNA opened this issue Jun 23, 2014 · 1 comment

Comments

@MisinformedDNA
Copy link

If I have a mapped property and am using a custom contract resolver, then the contract resolver can alter the mapped property.

EXPECTED: The mapped property is exactly what I specified.

using DragonDoor.Services.Misc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace DeserializeTest
{
    class Program
    {
        static void Main()
        {
            var json = new JObject(new JProperty("hello", "world"));

            var settings = new JsonSerializerSettings { ContractResolver = new CustomContractResolver() };
           var a = JsonConvert.DeserializeObject<Test>(json.ToString(), settings);
        }
    }

    public class Test
    {
        [JsonProperty("hello")]
        public string FooBar { get; set; }
    }

    public class CustomContractResolver : DefaultContractResolver
    {
        protected override string ResolvePropertyName(string propertyName)
        {
            return propertyName + "_";
        }
    }
}

The issue here is that I want every property that doesn't have a JsonPropertyAttribute specified, to follow the custom contract. However, if I specify the attribute then that is exactly what it should use.

Real world example: I am using the SnakeCamelCaseContractResolver. It puts an underscore in between text and numbers. This is supposed to mimic how Rails serializes, so I don't want to change their implementation. So I figure I can just add JsonProperty("address1"), an instance where the 3rd party does not follow the norm. But the contract resolver keeps changing the property name to address_1. It seems like if I specify what property I want, I should get that property, right?

@JamesNK
Copy link
Owner

JamesNK commented Jul 1, 2014

Override a higher level method.

@JamesNK JamesNK closed this as completed Jul 1, 2014
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

2 participants