-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
53 additions
and
4 deletions.
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
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
39 changes: 39 additions & 0 deletions
39
...-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodePropertyOrder.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,39 @@ | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using System.Text.Json.Schema; | ||
|
||
namespace JsonNodePropertyOrder; | ||
|
||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
// <Snippet1> | ||
var schema = (JsonObject)JsonSerializerOptions.Default.GetJsonSchemaAsNode(typeof(MyPoco)); | ||
|
||
JsonNode? idValue; | ||
switch (schema.IndexOf("$id")) | ||
{ | ||
// $id property missing. | ||
case < 0: | ||
idValue = (JsonNode)"https://example.com/schema"; | ||
schema.Insert(0, "$id", idValue); | ||
break; | ||
|
||
// $id property already at the start of the object. | ||
case 0: | ||
break; | ||
|
||
// $id exists but not at the start of the object. | ||
case int index: | ||
idValue = schema[index]; | ||
schema.RemoveAt(index); | ||
schema.Insert(0, "$id", idValue); | ||
break; | ||
} | ||
// </Snippet1> | ||
} | ||
} | ||
|
||
class MyPoco { } |
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