michalmuskala / jason

A blazing fast JSON parser and generator in pure Elixir.
Other
1.6k stars 170 forks source link

Support for decoding into data structures #124

Closed lustrousgorilla closed 1 year ago

lustrousgorilla commented 3 years ago

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?

michalmuskala commented 3 years ago

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.

lpender commented 3 years ago

Can be done like so

yordis commented 2 years ago

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?

michalmuskala commented 1 year ago

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.