-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add property ordering feature (#55586)
- Loading branch information
1 parent
f9076c7
commit 9720219
Showing
7 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...tem.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonPropertyOrderAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Text.Json.Serialization | ||
{ | ||
/// <summary> | ||
/// Specifies the property order that is present in the JSON when serializing. Lower values are serialized first. | ||
/// If the attribute is not specified, the default value is 0. | ||
/// </summary> | ||
/// <remarks>If multiple properties have the same value, the ordering is undefined between them.</remarks> | ||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] | ||
public sealed class JsonPropertyOrderAttribute : JsonAttribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of <see cref="JsonPropertyNameAttribute"/> with the specified order. | ||
/// </summary> | ||
/// <param name="order">The order of the property.</param> | ||
public JsonPropertyOrderAttribute(int order) | ||
{ | ||
Order = order; | ||
} | ||
|
||
/// <summary> | ||
/// The serialization order of the property. | ||
/// </summary> | ||
public int Order { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...braries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/PropertyOrderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
namespace System.Text.Json.Serialization.Tests | ||
{ | ||
public static class PropertyOrderTests | ||
{ | ||
private class MyPoco_BeforeAndAfter | ||
{ | ||
public int B { get; set; } | ||
|
||
[JsonPropertyOrder(1)] | ||
public int A { get; set; } | ||
|
||
[JsonPropertyOrder(-1)] | ||
public int C { get; set; } | ||
} | ||
|
||
[Fact] | ||
public static void BeforeAndAfterDefaultOrder() | ||
{ | ||
string json = JsonSerializer.Serialize<MyPoco_BeforeAndAfter>(new MyPoco_BeforeAndAfter()); | ||
Assert.Equal("{\"C\":0,\"B\":0,\"A\":0}", json); | ||
} | ||
|
||
private class MyPoco_After | ||
{ | ||
[JsonPropertyOrder(2)] | ||
public int C { get; set; } | ||
|
||
public int B { get; set; } | ||
public int D { get; set; } | ||
|
||
[JsonPropertyOrder(1)] | ||
public int A { get; set; } | ||
} | ||
|
||
[Fact] | ||
public static void AfterDefaultOrder() | ||
{ | ||
string json = JsonSerializer.Serialize<MyPoco_After>(new MyPoco_After()); | ||
Assert.EndsWith("\"A\":0,\"C\":0}", json); | ||
// Order of B and D are not defined except they come before A and C | ||
} | ||
|
||
private class MyPoco_Before | ||
{ | ||
[JsonPropertyOrder(-1)] | ||
public int C { get; set; } | ||
|
||
public int B { get; set; } | ||
public int D { get; set; } | ||
|
||
[JsonPropertyOrder(-2)] | ||
public int A { get; set; } | ||
} | ||
|
||
[Fact] | ||
public static void BeforeDefaultOrder() | ||
{ | ||
string json = JsonSerializer.Serialize<MyPoco_Before>(new MyPoco_Before()); | ||
Assert.StartsWith("{\"A\":0,\"C\":0", json); | ||
// Order of B and D are not defined except they come after A and C | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters