Skip to content

Commit 68f85b6

Browse files
authored
Update Copilot example (#46031)
1 parent 76e0b50 commit 68f85b6

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

docs/standard/serialization/system-text-json/customize-properties.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: How to customize property names and values with System.Text.Json
33
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
55
no-loc: [System.Text.Json, Newtonsoft.Json]
66
dev_langs:
77
- "csharp"
@@ -29,7 +29,7 @@ By default, property names and dictionary keys are unchanged in the JSON output,
2929
> The [web default](configure-options.md#web-defaults-for-jsonserializeroptions) naming policy is camel case.
3030
3131
> [!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).
3333
3434
For other scenarios that require special handling of JSON property names and values, you can [implement custom converters](converters-how-to.md).
3535

@@ -243,35 +243,35 @@ By default, properties are serialized in the order in which they're defined in t
243243

244244
:::code language="csharp" source="snippets/how-to-6-0/csharp/PropertyOrder.cs":::
245245

246-
## Use GitHub Copilot to customize property names and order
246+
## Use GitHub Copilot to customize how property names are serialized
247247

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.
249249

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:
251251

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+
```
266258

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.
268260

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+
```
275275

276276
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).
277277

0 commit comments

Comments
 (0)