Skip to content

Instantly share code, notes, and snippets.

@dry
Created February 4, 2013 22:04
Show Gist options
  • Save dry/4710115 to your computer and use it in GitHub Desktop.
Save dry/4710115 to your computer and use it in GitHub Desktop.
A Jiffy (https://github.com/davisp/jiffy) wrapper for Elixir
defmodule JSON do
@moduledoc """
This module is a wrapper for the Jiffy JSON library.
## Examples
JSON.encode({[{foo: bar, count: 1}]})
JSON.encode({[{"foo": "bar", "count": 1}]})
JSON.decode("\\"foo\\": \\"bar\\", \\"count\\": 1}]})
"""
actions = [:encode]
lc action inlist actions do
contents =
quote do
@doc "See the module doc"
def unquote(action).(data) do
jiffy(unquote(action), data)
end
@doc "Accepts the Jiffy argument list"
def unquote(action).(data, args) do
jiffy(unquote(action), data, args)
end
end
Module.eval_quoted __MODULE__, contents
end
@doc "This is a simple wrapper around the Jiffy.decode function"
def decode(data) do
:jiffy.decode(data)
end
defp jiffy(:encode, {data}) do
data = Enum.map data, fn({key, value}) ->
reformat {key, value}
end
:jiffy.encode({data})
end
defp jiffy(:encode, {data}, args) do
data = Enum.map data, fn({key, value}) ->
reformat {key, value}
end
:jiffy.encode({data}, args)
end
defp reformat({key, value}) when is_atom(key) and is_atom(value) do
{atom_to_binary(key), atom_to_binary(value)}
end
defp reformat({key, value}) when is_atom(key) do
{atom_to_binary(key), value}
end
defp reformat({key, value}) when is_atom(value) do
{key, atom_to_binary(value)}
end
defp reformat({key, value}) do
{key, value}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment