Quantcast
Channel: MikroTik
Viewing all articles
Browse latest Browse all 23620

Scripting • Re: Inconsistent results with JSON serialize, deserialize and numbers

$
0
0
Since RouterOS doesn't support floating point numbers, it can't import them from JSON either.
And speaking of the "dot", if there is a dot the data tries to be interpreted as an IP, so 7.1 is 7.0.0.1 and 7.14 is 7.0.0.14 as an IP...

The true problem is that if the value is quoted "" must be treated as string, and not converted to number, IP or time

On my code I added also missing null, boolean, array and object examples.
Code:
:global a "{\"0\":null,\"0q\":\"null\",\"1\":7,\"1q\":\"7\",\"2\":7.1,\"2q\":\"7.1\",\"3\":false,\"3q\":\"false\",\"4\":[\"A\",7.1,\"7.1\"],\"5\":{\"X\":7.1,\"Xq\":\"7.1\"}}":global b [:deserialize $a from=json]:foreach x,y in=$b do={   :put "$x = $[:tostr $y] as $[:typeof $y]"}

json code

{     "0":null,    "0q":"null",     "1":7,    "1q":"7",     "2":7.1,    "2q":"7.1",     "3":false,    "3q":"false",     "4":["A",7.1,"7.1"],     "5":{           "X":7.1,          "Xq":"7.1"         }}

results code

0 =  as nil0q = null as str1 = 7 as num1q = 7 as num2 = 7.1 as str2q = 7.0.0.1 as ip3 = false as bool3q = false as bool4 = A;7.1;7.0.0.1 as array5 = X=7.1;Xq=7.0.0.1 as array

Also inside the converted arrays is present the same problem.

Statistics: Posted by rextended — Thu Dec 28, 2023 11:54 am



Viewing all articles
Browse latest Browse all 23620

Trending Articles