0

I'm trying to do (maybe) something not possible today with QuickType. I would like to create c# classes directly in a function. In another words I want to call quicktype functions to create c# classes from json but programmatically and not use https://app.quicktype.io/ Any idea how to perform that ? Thanks in advance for your help!

Silvering
  • 756
  • 1
  • 6
  • 33
  • Does this answer your question? [Deserialize JSON to anonymous object](https://stackoverflow.com/questions/6904825/deserialize-json-to-anonymous-object) – Cid Nov 06 '19 at 10:39
  • On quicktype on the 3 dot in the top right corner there should be the command line and the GitHub. You can either fork or call the command line. But there is no need to go quick type as Visual Studio already have a special past button, there should be a function command somewhere in the c# Lib https://blogs.msdn.microsoft.com/vsx/2016/04/21/how-to-add-a-custom-paste-special-command-to-the-vs-editor-menu/ – Drag and Drop Nov 06 '19 at 10:53
  • Thanks for your reply. However I need to create dynamically the c# class regarding a downloaded json. All of this must run in background when I press a button. – Silvering Nov 06 '19 at 10:58
  • I don't see how my comment doesn't cover the first part of your comment. For the second I don't see how it's revelant. You can In C# call an execute command like this https://stackoverflow.com/questions/1469764/run-command-prompt-commands. Or using the GitHub source code, call directly the truescrypt function. – Drag and Drop Nov 06 '19 at 12:39
  • Both will provide the class definition. But it will not be a class compiled and all ready in the executing project. https://stackoverflow.com/questions/3862226/how-to-dynamically-create-a-class. . At this point you might just go back to 1rst comment from Cid and go back to dynamic type. – Drag and Drop Nov 06 '19 at 12:46

1 Answers1

1
  1. You need to install quicktype (Node.js must be installed)
    npm install -g quicktype
  2. Run this command in C#
    quicktype petstore.json -o petstore.cs

using this code (based on run-command-prompt-commands):

string jsonPath = @"petstore.json"; // path of the JSON file
string outputPath = @"petstore.cs"; // path of the output csharp file

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
    WindowStyle = ProcessWindowStyle.Hidden,
    FileName = "cmd.exe",
    Arguments = $"/C quicktype {jsonPath} -o {outputPath}"
};
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
RyuzakiH
  • 106
  • 1
  • 4