darklang / dark

Darklang main repo, including language, backend, and infra
https://darklang.com
Other
1.67k stars 90 forks source link

Package manager: parse+decode fetched JSON without any reflection #5279

Closed StachuDotNet closed 7 months ago

StachuDotNet commented 7 months ago

Our F# package manager fetches package items (types, consts, fns) from our dark-packages canvas, over HTTP. Those payloads are in JSON.

Our Json.Vanilla uses System.Text.Json under the hood, which is a reflection-based JSON deserializer.

Our CLI application is AOT-compiled, which puts us in a situation where we can't use run-time reflection.

So, we needed some way to safely deserialize JSON in without using reflection, or by using C# Source Generators (not yet available in F#).

I tried using C# source generators at first, but it broke, and it was broken magic, which is double bad, so abandoned that path.

Then I tried using an F# library Thoth.Json. I didn't quite understand or agree with its types/API, so accidentally started writing an equivalent module, included here as 'SimpleJson'. I don't love inventing a new thing for our use case, but I'm kinda happy with how it turned out.

Anyway - this works. (it might be a bit slow -- I haven't done a perf test, but at least this unblocks us)