-
Notifications
You must be signed in to change notification settings - Fork 6k
Add GitHub Copilot use case to serialize JSON #42357
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c58705a
Add GitHub Copilot usage scenario
anandmeg f32df14
Add links and notes
anandmeg e87343f
Updates
anandmeg a0cdc3e
Updates for Mike's feedback
anandmeg b29333b
Merge branch 'dotnet:main' into patch-2
anandmeg 9dafa77
Add copilot gif
anandmeg bf5adc8
Add gif and minor fixes
anandmeg 36323a8
Fix metadata
anandmeg 5cb7eb5
Apply suggestions from code review
anandmeg a63a537
Add bullets for Related content links
anandmeg f82da91
Move section to the end
anandmeg 3463c0e
Update how-to.md
anandmeg a36c91a
Update how-to.md
anandmeg d24ecdf
Update docs/standard/serialization/system-text-json/how-to.md
anandmeg be16b99
Fix typo
anandmeg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
title: "How to serialize JSON in C#" | ||
description: "Learn how to use the System.Text.Json namespace to serialize to JSON in .NET. Includes sample code." | ||
ms.date: 10/19/2023 | ||
ms.date: 9/04/2024 | ||
no-loc: [System.Text.Json, Newtonsoft.Json] | ||
dev_langs: | ||
- "csharp" | ||
|
@@ -12,15 +12,22 @@ | |
- "serialization" | ||
- "objects, serializing" | ||
ms.topic: how-to | ||
ms.collection: ce-skilling-ai-copilot | ||
ms.custom: vs-copilot-horizontal | ||
adobe-target: true | ||
--- | ||
|
||
# How to write .NET objects as JSON (serialize) | ||
|
||
This article shows how to use the <xref:System.Text.Json?displayProperty=fullName> namespace to serialize to JavaScript Object Notation (JSON). If you're porting existing code from `Newtonsoft.Json`, see [How to migrate to `System.Text.Json`](migrate-from-newtonsoft.md). | ||
|
||
>[!Tip] | ||
>You can use AI assistance to [serialize to JSON with GitHub Copilot](#use-github-copilot-to-serialize-to-json). | ||
|
||
To write JSON to a string or to a file, call the <xref:System.Text.Json.JsonSerializer.Serialize%2A?displayProperty=nameWithType> method. | ||
|
||
## Serialization examples | ||
|
||
The following example creates JSON as a string: | ||
|
||
:::code language="csharp" source="snippets/how-to/csharp/SerializeBasic.cs" id="all" highlight="23"::: | ||
|
@@ -43,6 +50,8 @@ | |
:::code language="csharp" source="snippets/how-to/csharp/SerializeWithGenericParameter.cs" highlight="23"::: | ||
:::code language="vb" source="snippets/how-to/vb/RoundtripToString.vb" id="SerializeWithGenericParameter"::: | ||
|
||
You can also use GitHub Copilot to generate serialization code for you. For instructions, see the [Use GitHub Copilot](#use-github-copilot-to-serialize-to-json) section in this article. | ||
|
||
## Serialization behavior | ||
|
||
* By default, all public properties are serialized. You can [specify properties to ignore](ignore-properties.md). You can also include [private members](immutability.md#non-public-members-and-property-accessors). | ||
|
@@ -97,3 +106,72 @@ | |
|
||
> [!TIP] | ||
> If you use `JsonSerializerOptions` repeatedly with the same options, don't create a new `JsonSerializerOptions` instance each time you use it. Reuse the same instance for every call. For more information, see [Reuse JsonSerializerOptions instances](configure-options.md#reuse-jsonserializeroptions-instances). | ||
|
||
## Use GitHub Copilot to serialize to JSON | ||
|
||
You can use GitHub Copilot in your IDE to generate code that uses `System.Text.Json` to serialize to JSON. | ||
|
||
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 serialize to JSON. 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). | ||
|
||
**Example prompt for Copilot Chat** | ||
|
||
``` | ||
Generate code to use `System.Text.Json` to serialize an object to a JSON string. The object contains the following fields: FirstName (string), Lastname (string), Age (int). Provide example output. | ||
``` | ||
|
||
You can customize the prompt to use object fields that suit your requirements. | ||
Check failure on line 125 in docs/standard/serialization/system-text-json/how-to.md
|
||
|
||
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. For an existing class in a project open in the IDE, you can prompt GitHub Copilot with `/generate code to serialize #Classname in #Filename to JSON` | ||
|
||
**Example Copilot Chat response** | ||
|
||
```output | ||
Sure! Below is an example of how to use System.Text.Json to serialize an object with the specified fields to a JSON string in C#: | ||
|
||
using System; | ||
using System.Text.Json; | ||
|
||
public class Person | ||
{ | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
public int Age { get; set; } | ||
} | ||
|
||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
var person = new Person | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
Age = 30 | ||
}; | ||
|
||
string jsonString = JsonSerializer.Serialize(person); | ||
Console.WriteLine(jsonString); | ||
} | ||
} | ||
|
||
**Example Output** | ||
{"FirstName":"John","LastName":"Doe","Age":30} | ||
|
||
This code defines a `Person` class, creates an instance of it, and then serializes it to a JSON string using `JsonSerializer.Serialize`. The resulting JSON string is then printed to the console. | ||
``` | ||
|
||
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. | ||
|
||
:::image type="content" source="./media/serialize-json-using-github-copilot.gif" alt-text="Animated screenshot that shows using GitHub Copilot Chat in Visual Studio to serialize an object to a JSON string." lightbox="./media/serialize-json-using-github-copilot.gif"::: | ||
|
||
## Related content | ||
|
||
* [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) |
Binary file added
BIN
+2.55 MB
...rd/serialization/system-text-json/media/serialize-json-using-github-copilot.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.