.NET Tips and Tricks

Blog archive

Paste JSON and XML as a Class

I don't use Visual Studio's Edit menu enough, obviously: For some time there's been a Paste Special option on that menu that I didn't know about. It has up to two choices depending on what kind of file you're editing: Paste XML as Classes and Paste JSON as Classes. Using the Paste JSON as Classes option, I can copy some JSON in my JavaScript code and create a corresponding C# or Visual Basic class in my server-side code just by picking a menu the appropriate menu choice.

However, I'd rather be able to use this functionality without having to drag my mouse to the Edit menu. To support that, I added a keystroke combination for the underlying commands related to these menu choices.

If you want to do the same, first go to Tools | Options | Environment | Keyboard. To grab the commands, in the "Show commands containing" textbox, type in AsClasses and select one. Then, from the "Use new shortcut in" dropdown list, pick your language's editor (for example, CSharp Editor or Microsoft Visual Basic Editor). Now you can click in the "Press shortcut keys" textbox to select your shortcut key(s): Just press the keys you want to use. Finally, click the Assign button. Lots of people forget that step and just click the OK button at the bottom of the dialog (by "lots of people" I mean: me).

You probably don't want to override any existing shortcut keys so I'd recommend picking a "chord": two or more keys pressed sequentially while holding down one of the control keys (the dialog box will tell you if you pick a shortcut that's already in use). I picked Ctrl+K, J for pasting JSON and Ctrl+K, M for pasting XML (Ctrl+K, X was already taken for inserting snippets and, who knows, I might start using that some day).

My life would be complete if there was a Paste Class as JSON when I was in a TypeScript or JavaScript file. In the meantime, I'm settling for Edit | Insert File as Text and doing some editing or using Nha Bui Doc's TypeScriptSyntaxPaste.

Posted by Peter Vogel on 02/16/2018


comments powered by Disqus

Featured

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube