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
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
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
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
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
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
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










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: