Skip to content

Deserializing as MapSet fails with BadMapError #187

@janpieper

Description

@janpieper

Poison encodes a MapSet into a list:

iex> MapSet.new([1, 2, 3]) |> Poison.encode!()
"[1,2,3]"

But when trying to deserialize a JSON list as a MapSet, it fails:

iex> "[1,2,3]" |> Poison.decode!(as: %MapSet{})
** (BadMapError) expected a map, got: [1, 2, 3]
    (elixir) lib/map.ex:437: Map.get([1, 2, 3], "map", %{})
    (poison) lib/poison/decoder.ex:73: anonymous fn/3 in Poison.Decode.transform_struct/4
    (stdlib) maps.erl:232: :maps.fold_1/3
    (poison) lib/poison/decoder.ex:72: Poison.Decode.transform_struct/4
    (poison) lib/poison.ex:87: Poison.decode!/2

The reason for the BadMapError is, that Poison tries to deserialize it as a simple struct, which is a map, not a list:

defp transform(value, keys, %{__struct__: _} = as, options) do
transform_struct(value, keys, as, options)
end

I am wondering if Poison should implement a special handling for deserializing a MapSet to avoid this error, because MapSet is more or less a list, not a map.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions