Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Copilot use case #42625

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions docs/standard/serialization/system-text-json/customize-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ helpviewer_keywords:
- "serialization"
- "objects, serializing"
ms.topic: how-to
ms.collection: ce-skilling-ai-copilot
ms.custom: vs-copilot-horizontal
---

# How to customize property names and values with System.Text.Json
Expand All @@ -28,6 +30,9 @@ By default, property names and dictionary keys are unchanged in the JSON output,
> [!NOTE]
> The [web default](configure-options.md#web-defaults-for-jsonserializeroptions) naming policy is camel case.

> [!TIP]
> You can use AI assistance to [customize property names and values with GitHub Copilot](#use-github-copilot-to-customize-property-names-and-order).

For other scenarios that require special handling of JSON property names and values, you can [implement custom converters](converters-how-to.md).

## Customize individual property names
Expand Down Expand Up @@ -228,7 +233,108 @@ 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

You can use GitHub Copilot in your IDE to generate code to customzie names and order of serialized properties.

If you're using [Visual Studio 2022 version 17.8 or later](/visualstudio/releases/2022/release-notes), you can try the AI-driven [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states) to generate code that uses `System.Text.Json` to customize property names and order in the JSON output from serializtion. Submit your question as a prompt in the Copilot chat window, as in the following example. You can also submit prompts using [inline chat](/visualstudio/ide/visual-studio-github-copilot-chat#ask-questions-in-the-inline-chat-view) in the editor window itself.

> [!NOTE]
> GitHub Copilot is powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions. For more information about the general use of GitHub Copilot, product impact, human oversight, and privacy, see [GitHub Copilot FAQs](https://github.com/features/copilot#faq).

The following example shows you how to use Copilot to modify existing code to customize property names and order when serializing to JSON.

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"}
```

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 serializtion. Set property names FirstName to first_name, LastName to last_name. Set order to: Country, FirstName, LastName, Age. Provide customized serialization output.

`#Example.cs` in the prompt provides a [file reference as context](/visualstudio/ide/copilot-chat-context#reference-a-file) to Copilot. You can customize the prompt to output a JSON string with property names and values that suit your requirements.
You can use chat features, such as [slash commands, references to files, methods, or classes, and threads](/visualstudio/ide/copilot-chat-context), to set intent and get better answers with scoped context.

The following output shows an example Copilot Chat response:

```output

To customize the property names and order of the JSON output using System.Text.Json, you can use the JsonPropertyName attribute to rename properties and the JsonPropertyOrder attribute to set the order. Here's how you can modify your code:

using System.Text.Json;
using System.Text.Json.Serialization;

public class Person
{
[JsonPropertyName("first_name")]
[JsonPropertyOrder(2)]
public string? FirstName { get; set; }

[JsonPropertyName("last_name")]
[JsonPropertyOrder(3)]
public string? LastName { get; set; }

[JsonPropertyOrder(4)]
public int Age { get; set; }

[JsonPropertyOrder(1)]
public string? Country { get; set; }
}

public class Program
{
public static void Main()
{
var person = new Person
{
FirstName = "John",
LastName = "Doe",
Age = 30,
Country = "USA"
};

string jsonString = JsonSerializer.Serialize(person, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(jsonString);
}
}

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

**Customized Serialization Output:**
{
"Country": "USA",
"first_name": "John",
"last_name": "Doe",
"Age": 30
}

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]
> Your results might be different from what's shown in the example responses. AI models are non-deterministic, which means that they can return different responses when asked the same question. This might be due to additional learning and adaption over time, language variation, changes in context, such as your chat history, and more.

## See also

- [System.Text.Json overview](overview.md)
- [How to serialize and deserialize JSON](how-to.md)
- [GitHub Copilot Trust Center](https://resources.github.com/copilot-trust-center/)
- [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states)
- [GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/overview)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Text.Json;

public class Person
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public int Age { get; set; }
public string? Country { get; set; }
}

public class Program
{
public static void Main()
{
var person = new Person
{
FirstName = "John",
LastName = "Doe",
Age = 30,
Country = "USA"
};

string jsonString = JsonSerializer.Serialize(person);
Console.WriteLine(jsonString);
}
}
Loading