Is possible to generate classes by a macro that loads and parse JSON definition?

Hello! Is possible to make macro, that loads JSON, parse it and generate classes based on this JSON recipe? I know, that I can load file by the macro - is JSON parsing completely out of the concept of macros? Thanks!

  1. It’s always possible to achieve this through any build system (Makefile etc.)
  2. In Crystal any code generation is possible through macro run. Just make a program that prints source code “normally”. This is quite heavy, though, and I don’t think it’s parallelized (but it is cached).
1 Like

I wonder if it would make sense to translate the JSON schema to Crystal outside of the compilation step, similar to how Protobuf compiles from its source format into language-specific code in a separate step using the protoc tool.

This way you can see exactly what Crystal code you’re generating. That generated code might use macros but it can be a lot easier to read. I’ve never found JSON schemas written in JSON to be very readable as source material, though. :slightly_smiling_face:

Is this what you are looking for?