.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

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

  • Microsoft Execs to Tackle AI and Cloud in Dev Conference Keynotes

    AI unsurprisingly is all over keynotes that Microsoft execs will helm to kick off the Visual Studio Live! developer conference in Las Vegas, March 10-14, which the company described as "a must-attend event."

  • Copilot Agentic AI Dev Environment Opens Up to All

    Microsoft removed waitlist restrictions for some of its most advanced GenAI tech, Copilot Workspace, recently made available as a technical preview.

Subscribe on YouTube