1stSolo

1stSolo

Deserializing struct's from string to DateTime

I have a struct containing some DateTime fields. When I put it to Redis, it gets serialized as json (using Jason lib). So when it gets deserialized the datetime fields come back as strings.

How to restore the data to its original data type? Is there a Plug or Protocol to implement to tell Jason to decode those fields as DateTime?

Marked As Solved

dimitarvp

dimitarvp

Jason doesn’t support extra decoding functionality the same way that it supports custom encoding via its Elixir Protocol. You’re better off just using JSON decoding first and then parse the date strings.

…or you can store your Elixir data structures in Redis verbatim with :erlang.term_to_binary function for encoding and then :erlang.binary_to_term for decoding.

Also Liked

LostKobrakai

LostKobrakai

If you have ecto around you can use Ecto.Type or Repo.load to transform data back to their higher level types.

Last Post!

LostKobrakai

LostKobrakai

If you have ecto around you can use Ecto.Type or Repo.load to transform data back to their higher level types.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement