quda
Function accepting only json as argument, with guard?
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)
First Post!
cmo
Use Jason.decode! to throw or Jason.decode and pattern match on the result. Not in a guard though.
Or similar in whatever json library you’re using.
Most Liked
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 library -
Python has
json.loads&json.dumpsto decode & encode JSON viaimport json -
PHP has
json_decode&json_encodeto decode & encode JSON via the std library -
Ruby 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. ![]()
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.
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}
Last Post!
ONeill
I dont know if you can do it with a guard but you should be able to do pattern matching in the function clause like
def func(%JSON{} = my_json), do: my_json
That way the function will only match when a JSON struct is passed in.
Edit:
defmodule Test do
defstruct hello: "world", field: 2
def func(%Test{} = my_struct) do
IO.puts("Test struct")
end
end
%Test{hello: "world", field: 2}
|>Test.func()
# Test struct
%{hello: "world", field: 2}
|> Test.func()
# ** (FunctionClauseError) no function clause matching in Test.func/1
Popular in Questions
Other popular topics
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









