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

Scripting • Inconsistent results with JSON serialize, deserialize and numbers

$
0
0
A word of warning with the new serialize and deserialize commands, each of them do different conversion, so the process is not reversible!

I have been working a bit with the new serialise and deserialize commands and JSON containing various types of numbers, and the results are wildly inconsistent.

An example JSON containing various types of numbers, and numbers as strings:
Code:
{    "1": "7",    "2": 7,    "3": "7.1",    "4": 7.1,    "5": 7.14,    "6": "7.14",    "8": "7.1234567"}
First deserialized to an array, and then serialized to JSON:
Code:
global a "{\"1\":\"7\",\"2\":7,\"3\":\"7.1\",\"4\":7.1,\"5\":7.14,\"6\":\"7.14\",\"8\":\"7.1234567\"}"    global b [deserialize $a from=json ]                                                                  put $b                                                                                                1=7;2=7;3=7.0.0.1;4=7.1;5=7.14;6=7.0.0.14;8=00:00:07.123456700global c [serialize $b to=json ]                                                                      put $c                                                                                                {"1":7,"2":7,"3":"7.0.0.1","4":7.100000,"5":7.140000,"6":"7.0.0.14","8":"1970-01-01 00:00:07"}
And the result:
Code:
{    "1": 7,    "2": 7,    "3": "7.0.0.1",    "4": 7.1,    "5": 7.14,    "6": "7.0.0.14",    "8": "1970-01-01 00:00:07"}
Also both strings and number are converted to numbers when serializing, however not the same numbers:
Code:
global d set ($d->"1") "7"set ($d->"2") 7   global e [serialize $d to=json ]put $d1=7;2=7put $e                          {"1":7.000000,"2":7}
This shows that a number contained in a string is always converted to a number with 6 decimals, while the same number as a number is converted to an integer.

I have created a support ticket on this also: SUP-138863

Statistics: Posted by xrlls — Thu Dec 28, 2023 11:43 am



Viewing all articles
Browse latest Browse all 23620

Trending Articles