poison icon indicating copy to clipboard operation
poison copied to clipboard

Bring back Phoenix compatibility

Open erszcz opened this issue 2 years ago • 2 comments

Hi! Dropping encode_to_iodata!/1 has lead to losing compatibility with Phoenix as can be seen with https://github.com/phoenixframework/phoenix/pull/4727. If this PR gets accepted it will unblock https://github.com/phoenixframework/phoenix/pull/4728 which brings that compatibility back.

erszcz avatar Mar 17 '22 19:03 erszcz

PR's been open since Aug 2021 😭 https://github.com/devinus/poison/pull/197

RudolfMan avatar Mar 18 '22 04:03 RudolfMan

Thanks, @RudolfMan! Well, I can only hope this PR is heard as another voice in the case 🤞

erszcz avatar Mar 18 '22 07:03 erszcz

This is still a problem, but I'm cleaning up some of my repos, so I'll be closing this PR. If one needs to use Poison with Phoenix, Postgrex, or PhoenixSwagger, then a shim module like this one might be handy:

defmodule MyApp.Poison do
  @moduledoc false

  defdelegate decode(iodata, options \\ %{}), to: Poison
  defdelegate decode!(value, options \\ %{}), to: Poison

  defdelegate encode(value, options \\ %{}), to: Poison
  defdelegate encode!(value, options \\ %{}), to: Poison

  @doc """
  Encodes a value to JSON, returning an iodata.

  Equivalent to `encode!(value, iodata: true)`.

  Prefer `encode!/1`.
  This function only exists for compatibility with Phoenix, Postgres, or PhoenixSwagger. 
  """
  def encode_to_iodata!(value) do
    Poison.encode!(value, iodata: true)
  end
end

Then the only remaining bit is the config:

$ rg json_lib
config/config.exs
9:config :phoenix, :json_library, MyApp.Poison
10:config :postgrex, :json_library, MyApp.Poison
22:config :phoenix_swagger, json_library: MyApp.Poison

erszcz avatar Jun 27 '23 07:06 erszcz