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

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

Other popular topics Top

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
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement