Elixir JSON
This library provides a natively implemented JSON encoder and decoder for Elixir.
All contributions are welcome.
Installing
Latest stable version
Add {:json, "~> 1.0"} to your project's mix.exs file,
in the dependencies list and run mix deps.get json.
Bleeding Edge
In addition to the above, which should always be use for MIX_ENV=prod,
you can use bleeding edge on your de builds and they will always be updated
with the latest and most performant builds.
Add {:json, github: "cblage/json", branch: "develop", only: [:dev, :test] }
to your project's mix.exs file,
in the dependencies list and run mix deps.get json.
I strongly recommend keeping {:json, "~> 1.0"} for MIX_ENV=prod.
Example for a project that already uses Plug:
defp deps do
[{:cowboy, "~> 1.0.0"},
{:plug, "~> 1.0"},
{:json, "~> 1.0"}
endExample for a project that uses and bleeding edge for dev and Plug:
defp deps do
[{:cowboy, "~> 1.0.0"},
{:plug, "~> 1.0"},
json(Mix.env())]
end
defp json(:prod), do: {:json, "~> 1.0"}
defp json(:dev), do: {:json, github: "cblage/json", branch: "develop", only: [:dev, :test]}
defp json(:test), do: {:json, github: "cblage/json", branch: "develop", only: [:dev, :test]}Usage
Encoding an Elixir type
@doc "
JSON encode an Elixir list
"
list = [key: "this will be a value"]
is_list(list)
# true
list[:key]
# "this will be a value"
{status, result} = JSON.encode(list)
# {:ok, "{\"key\":\"this will be a value\"}"}
String.length(result)
# 41Decoding a list from a string that contains JSON
@doc "
JSON decode a string into an Elixir list
"
json_input = "{\"key\":\"this will be a value\"}"
{status, list} = JSON.decode(json_input)
{:ok, %{"key" => "this will be a value"}}
list[:key]
# nil
list["key"]
# "this will be a value"License
The Elixir JSON library is available under the BSD 3-Clause aka "BSD New" license