Piority option on the JsonProperty attribute

Mar 6, 2011 at 11:06 AM
Edited Mar 6, 2011 at 3:25 PM

Hi,

I'm working on a Facebook project where I'm generating classes based on online documentation. (If you're interested in more about the Facebook stuff I'm working on, read this: http://ondotnet.deap.nu/2011/03/taking-stab-at-generating-strongly.html)

Some of the properties types can't be inferred from the documentation so I fall back on dynamic members and use partial class definitions so that the specific property can be implemented in another file. The following dummy class describes the situation:

[JsonObject(MemberSerialization.OptIn)]
public partial class FooBar
{
#region Think of this as the generated partial class definition

[JsonProperty("foo")]
public dynamic FooDynamic { get; set; }

#endregion


#region Think of this as the hand coded partial class definition

[JsonProperty("foo")]
public string Foo { get; set; }

#endregion
}

 

Now, serializing this class to Json wont work because I'm using the same name in both of the JsonProperty attribute, so, what I would want is the possibility to do something like this:

[JsonObject(MemberSerialization.OptIn)]
public partial class FooBar
{
#region Generated partial class definition

[JsonProperty("foo", PropertyAmbiguityPriority = PropertyAmbiguityPriority.Low)]
public dynamic FooDynamic { get; set; }

#endregion


#region Hand coded partial class definition

[JsonProperty("foo")]
public string Foo { get; set; }

#endregion
}

The PropertyAmbiguityPriority would only be used during serializing if there a two properties with the same JsonProperty name. If property "A" has it set to Low and property "B" has no setting, "B" will be used. If "A" has the setting High and B has no setting, "A" will be used. If both have the same name, and the same PropertyAmbiguitiPriority the same behaviour as now should occur, namely an exception being thrown on serialization. When deserializing both properties should be mapped to the data from the json.

Please let me know your thoughts on this.