-Added Order to JsonPropertyAttribute to override the order of serialized JSON
This commit is contained in:
@@ -4691,6 +4691,7 @@ keyword such as type of business.""
|
||||
public Boolean WheelchairAccessible { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeGenericEnumerableProperty()
|
||||
{
|
||||
BusRun r = JsonConvert.DeserializeObject<BusRun>("{\"Departures\":[\"\\/Date(1309874148734-0400)\\/\",\"\\/Date(1309874148739-0400)\\/\",null],\"WheelchairAccessible\":true}");
|
||||
@@ -4700,5 +4701,52 @@ keyword such as type of business.""
|
||||
Assert.IsNotNull(r.Departures.ElementAt(1));
|
||||
Assert.IsNull(r.Departures.ElementAt(2));
|
||||
}
|
||||
|
||||
#if !(NET20)
|
||||
[DataContract]
|
||||
public class BaseType
|
||||
{
|
||||
|
||||
[DataMember]
|
||||
public string zebra;
|
||||
}
|
||||
|
||||
[DataContract]
|
||||
public class DerivedType : BaseType
|
||||
{
|
||||
[DataMember(Order = 0)]
|
||||
public string bird;
|
||||
[DataMember(Order = 1)]
|
||||
public string parrot;
|
||||
[DataMember]
|
||||
public string dog;
|
||||
[DataMember(Order = 3)]
|
||||
public string antelope;
|
||||
[DataMember]
|
||||
public string cat;
|
||||
[JsonProperty(Order = 1)]
|
||||
public string albatross;
|
||||
[JsonProperty(Order = -2)]
|
||||
public string dinosaur;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JsonPropertyDataMemberOrder()
|
||||
{
|
||||
DerivedType d = new DerivedType();
|
||||
string json = JsonConvert.SerializeObject(d, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""dinosaur"": null,
|
||||
""dog"": null,
|
||||
""cat"": null,
|
||||
""zebra"": null,
|
||||
""bird"": null,
|
||||
""parrot"": null,
|
||||
""albatross"": null,
|
||||
""antelope"": null
|
||||
}", json);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ namespace Newtonsoft.Json
|
||||
internal ObjectCreationHandling? _objectCreationHandling;
|
||||
internal TypeNameHandling? _typeNameHandling;
|
||||
internal bool? _isReference;
|
||||
internal int? _order;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the null value handling used when serializing this property.
|
||||
@@ -77,6 +78,16 @@ namespace Newtonsoft.Json
|
||||
set { _isReference = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the order of serialization and deserialization of a member.
|
||||
/// </summary>
|
||||
/// <value>The numeric order of serialization or deserialization.</value>
|
||||
public int Order
|
||||
{
|
||||
get { return _order ?? default(int); }
|
||||
set { _order = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the name of the property.
|
||||
/// </summary>
|
||||
|
||||
@@ -753,7 +753,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
properties.AddProperty(property);
|
||||
}
|
||||
|
||||
return properties;
|
||||
IList<JsonProperty> orderedProperties = properties.OrderBy(p => p.Order ?? -1).ToList();
|
||||
return orderedProperties;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -830,13 +831,21 @@ namespace Newtonsoft.Json.Serialization
|
||||
property.UnderlyingName = name;
|
||||
|
||||
if (propertyAttribute != null)
|
||||
{
|
||||
property.Required = propertyAttribute.Required;
|
||||
property.Order = propertyAttribute._order;
|
||||
}
|
||||
#if !PocketPC && !NET20
|
||||
else if (dataMemberAttribute != null)
|
||||
{
|
||||
property.Required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default;
|
||||
property.Order = (dataMemberAttribute.Order != -1) ? (int?) dataMemberAttribute.Order : null;
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
property.Required = Required.Default;
|
||||
}
|
||||
|
||||
property.Ignored = (hasIgnoreAttribute ||
|
||||
(memberSerialization == MemberSerialization.OptIn
|
||||
|
||||
@@ -38,6 +38,12 @@ namespace Newtonsoft.Json.Serialization
|
||||
/// <value>The name of the property.</value>
|
||||
public string PropertyName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the order of serialization and deserialization of a member.
|
||||
/// </summary>
|
||||
/// <value>The numeric order of serialization or deserialization.</value>
|
||||
public int? Order { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the name of the underlying member or parameter.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user