nikody

nikody

Hey everyone. I have to send a JSON array with non-negative integer values, for example [100, 100], as a value in a JSON payload. How can I do this without having the list changed being charlist 'dd'?

Showing Posts 1 to 8

hauleth

hauleth

These two are exactly the same thing, just different notation. So you do not need nothing.

To explain it further, difference between [100, 100] and 'dd' is the same as between 255 and 0xff. Different notation, the same data.

nikody

nikody OP

I understand this, but the problem is I want strictly the list with integers, not a charlist, for a value in a JSON payload. The problem is that I’m constructing a JSON payload as a map and then encoding it with Poison and the value on the other end, which is a Phoenix app too, instead of it being an array [100, 100] is 'dd'.

I think the problem is actually on the receiving side. Inspecting the encoded by Poison payload seems ok, but the parameter is received with value 'dd' in the other application.

I guess I’ll have to not use a list for this, but I was wondering if there is way to still use a list with strictly integers.

NobbZ

NobbZ

Its just representation, true = [100, 100] === 'dd'!

Enum.each('dd', &IO.inspect/1)

This will output two lines 100.

hauleth

hauleth

The point is that you cannot, because these two are exactly the same. It is just different print representation of them. Just for sake of this conversation assume that you decode JSON like this:

{"foo": 1e2}

Would you be surprised when:

IO.inspect Poison.parse!(~S[{"foo": 1e2}])

Would output:

%{"foo" => 100}

Because 1e2 == 100 in the same way [100, 100] == 'dd'. These two are exactly the same thing just textual representation is different.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe
iex(2)> 'dd' |> IO.inspect(charlists: false) 
[100, 100]

It’s just how your phoenix app is displaying it. You have a list of two 100 values.

jola

jola

To help visualize this, you can try it in IEx

iex(4)> IEx.configure([inspect: [charlists: :as_lists]])
:ok
iex(5)> [100, 100]
[100, 100]
iex(6)> IEx.configure([inspect: [charlists: :as_charlists]])
:ok
iex(7)> [100, 100]                                          
'dd'
nikody

nikody OP

Thanks for the responses everyone. :slight_smile: I had looked through the docs, but I was still a bit confused I guess.

I understand they’re essentially the same. I was confused because I wanted to take a list of integers and join them in a string with a - or :, for instance, and I thought that this might be a problem, but it is not.

Ankhers

Ankhers

Just to clarify, they are not essentially the same. The are exactly the same.

Edit

Basically, because charlists are literally a list of integers (code points), the Elixir and Erlang consoles will attempt to display all integer lists as charlists (or string for erlang). It can only do this if all values are within the ascii display range. So if you had [0, 100, 100], it would not display it as the charlist because 0 (being null) is outside of the ascii display range.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
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
velrest
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
samoloth
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
FlyingNoodle
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

Other Trending Topics Top

JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews