Skip to content

Instantly share code, notes, and snippets.

@alanpeabody
Created March 11, 2015 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanpeabody/1d42022eaa89b34aa221 to your computer and use it in GitHub Desktop.
Save alanpeabody/1d42022eaa89b34aa221 to your computer and use it in GitHub Desktop.
embedding elixir structs part 2
defmodule Extensions.JSON do
alias Postgrex.TypeInfo
@behaviour Postgrex.Extension
@json ["json", "jsonb"]
def init(_parameters, opts),
do: Keyword.fetch!(opts, :library)
def matching(_library),
do: [type: "json", type: "jsonb"]
def format(_library),
do: :binary
def encode(%TypeInfo{type: type}, map, _state, library) when type in @json,
do: library.encode!(map)
def decode(%TypeInfo{type: type}, json, _state, library) when type in @json,
do: library.decode!(json)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment