添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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...

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)

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.

I understand.
I’ve just discovered that I cannot use a custom function as guard. I initially thought I could create my own is_json guard function … :no_mouth:

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.

quda:

Too bad Elixir does not have json as a native type. Json is an obliquus format, any modern language must support it natively.

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.stringify to decode & encode JSON via the std library

Python has json.loads & json.dumps to decode & encode JSON via import json

PHP has json_decode & json_encode to decode & encode JSON via the std library

Ruby has JSON.parse & JSON.generate to decode & encode JSON via require '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. :slight_smile:

  • json.org
  • quda:

    I’ve just discovered that I cannot use a custom function as guard. I initially thought I could create my own is_json guard function … :no_mouth:

    Too bad Elixir does not have json as a native type. Json is an obliquus format, any modern language must support it natively.

    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”.

    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.

    […] there doesn’t appear to be a modern language that has – what I would be willing to call – native support for JSON.

    What do you think would Ballerina’s json type qualify?