jason icon indicating copy to clipboard operation
jason copied to clipboard

Support for decoding into data structures

Open lustrousgorilla opened this issue 4 years ago • 3 comments

no support for decoding into data structures (the as: option)

@michalmuskala is this intentionally unsupported, or would you be open to adding this as a feature if I submit a PR?

lustrousgorilla avatar Nov 26 '20 07:11 lustrousgorilla

I would say intentional.

In general, I find the feature not particularly useful (since it doesn't do any data validation I'd find it quite hard to use for processing external data), and quite awkward to use with nested data structures.

Additionally, there's no need for it to be part of the JSON library. In Poison, the feature is completely separate from the decoder and runs as a separate pass on the decoded data. As such it's possible to extract it to a standalone library that could be used more widely than just for JSON or for various JSON libraries.

I did think about building a proper decoding support into Jason (or as a separate company library) that would be more complete and easier to use, however, I never found time to actually implement my ideas. It would be a much bigger endeavour than just porting the Poison mechanism.

michalmuskala avatar Nov 26 '20 17:11 michalmuskala

Can be done like so

lpender avatar Mar 03 '21 18:03 lpender

Doubling down on what @michalmuskala said, probably using a package that behaves similarly to how protobuf does it is the solution here, but it should be a package around Jason instead of complex.

Are you familiar with any intention to solve such a problem of mapping into some structs and whatnot other than protobuf? Could that be the solution?

yordis avatar May 30 '22 06:05 yordis

I'm going to close this as out of scope for Jason.

There are many libraries in the Elixir ecosystem that can handle loading data into structures and include proper handling for nested data and type-safety. For example: Ecto.Changeset, Vex, Norm, or any similar. I'd recommend using one of them to achieve this.

michalmuskala avatar Sep 12 '22 19:09 michalmuskala