From d9a113de8a3acf13d099929789234391560365a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 12 Dec 2014 14:40:53 +0100 Subject: [PATCH 1/2] Add Poison.encode_to_iodata(!)/2 Having an explicit function plays nicer with dialyzer and makes Poison simpler to use as callbacks. Ideally we would deprecate the :iodata option in encode and titghen its typespecs but I haven't done so in this commit in order to keep backwards compatibility. --- lib/poison.ex | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/poison.ex b/lib/poison.ex index c2ac4bcf..8a02d2ef 100644 --- a/lib/poison.ex +++ b/lib/poison.ex @@ -24,6 +24,17 @@ defmodule Poison do {:error, exception} end + @doc """ + Encode a value to JSON as iodata. + + iex> Poison.encode_to_iodata([1, 2, 3]) + {:ok, [91, "1", [44, "2", 44, "3"], 93]} + """ + @spec encode_to_iodata(Encoder.t, Keyword.t) :: {:ok, iodata} | {:error, {:invalid, any}} + def encode_to_iodata(value, options \\ []) do + encode(value, [iodata: true] ++ options) + end + @doc """ Encode a value to JSON, raises an exception on error. @@ -45,6 +56,17 @@ defmodule Poison do end end + @doc """ + Encode a value to JSON as iodata, raises an exception on error. + + iex> Poison.encode_to_iodata!([1, 2, 3]) + [91, "1", [44, "2", 44, "3"], 93] + """ + @spec encode_to_iodata!(Encoder.t, Keyword.t) :: iodata | no_return + def encode_to_iodata!(value, options \\ []) do + encode!(value, [iodata: true] ++ options) + end + @doc """ Decode JSON to a value. From 4495364f8949923f3bec14ab42740280e57ba570 Mon Sep 17 00:00:00 2001 From: Rudolf Manusadzhian Date: Thu, 5 Aug 2021 17:43:53 -0700 Subject: [PATCH 2/2] Bring back `Poison.encode_to_iodata/1` adjust examples --- lib/poison.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/poison.ex b/lib/poison.ex index 8a02d2ef..bb07476a 100644 --- a/lib/poison.ex +++ b/lib/poison.ex @@ -28,7 +28,7 @@ defmodule Poison do Encode a value to JSON as iodata. iex> Poison.encode_to_iodata([1, 2, 3]) - {:ok, [91, "1", [44, "2", 44, "3"], 93]} + {:ok, [91, ["1", 44, "2", 44, "3"], 93]} """ @spec encode_to_iodata(Encoder.t, Keyword.t) :: {:ok, iodata} | {:error, {:invalid, any}} def encode_to_iodata(value, options \\ []) do @@ -60,7 +60,7 @@ defmodule Poison do Encode a value to JSON as iodata, raises an exception on error. iex> Poison.encode_to_iodata!([1, 2, 3]) - [91, "1", [44, "2", 44, "3"], 93] + [91, ["1", 44, "2", 44, "3"], 93] """ @spec encode_to_iodata!(Encoder.t, Keyword.t) :: iodata | no_return def encode_to_iodata!(value, options \\ []) do