How to create a wrapper package

I’m interested in using HJSON but it’s quite annoying to run hjson every time and create a json file that I can then read with JSON3. How would I go about creating a HJSON.jl such that I can do the conversion inside julia which would already be an improvement and then probably just have a readhjson function that goes through conversion and reading it using JSON3 in one go.

Looking forward to any pointers of how to create such a wrapper package.

1 Like

You don’t really need a package for that. If you have the hjson binary on your path, then the following simple function should suffice:

using JSON

function hjson_parse(file::AbstractString; kwargs...)
    json = Pipe()
    run(pipeline(`hjson -c $file`, json); wait = false)
    return JSON.parse(json; kwargs...)
end

The problem is installing that binary on the servers I need to run my stuff on so I need some kind of jll stuff I would guess

I don’t see why that’s a problem. The tool doesn’t require installation, it’s just an ELF binary that can be downloaded and extracted from the homepage.

curl -L https://github.com/hjson/hjson-go/releases/download/v4.4.0/hjson_v4.4.0_linux_amd64.tar.gz | tar xz ./hjson

In this case, you need to modify the above julia snippet to call ./hjson instead of plain hjson, otherwise it won’t be able to find the binary, but that’s it. You can carry a copy of the hjson binary with your code, even commit it into your repository, etc.

If you really need, you can even make this part of your julia code:

using JSON

const URL = "https://github.com/hjson/hjson-go/releases/download/v4.4.0/hjson_v4.4.0_linux_amd64.tar.gz"

function hjson_parse(file::AbstractString; kwargs...)
    if !isfile("hjson")
        run(pipeline(`curl -sL $URL`, `tar xz ./hjson`))
    end
    json = Pipe()
    run(pipeline(`./hjson -c $file`, json); wait = false)
    return JSON.parse(json; kwargs...)
end

GitHub - JuliaPackaging/BinaryBuilder.jl: Binary Dependency Builder for Julia supports Go so it should be relatively straight forward to make a jll of GitHub - hjson/hjson-go: Hjson for Go.

An example of a go app on Yggdrasil is Yggdrasil/G/goaws/build_tarballs.jl at 5c85d729b0f178277359ace7f9091c8677fbf777 · JuliaPackaging/Yggdrasil · GitHub

1 Like

Thanks. My colleague just created: GitHub - fmatesa/HJSON.jl

1 Like