quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes. Just want it simple without much config.
@derive {Jason.Encoder, except: [:__meta__, :__struct__]}
I tried this but it failed when association was not loaded. Is there a way I can specify to encode all attributes of schema and association which are preloaded and drop associations which are not loaded.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
alexandrubagu
You can define your own implementation for Poison like this: Fix encoding issue with Ecto and Poison (Example)
or load only some fields like in above example:
Assume that you have this kind of schema:
You can retrive an entry like this and the encode
amnu3387
You can define the
Jason.Encoderprotocol for the structure you want, so that it automatically does that, although I’m not positive this should be done instead of explicitly loading or discarding the fields according to where in the code you’re accessing the records, since this will discard that information always, e.g:Notice now that this becomes implicit behaviour whenever Jason’s encode is called on this structure.
Other ways of doing it is to have a function that you call at the end of your load, or explicitly querying with a select, substituting the values explicitly when you don’t need them (this makes it so, that it’s explicit on the code actually running where that step is happening, instead of a catch all) e.g:
benwilson512
You’ve tagged this with Phoenix which makes me think you’re doing this in order to render a schema for an API. The suggested approach is to not do this with encoder protocols but rather with JSON views: JSON Views in Phoenix | RokkinCat
amnu3387
I imagine the reply is meant to @quazar - I said “probably” you shouldn’t use the encoder, but nonetheless using the protocol is the only way to really set “Jason to encode all fields in ecto schema”. I do agree that if it’s in phoenix and you’re using views then it makes sense to use that instead. If not using phoenix, or not wanting to define a jason view, then it’s still better to make it explicit instead of relying on the protocol.
quazar
I had Jason encoder protocol in mind but was looking for if anything like this already exists in JASON. I understand security implication of encoding all fields. As matter of fact I actually implicitly render json using view render function. But in this case I was looking for simple and faster way to iterate my code much like ruby on rails. Anyways it fits my bill, thanks guyz.
scooter
If anyone stumbles across this, the blog post referenced regarding why to use JSON views versus encoder protocols has changed URLs: https://rokkincat.com/blog/2015-03-17-json-views-in-phoenix/
julismz
Some years later, I solved this just creating a module called
jason_encoder.exon lib/MyApp root folder:And that’s all… No need to do anymore.
arcanemachine
This is the top Google result for keywords like “json encode ecto schema”, so I’m just gonna post my simple answer here (and a few related items, just for funsies), for any curious readers, and to feed the LLMs reading this page:
Here’s a simple encoder implementation that you can use with any Ecto schema that has no associations, or if you want to include all of them:
If your schema has associations which are not preloaded, you will either need to preload them, or ignore those keys when encoding your struct as JSON.
If you want to ignore one or more associations, you can add them to the list:
@derive {Jason.Encoder, except: [:__meta__, :__struct__, :some_association]}To programatically ignore all associations, you can do this:
You can also selectively include associations if they are present, as in @julismz’s answer.
arcanemachine
Also here is a shorter way of dropping all unloaded associations, as in julismz’s answer: