Visual Studio 2022 offers so many new features that we easily overlook how existing features have been improved. In today’s post, I like to point out how “Paste JSON as Classes” got better and better. Pasting XML or JSON as classes exists since Visual Studio 2017. However, in Visual Studio 2022 you can work with complex object graphs that earlier Visual Studios could not handle.
Before you can use this feature, you need a place where you can paste your classes. An empty file or a namespace declaration is enough:
1 2 3 4 5 6 |
using System; namespace PasteJsonDemo { } |
Then you can copy your JSON into the clipboard and go in Visual Studio to
Edit / Paste Special / Paste JSON AS Classes:
Visual Studio 2022 nearly instantly pastes the code for the classes into your file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; namespace PasteJsonDemo { public class Rootobject { public Data data { get; set; } } public class Data { public Bookmark_Timeline bookmark_timeline { get; set; } } public class Bookmark_Timeline { public Timeline timeline { get; set; } } ... } |
To see how much this feature improved I used the return data of Twitter’s Bookmarks feature. Visual Studio 2017 crashed, while Visual Studio 2022 created the enormous object graph without any problems or delays. And this object graph is huge (206 classes):
If you can throw this object graph at Visual Studio 2022, it will work with everything you create. Try it!