devinus / poison

An incredibly fast, pure Elixir JSON library
BSD Zero Clause License
2.02k stars 217 forks source link

Decoding to arbitrary structs #124

Open leg100 opened 7 years ago

leg100 commented 7 years ago

How I would go about decoding JSON into arbitrary structs? e.g.:

Poison.decode!(~s([
  {"type": "giraffe", "args": {"height": 400, "age": 27}},
  {"type": "turtle", "args": {"height": 30, "age": 108}}
]), as: [%type{}???])

Where type is arbitrary but one of a finite number of defined structs, and args are the fields for that struct.

roehst commented 5 years ago

You mean as if the data define the type of struct to be used?

rauann commented 2 years ago

This is the way: https://github.com/devinus/poison/pull/207