quda
Is it possible to create a function to accept only a valid json as parameter/argument ? Not map!
Precisely, I need something that would perform as:
def myFn(obj) when is_json(obj) do
...function content...
end
I am aware that is_json() does not exist in Elixir.
The idea is the function should accept only a string that will parse to json:
myFn("{\"k1\":7,"\k2\":"\hello\"}") should be valid but myFn("bla bla bla") not. (the json objects arrive already stringified from an upstream API)
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
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 have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
cmo
Use
Jason.decode!to throw orJason.decodeand pattern match on the result. Not in a guard though.Or similar in whatever json library you’re using.
quda
Might be a fine workaround, but I look for solving it with a guard as I consider it to be more elegant.
benwilson512
Guard clauses map to VM level instructions, you can only use those specific clauses in guards, in combinations. The only way to know that a string is JSON is to parse it, and I don’t see how you could create a set of guard clauses that would parse JSON.
quda
I understand.
I’ve just discovered that I cannot use a custom function as guard. I initially thought I could create my own
is_jsonguard function …Too bad Elixir does not have json as a native type. Json is an obliquus format, any modern language must support it natively.
Eg.: I am working with data feeds from industrial machines (automation robots) and their output standard format is json.
l00ker
I understand your frustration, but I just have to ask the question:
What modern language has native support for JSON?
JavaScript has
JSON.parse&JSON.stringifyto decode & encode JSON via the std libraryPython has
json.loads&json.dumpsto decode & encode JSON viaimport jsonPHP has
json_decode&json_encodeto decode & encode JSON via the std libraryRuby has
JSON.parse&JSON.generateto decode & encode JSON viarequire 'json'… [1]
The point I’m trying to make is that there doesn’t appear to be a modern language that has – what I would be willing to call – native support for JSON.
Every widely used (modern?) language has functions or methods to encode & decode its native objects or data structures to/from JSON.
Even JavaScript (the ‘J’ in JSON) doesn’t appear to have native support.
JSON (JavaScript Object Notation) is a lightweight data-interchange format [1], and in being such, I believe every language would need to attempt to parse JSON order to determine whether it’s valid or not, and Elixir is no exception.
eksperimental
You can, it only can use other guard safe functions.
You can create your own macro, or use
defguard,defguardp,smathy
JSON is just a format specification of a string, Elixir has native support for strings in any format, including JSON. To the extent that a language can parse a JSON formatted string to a native data structure, and marshal a native data structure to a string of JSON, it has the most “support for JSON” possible.
Lamenting that a language doesn’t have “a JSON native type” is like lamenting that a language doesn’t have “an HTML native type”.
wanton7
Validating JSON could be quite slow process depending how big is the string. If I’m not mistaken all guards in Elixir execute in constant time. Meaning variable length JSON string can’t be a guard. Anyway you shouldn’t be validating JSON anywhere else but where the data comes into your system. As JSON being dynamic data, so just checking is JSON might not be enough if you are getting JSON from untrusted source like a browser.
hauleth
That is not true. Few aren’t constant time:
length/1is linearis_map_key/2and map access (map_get/2) are logarithmic (and dependants likeis_struct/{1,2}wanton7
Interesting. So length is not stored in the list and it has to be iterated to get the length?