The jason
is a blazing-fast JSON parser and generator that’s written in pure Elixir.
The jason
package can be installed by adding it to the dependencies list in mix.exs
:
def deps do
[{:jason, "~> 1.3"}]
end
The jason.encode()
function is used to generate a JSON corresponding to the given input.
Jason.encode(input, opts \\ [])
input
: This is the input whose JSON has to be generated.opts
: These are the options for the function.The jason.decode()
function is used to parse a JSON from the given input.
Jason.decode(input, opts \\ [])
input
: This is the input to be parsed to a JSON.opts
: These are the options for the function.Let’s look at the code below:
defp to_json(value, opts \\ []) doJason.encode!(value, opts)enddefp parse!(json, opts \\ []) doJason.decode!(json, opts)endIO.puts to_json(%{a: 3.14159, b: 1}, pretty: true)IO.puts parse!(~s( { "foo" : "bar" , "baz" : "quux" } ))
to_json()
function is defined where the json is encoded using the Jason.encode!()
function.parse()
function is defined where the json is decoded using the Jason.decode!()
function.to_json()
function is invoked.parse()
function is invoked.Free Resources