zf

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

jsonb.ex (1077B)


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