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!
- It’s always possible to achieve this through any build system (Makefile etc.)
- 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).
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
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.
Is this what you are looking for?