JSON to Elixir code generator (online tool)

Whenever I’m writing an API I always like to create an embedded schema at the “web” level to validate and add types to my request/responses. Manually creating an embedded schema for a large JSON payload is pretty annoying. So I hacked together this little LiveView App to automate that process for us.

Here is a screenshot of the app:

There’s still some other little things I want to add and edge cases that need to be taken care of. But I think its ready enough to be played with.
Let me know what you think!

20 Likes

Oh this is a really, really neat tool! These kind of code generation tools are just really nice to use, so this is probably going to be popular - maybe you could add to it the ability to generate structs from the JSON!

Would love for this to be a CLI tool instead :slight_smile:

2 Likes

Yea my initial idea was to create structs. But since you can’t type the items in a list with defstruct I decided for a first pass I would just do an embedded_schema. I wanna give the option as all if to nest the embeds or if to generate new modules for them.

I think for generating the structs when encountering a list I might just set the value to an empty list but still generate the struct for the list items.

Yea or maybe a mix task?

Yeah, what I imagined when I said a CLI tool :slight_smile:

I think I crashed your app :slight_smile:

2 Likes

Mate you can’t crash the elixir app. :joy:, it works

What I mean by that is I have a json payload that causes the app to reload itself, not a permanent denial of service.

2 Likes

Awesome! Send me the JSON so I can get it fixed on my end =]

Your project can be actually DDOSed as you are using string_to_atom (or I think so?), witch is a dangerous function and can overflow the number of atoms in the system. To crash you can simply do:

{
  "\"": 123
}

Yea I don’t use real atoms anywhere in the code. What I ended up doing is in the parsing stage whenever I need to “create” and atom I actually create a string with a placeholder for later use in the eval step.

You can see an example of it being used here

And the actual atom function that sets the placeholder:.

Then in the eval step all we do is generate the code with the Macro.to_string helper and regex out the placeholder atom to a “real” but not really a real atom.

1 Like

@yangm97 Thank you! Issue has been resolved! The lexer wasn’t handling negative numbers. Give it a try and let me know if it works!

1 Like

Thank you for your tool! It now works as expected… well kind of.
I noticed it duplicates/attempts to redefine recursive data structures instead of normalizing and defining them once.

What do you mean by this? Do you have an example?

Sure! You can see that happening on my previous JSON. Some entities are being defined multiple times with varying properties, instead of being merged as a single entity composed of all properties inferred by each instance.