Thanks to visit codestin.com
Credit goes to github.com

Skip to content

How to parse values of an object into struct  #188

@iilyak

Description

@iilyak

I have a JSON in the following format.

{  
   "person1": {
      "name": "name1"
   },
   "person2": {
      "name": "name2"
   }
}
defmodule Person do
  defstruct [:name]
end

How do I specify as parameter to parse into Person struct?

I cannot specify %Person{} since it would mean different thing:

> Poison.decode!(json, %Person{})
%{"person1" => %{"name" => "name1"}, "person2" => %{"name" => "name2"}}

I know that there is no support for things like as: {String, %Person{}}. Are there any workarounds?

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