jeroenhouben

jeroenhouben

Jason decode string and keys options, what do they do?

I’m reading the options in Jason.decode but the docs don’t explain it well enough for me to fully understand the pros and cons of the options:

https://hexdocs.pm/jason/Jason.html#functions

I’m parsing a long list of JSON files into Structs and storing those in a long running Agent. Which options should I be using?

TIA,

Jeroen

Marked As Solved

lucaong

lucaong

Then :copy might be better, especially if it is fast enough for your case, but you could try to check the memory usage: in most cases it won’t make much difference. The worst case scenario is when you have a huge JSON, but you only save for long time a tiny part of the decoded data: the whole big JSON binary would stay in memory, even if you only use a small part of it.

Also Liked

lucaong

lucaong

The keys option controls whether keys are decided into strings or atoms. Atoms are never garbage-collected, so if the JSON can contain arbitrary keys, you should decode keys as strings to avoid the possibility that a malicious user would send many different keys, creating many atoms and causing your app to leak memory. If you expect the keys to belong to a known set of possible values, you could use :atoms!, which would decode keys to atoms but only allow existing atoms, not create new ones, to prevent the case above.

The strings option controls performance optimization in string keys and values. The end result is the same, but there are performance considerations to take into account. The default is to reference string keys and values: the decoded data would reference the original binary when possible, instead of copying over the strings. In other words, if the original JSON string was "{\"foo\": 123}", and the decoded data is %{"foo" => 123}, the "foo" key in the decoded data would not be a new string, but “point” to the part of the original JSON string that contains foo. This will save memory and time, as no copies of the strings need to be created, but could create memory leaks if you store pieces of the decoded data in memory for a long time: since the decoded data might be referencing the original binary data, the latter cannot be garbage collected. If you don’t store the parsed data in memory, but rather just use it, you can safely use the default here.

Is this clearer?

lpil

lpil

Creator of Gleam

To add to the excellent info from @lucaong, if you don’t know specifically what you want then the defaults are best :slight_smile:

jeroenhouben

jeroenhouben

Well first of all many thanks for this elaborate answer!

If you don’t store the parsed data in memory, but rather just use it, you can safely use the default here.

I read the json from files and then I create structs from the parsed data. And I store those structs in memory… I think I should use :copy to avoid mem leaks then… ?

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
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

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

We're in Beta

About us Mission Statement