0

I've run into a scenario where I would get lot of form.io Javasrcipt based JSON objects to come to a Controller. How could I generate C# classes so that they could be strong typed?

JavaScript Classes:

export default class SomeComponent {
static schema(...sources) {
return _.merge({
  /**
   * Determines if this component provides an input.
   */
  input: true,

  /**
   * The data key for this component (how the data is stored in the database).
   */
  key: '',

  ...

  }

JSON object that goes to the controller:

{
 "input":true,
  "key":"inputElement"
}

The controller that receives:

  public IActionResult Receiver(???) // strongly typed for many kinds of JSON?
    {
        return View();
    }
John
  • 693
  • 1
  • 12
  • 37
  • Do you want to convert the object to C# at run-time? or it will be like any other controller action e.g public IActionResult Receiver(Product product) // strongly typed for many kinds of JSON? { return View(); } – Ikram Shah Mar 11 '19 at 12:24
  • I think you are looking for something like this John: https://stackoverflow.com/questions/11260631/load-json-text-into-class-object-in-c-sharp – Matt Mar 11 '19 at 12:24
  • @IkramShah I'll have the controller action with strongly typed for many kinds of JSON in the noraml way. I basically have many objects to convert into strong typed classes. – John Mar 11 '19 at 12:38
  • If you're using visual studio 2017 then here is your answer. https://stackoverflow.com/a/48023576/1526972 – Ikram Shah Mar 11 '19 at 12:40
  • @IkramShah I've come across this option earlier but I'm looking for a better way than pasting each JSON object manually. – John Mar 11 '19 at 12:47
  • It sounds like you're looking for some sort of automated solution. None exists. You'll need to add classes manually which model your JSON. There's ways to automate that particular part: JSON to C# class. However, you'll still need to go class by class and update your controller actions accordingly to accept those classes. – Chris Pratt Mar 11 '19 at 12:49
  • @ChrisPratt Yes, I'm looking for JSON to C# class generation part. – John Mar 11 '19 at 12:58
  • See the answer @IkramShah linked to. – Chris Pratt Mar 11 '19 at 12:58
  • @ChrisPratt Well, automation isn't presented in the solutions. Copy + Paste options are available. – John Mar 11 '19 at 13:01
  • 1
    Again. The only part you can automate is the process of turning a JSON object into a C# class. Everything else is only you, including potentially creating the physical files in your project, etc. It saves you the time of having to manually write out properties by hand, but there is no *true* automation for something like this. – Chris Pratt Mar 11 '19 at 13:04

0 Answers0