diff --git a/docs/standard/serialization/system-text-json/customize-properties.md b/docs/standard/serialization/system-text-json/customize-properties.md index 7c6d79834124f..e36b2ec9ff0f2 100644 --- a/docs/standard/serialization/system-text-json/customize-properties.md +++ b/docs/standard/serialization/system-text-json/customize-properties.md @@ -233,7 +233,7 @@ By default, properties are serialized in the order in which they're defined in t :::code language="csharp" source="snippets/how-to-6-0/csharp/PropertyOrder.cs"::: -## Use Github Copilot to customize property names and order +## Use Github Copilot to customize property names and order You can use GitHub Copilot in your IDE to generate code to customize names and order of serialized properties. @@ -246,16 +246,19 @@ The following example shows you how to use Copilot to modify existing code to cu 1. Add the following C# example code to a code file `Example.cs` in your editor. In Visual Studio, you can use a C# console application project to try this example. - + :::code language="csharp" source="snippets/how-to-6-0/csharp/copilot-example.cs"::: `Example.cs` code does the following: + - Creates an instance of the `Person` class and initializes its properties with values. - Serializes the `person` object to a JSON string using `JsonSerializer.Serialize`. - Prints the following JSON string to the console: - ```json - {"FirstName":"John","LastName":"Doe","Age":30,"Country":"USA"} - ``` + + ```json + {"FirstName":"John","LastName":"Doe","Age":30,"Country":"USA"} + ``` + 1. In Copilot Chat, enter the following prompt to modify the code to customize names and order of the JSON serialization output. > `#Example.cs` modify code to use `System.Text.Json` to customize property names and order of JSON output from serialization. Set property names: FirstName to first_name, LastName to last_name. Set order to: Country, FirstName, LastName, Age. Provide customized serialization output. @@ -305,13 +308,14 @@ The following example shows you how to use Copilot to modify existing code to cu } **Explanation:** - 1. Renaming Properties: The JsonPropertyName attribute is used to rename FirstName to first_name and LastName to last_name. - 2. Ordering Properties: The JsonPropertyOrder attribute is used to set the order of the properties in the JSON output. The order is set as: - • Country (Order 1) - • FirstName (Order 2) - • LastName (Order 3) - • Age (Order 4) - 3. Indented JSON: The JsonSerializerOptions with WriteIndented = true is used to make the JSON output more readable. + + 1. Renaming Properties: The JsonPropertyName attribute is used to rename FirstName to first_name and LastName to last_name. + 2. Ordering Properties: The JsonPropertyOrder attribute is used to set the order of the properties in the JSON output. The order is set as: + • Country (Order 1) + • FirstName (Order 2) + • LastName (Order 3) + • Age (Order 4) + 3. Indented JSON: The JsonSerializerOptions with WriteIndented = true is used to make the JSON output more readable. **Customized Serialization Output:** { @@ -323,7 +327,7 @@ The following example shows you how to use Copilot to modify existing code to cu This ensures that the JSON output has the customized property names and the specified order. ``` - + When Copilot returns a code block, the response includes options to copy the code, insert the code into a new file, or preview the code output. > [!NOTE]