|
1 | 1 | ---
|
2 | 2 | title: How to customize property names and values with System.Text.Json
|
3 | 3 | description: "Learn how to customize property names and values when serializing with System.Text.Json in .NET."
|
4 |
| -ms.date: 02/11/2025 |
| 4 | +ms.date: 05/06/2025 |
5 | 5 | no-loc: [System.Text.Json, Newtonsoft.Json]
|
6 | 6 | dev_langs:
|
7 | 7 | - "csharp"
|
@@ -29,7 +29,7 @@ By default, property names and dictionary keys are unchanged in the JSON output,
|
29 | 29 | > The [web default](configure-options.md#web-defaults-for-jsonserializeroptions) naming policy is camel case.
|
30 | 30 |
|
31 | 31 | > [!TIP]
|
32 |
| -> You can use AI assistance to [customize property names and values with GitHub Copilot](#use-github-copilot-to-customize-property-names-and-order). |
| 32 | +> You can use AI assistance to [create an object with custom serialization properties with GitHub Copilot](#use-github-copilot-to-customize-how-property-names-are-serialized). |
33 | 33 |
|
34 | 34 | For other scenarios that require special handling of JSON property names and values, you can [implement custom converters](converters-how-to.md).
|
35 | 35 |
|
@@ -243,35 +243,35 @@ By default, properties are serialized in the order in which they're defined in t
|
243 | 243 |
|
244 | 244 | :::code language="csharp" source="snippets/how-to-6-0/csharp/PropertyOrder.cs":::
|
245 | 245 |
|
246 |
| -## Use GitHub Copilot to customize property names and order |
| 246 | +## Use GitHub Copilot to customize how property names are serialized |
247 | 247 |
|
248 |
| -You can use GitHub Copilot in your IDE to generate code to customize names and order of serialized properties. You can customize the prompt to output a JSON string with property names and values that suit your requirements. |
| 248 | +You can prompt GitHub Copilot to apply patterns of changes to how your code serializes. |
249 | 249 |
|
250 |
| -The following example shows you how to use Copilot to modify existing code to customize property names and order when serializing to JSON. |
| 250 | +Suppose your class declaration has properties that follow `PascalCasing`, and the JSON standard for your project is `snake_casing`. You can use AI to add the necessary [[JsonPropertyName]](xref:System.Text.Json.Serialization.JsonPropertyNameAttribute) attributes to every property in your class. You can use Copilot to make these changes with a chat prompt like this: |
251 | 251 |
|
252 |
| -1. Add the following C# example code to a code file `Example.cs` in your editor. |
253 |
| - In Visual Studio, you can use a C# console application project to try this example. |
254 |
| - |
255 |
| - :::code language="csharp" source="snippets/how-to-6-0/csharp/copilot-example.cs"::: |
256 |
| - |
257 |
| - `Example.cs` code does the following: |
258 |
| - |
259 |
| - - Creates an instance of the `Person` class and initializes its properties with values. |
260 |
| - - Serializes the `person` object to a JSON string using `JsonSerializer.Serialize`. |
261 |
| - - Prints the following JSON string to the console: |
262 |
| - |
263 |
| - ```json |
264 |
| - {"FirstName":"John","LastName":"Doe","Age":30,"Country":"USA"} |
265 |
| - ``` |
| 252 | +```copilot-prompt |
| 253 | +Update #ClassName: |
| 254 | +when the property name contains more than one word, |
| 255 | +change the serialized property name to use underscores between words. |
| 256 | +Use built-in serialization attributes. |
| 257 | +``` |
266 | 258 |
|
267 |
| -1. In Copilot Chat, enter the following prompt to modify the code to customize names and order of the JSON serialization output. |
| 259 | +Here's a more complete version of the example that includes a simple class. |
268 | 260 |
|
269 |
| - ```copilot-prompt |
270 |
| - #Example.cs modify code to use System.Text.Json to customize property names and order of JSON output from serialization. |
271 |
| - Set property names: FirstName to first_name, LastName to last_name. |
272 |
| - Set order to: Country, FirstName, LastName, Age. |
273 |
| - Provide customized serialization output. |
274 |
| - ``` |
| 261 | +```copilot-prompt |
| 262 | +Take this C# class: |
| 263 | +public class WeatherForecast |
| 264 | +{ |
| 265 | + public DateTime Date { get; set; } |
| 266 | + public int TemperatureC { get; set; } |
| 267 | + public int TemperatureF { get; set; } |
| 268 | + public string? Summary { get; set; } |
| 269 | + public int WindSpeed { get; set; } |
| 270 | +} |
| 271 | +When the property name contains more than one word, |
| 272 | +change the serialized property name to use underscores between words. |
| 273 | +Use built-in serialization attributes. |
| 274 | +``` |
275 | 275 |
|
276 | 276 | GitHub Copilot is powered by AI, so surprises and mistakes are possible. For more information, see [Copilot FAQs](https://aka.ms/copilot-general-use-faqs).
|
277 | 277 |
|
|
0 commit comments