zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

json.ex (1174B)


      1 defmodule Postgrex.Extensions.JSON do
      2   @moduledoc false
      3   @behaviour Postgrex.Extension
      4   import Postgrex.BinaryUtils, warn: false
      5 
      6   def init(opts) do
      7     json =
      8       Keyword.get_lazy(opts, :json, fn ->
      9         Application.get_env(:postgrex, :json_library, Jason)
     10       end)
     11 
     12     {json, Keyword.get(opts, :decode_binary, :copy)}
     13   end
     14 
     15   def matching({nil, _}),
     16     do: []
     17 
     18   def matching(_),
     19     do: [type: "json"]
     20 
     21   def format(_),
     22     do: :binary
     23 
     24   def prelude({library, _}) do
     25     if library do
     26       quote do
     27         @compile {:no_warn_undefined, unquote(library)}
     28       end
     29     end
     30   end
     31 
     32   def encode({library, _}) do
     33     quote location: :keep do
     34       map ->
     35         data = unquote(library).encode_to_iodata!(map)
     36         [<<IO.iodata_length(data)::int32()>> | data]
     37     end
     38   end
     39 
     40   def decode({library, :copy}) do
     41     quote location: :keep do
     42       <<len::int32(), json::binary-size(len)>> ->
     43         json
     44         |> :binary.copy()
     45         |> unquote(library).decode!()
     46     end
     47   end
     48 
     49   def decode({library, :reference}) do
     50     quote location: :keep do
     51       <<len::int32(), json::binary-size(len)>> ->
     52         unquote(library).decode!(json)
     53     end
     54   end
     55 end