bhall2001 / fastjson

A Livecode library for JSON encoding and decoding to and from Arrays.
MIT License
17 stars 11 forks source link

Cannot correctly create or convert an empty array #23

Closed madpink closed 8 years ago

madpink commented 8 years ago

I wanted to post this as an issue because it may be an issue for others. I have a couple of quick fixes, but I am not 100% sure they need to be implemented so I am not going to create a pull request. For CouchDB the empty array is important, I believe that it also is for MongoDB. I am not sure about other uses.

Here is an example of what I am trying to produce (this is a user record for CouchDB):

{"name":"madpink","password":"trustno1","roles":[],"type":"user"}

"Roles" needs to be an array, and can only be full of strings or completely empty. Closest I can get is this script:

   put "madpink" into tData["name"]
   put "trustno1" into tData["password"]
   put "user" into tData["type"]
   put empty into tData["roles"][1]
   put jsonFromArray(tData) into tJson

This is the result:

{"type":"user","password":"trustno1","roles":[null],"name":"madpink"}

However, this errors out. For the time being I just have the following to my own script:

replace "[null]" with "[]" in tJson

Needless to say, the reverse process is also a problem, so I added this step before processing a JSON into an array:

put "{"&quote&1&quote&":null}" into tEmpty
replace "[]" with tEmpty in tJson
bhall2001 commented 8 years ago

Very interesting. This comes from easyJson library. I'm going with it's a bug. "null" is a string. What I believe the JSON definition implies is that null is equivalent to LC's empty. So, if you've made the correction in fastJson go ahead and submit a pull request. I think it's the correct way the library should handle null.

madpink commented 8 years ago

null is a valid value in JSON, it is just specifically in the context of an array that there is a problem...

I'm going to dive through the code to see if there is a more elegant solution for the array to json conversion, but I think that the json to array is going to be more problematic

bhall2001 commented 8 years ago

Fixed in version 1.1.0