Automation of JSON2APEX – My journey to writing my own automated tooling for this!
Each and every time developing an integration, I wanted to automate the most tedious and routine part of it – creating wrappers for integration entities. I found a couple of tools for it, but all of them are online tools. That meant I had to leave the comfort and speed of automation via command-line and my keyboard, which I did not prefer to do. I used Sublime for a while and tried to find some polygons that would help me, but failed. But, that’s alright – as they say, if you can’t find something that suits your requirements – DIY!
Basically, this tool can create an Apex class from a JSON sample, e.g. an API response. How is this any different from online tools, you may ask? Most online tools do not care about having the same attributes in entities and often generate a lot of almost similar classes. Well, when I’m generating a code I want to get rid of manual completely, so I don’t want to look through generated classes to find duplicates. JSON2Apex handles similar properties and generates only one wrapper class for them.
There’s one more feature in the plugin – it allows you to code in a meta-language. This means you can quickly create some kind of an interface for the future class. You describe the whole class in YAML, specifying interfaces it implements and classes it extends, and JSON2Apex generates a class with methods’ notations. So you should only insert implementation.
At the time when I started coding this tool, I was working on an API for an Angular app, and we had to document it in Swagger 2.0. Researching the Swagger syntax, I thought that it would be nice to have a tool that would create an API from the description. Unfortunately, I’ve found nothing useful for generating Apex from Swagger, so, once again, I started coding it myself. The work is still in progress, but soon we will generate a Salesforce custom REST resource out of an API description written in Swagger. RAML 1.0 is also in the works!