Visual Studio Tips and tricks: Paste as JSON
If you often work with JSON documents, you may have created classes to map the content of the JSON document to a .NET class. Then, Json.NET
or the new System.Text.Json
allows to serialize a class to a JSON string or to deserialize a JSON string to a .NET class.
Writing mapping classes is annoying and error-prone. Instead of writing them yourself, Visual Studio can generate them using a sample JSON string.
- Copy a JSON string to the clipboard
- Use Paste JSON as classes
Paste JSON as classes in Visual Studio
Let's try with the following JSON document:
JSON
{
"markers": [
{
"name": "Rixos The Palm Dubai",
"position": [25.1212, 55.1535],
},
{
"name": "Shangri-La Hotel",
"location": [25.2084, 55.2719]
},
{
"name": "Grand Hyatt",
"location": [25.2285, 55.3273]
}
]
}
Visual Studio generates the following code:
C#
public class Rootobject
{
public Marker[] markers { get; set; }
}
public class Marker
{
public string name { get; set; }
public float[] position { get; set; }
public float[] location { get; set; }
}
Very handy 😃
This post is part of the series 'Visual Studio Tips and Tricks'. Be sure to check out the rest of the blog posts of the series!
- View and edit the Tab Order of Windows Forms Controls
- Comparing files using Visual Studio
- Visual Studio Tips and tricks: Clipboard history
- Visual Studio Tips and tricks: Open recently closed files
- Visual Studio Tips and tricks: Multi-line and multi-cursor editing
- Visual Studio Tips and tricks: Extend/Reduce selection
- Visual Studio Tips and tricks: Undock/Re-dock a tool window
- Visual Studio Tips and tricks: Regex editing
- Visual Studio Tips and tricks: Find the current opened file in the solution explorer
- Visual Studio Tips and tricks: Default startup project
- Visual Studio Tips and tricks: Open the documentation of a symbol
- Visual Studio Tips and tricks: Paste as JSON (this post)
- Visual Studio Tips and tricks: Add project reference using drag & drop
- Visualizing the code coverage results from Azure Pipelines in Visual Studio
- Debugging a .NET assembly without the source code with Visual Studio
- Visual Studio Tips and tricks: Subword navigation
Do you have a question or a suggestion about this post? Contact me!
Enjoy this blog?💖 Sponsor on GitHub