Is there a way to parse JSON in Elixir without installing a dependency?

I’m building a library and so far have no other dependencies except Elixir. Now I need to parse some JSON and would rather not reach for a third-party library if Elixir can do this natively.

Couldn’t find anything in the docs related to this.

1 Like

:wave:

No, I’m afraid you’d either need to add a library or write your own module that would do the parsing.

You can also shift that choice to the user of your library by using json encoding/decoding callback module.

config :your_lib, json_library: Jason
# somewhere in your library
defmodule JSON do
  # or maybe do it dynamically instead by fetching it inside the functions
  @json Application.get_env(:your_lib, :json_library) || raise("need your_lib.json_library set")

  def decode(json) do
    @json.decode(json)
  end

  def encode_to_iodata(json) do
    @json.encode_to_iodata(json)
  end
end
5 Likes