On Thu, Sep 12, 2019 at 1:42 PM Sannyasin Brahmanathaswami via use-livecode
<use-livecode@lists.runrev.com> wrote:

> Been working Panos off list.
>
> There is a caveat, jsonImport and jsonExport not only "display" but
> "preserve" the scientific notation as a literal string and math will fail.
>
> Panos says: must use, (and include in a standalone)  mergeJson to
> use:  jsonToArray  and arrayToJSON
>

While ArrayToJSON does fix the problem you are experiencing, it does
introduce additional problems. Try the following code:

```
on mouseUp
  put 1 is 1 into tValueA["root"]["boolean_1"]
  put 1 is 0 into tValueA["root"]["boolean_2"]
  put null into tValueA["root"]["null_1"]
  put "100" into tValueA["root"]["text"]
  put 100+0 into tValueA["root"]["number"]
  put ArrayToJSON(tValueA)
end mouseUp
```

The output is as follows:

```
{
  "root": {
    "boolean_2": false,
    "text": 100,
    "null_1": "",
    "boolean_1": true,
    "number": 100
  }
}

```

ArrayToJSON won't ever quote numbers, even if the variable in LiveCode has
the number stored as a string (and not an integer or a real). Imagine a
user enters the string "100" into a text field and then your app sends that
value to a JSON API. If you send an integer to a JSON API that is expecting
a string you will most likely get an error.

The output would more appropriately be rendered this way:

```

{
  "root": {
    "boolean_2": false,
    "text": "100",
    "null_1": null,
    "boolean_1": true,
    "number": 100
  }
}

```

Here is a variation on ArrayToJSON that renders the more appropriate output:

```

function ArrayToJSON pArray,pForceRootType,pPretty
  local tKey, e

  repeat for each key tKey in pArray
    if pArray[tKey] is an array then
      put "}"&ArrayToJSON(pArray[tKey]) into pArray[tKey]
    else if pArray[tKey] is NULL then
      put "null" into pArray[tKey]
    else if pArray[tKey] is strictly a boolean \
          or pArray[tKey] is strictly an integer \
          or pArray[tKey] is strictly a real then
      put pArray[tKey] into pArray[tKey]
    else
      # treat as a string
      put "}}"&pArray[tKey] into pArray[tKey]
    end if
  end repeat

  return(mergJSONEncode("pArray",pForceRootType,pPretty))
end ArrayToJSON

```

In my projects I use this modified version of ArrayToJSON to export JSON
and a wrapper around JsonImport to import JSON. In my experience that gets
me the closest to expected values. Unfortunately JsonImport is rather slow,
but it is more accurate. I don't recall all of the nuances as to why though.


Here is my wrapper around JsonImmport:

```

function _importJSON pJSON
  # zero width joiner breaks JSON Importer
  # http://quality.livecode.com/show_bug.cgi?id=19691
  replace numtoCodePoint("0x200D") with empty in pJSON
  return JsonImport(pJSON)
end _importJSON

```

-- 
Trevor DeVore
 ScreenSteps
www.screensteps.com
_______________________________________________
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Reply via email to