libJson

Find out what's going on with LiveCode (the company), product releases, announcements, and events.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

libJson

Post by Mark Smith » Wed Jan 07, 2009 5:23 pm

Here's a first go at a json parser/generator that converts between revolution arrays and json objects/arrays, to an arbitrary level of nesting. It has some basic formatting capability also.

As usual, any observations, modifications, disasters or other comments welcome.


http://futsoft.futilism.com/revolutionstuff.html

Best,

Mark

nimbleRev
Posts: 10
Joined: Mon Mar 09, 2009 9:50 pm

problem with download link

Post by nimbleRev » Mon Mar 09, 2009 9:54 pm

hey Mark,

When I click the download link on your site I get an xml error message, from S3 that says "Access Denied". Is there another URL I can try?

I'm Really excited to check out your json library!

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

Post by Mark Smith » Tue Mar 10, 2009 3:33 am

Sorry about that! I just noticed from the logs that people were getting "access denied" - I had the permissions set wrong. It's fixed now, so you should be able to dl it.

Best,

Mark

nimbleRev
Posts: 10
Joined: Mon Mar 09, 2009 9:50 pm

Post by nimbleRev » Tue Mar 10, 2009 6:42 pm

Thanks for updating the link! Downloaded and got it working. Ah, sweet JSON bliss in RunRev :)

I'm new to the Rev world and was curious to find out the best practice for including a library like this in my stack?

From what I've read so far it seems like there are many options from including it as an external within the Stack inspector to manually adding it to the back scripts when the Stack opens, or maybe a combination of both?

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

Post by Mark Smith » Wed Mar 11, 2009 2:34 am

It all depends - the simplest approach is simply to include it as a substack of your main stack, and then 'start using' it on startup.

You can also 'start using' or 'insert into back' when required, and 'stop using' or 'remove from back' when no longer needed.

To be honest, I'm not very sure what the difference really is between 'start using' and 'insert into back'. I haven't seen any noticeable differences in my own projects, so I guess whatever seems most convenient in terms of your application design...

Do let me know if you run into any problems with libJson.

Best,

Mark

nimbleRev
Posts: 10
Joined: Mon Mar 09, 2009 9:50 pm

Post by nimbleRev » Thu Mar 12, 2009 2:47 am

I appreciate the tips Mark. I think I'm going to use "insert into back" for now. From what I understand when I use "insert into" for libjson it will only take the stack script and insert it into my stack's back scripts, which is perfect. But the "start using" command seems like it will load everything in the stack which would include your sample card and all the scripts in your buttons. I'm not 100% sure on that but seems like insert is better for including a library.

Thanks again for sharing the JSON library with the community. It's working great :D I'll let you know if anything comes up.

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

Post by Mark Smith » Thu Mar 12, 2009 11:17 pm

Great!

Mark

imed-edition
Posts: 12
Joined: Fri Apr 21, 2006 1:53 pm

Fix proposal to deal with diacritics characters

Post by imed-edition » Wed Sep 09, 2009 11:37 am

Mark,

Diacritics does not seem to convert appropriately. I suggest to change the two following handlers to:

Code: Select all

private function unicodeEscapes pString
   put false into inEsc
   repeat for each char c in pString
      add 1 to count
      put c after buffer
      
      if count < 2 then next repeat
      
      if buffer is "\u" then put true into inEscape
      
      if inEscape then
         if length(buffer) < 6 then
            next repeat
         else
            -- TO DEAL WITH DIACRITICS :
            if platform() is "MacOS" then
               put isotomac(numtochar(baseconvert(char 3 to 6 of buffer, 16, 10))) into buffer
            else
               put numtochar(baseconvert(char 3 to 6 of buffer, 16, 10)) into buffer
            end if
            put false into inEscape
         end if
      else
         put char 1 of buffer after nString
         delete char 1 of buffer
      end if
      
   end repeat
   put buffer after nString
   return nString
end unicodeEscapes

private function json.encodeString pString
   set the casesensitive to true
   repeat with i=0 to 7
      replace numtochar(i) with "\u" & format("%04x", i) in pString
   end repeat
   replace numtochar(8) with "\b" in pString
   replace numtochar(9) with "\t" in pString
   replace numtochar(10) with "\n" in pString
   replace numtochar(11) with "\u" & format("%04x", i) in pString
   replace numtochar(12) with "\f" in pString
   replace numtochar(13) with "\r" in pString
   repeat with i=14 to 31
      replace numtochar(i) with "\u" & format("%04x", i) in pString
   end repeat
   replace "\" with "\\" in pString
   replace quote with "\u0022" in pString
   
   -- TO DEAL WITH DIACRITICS :
   if platform() is "MacOS" then
      repeat with i=129 to 255
         put numtochar(i) into c
         put format("%04x",chartonum(mactoiso(c))) into tHexEncode
         replace c with "\u" & tHexEncode in pString
      end repeat
   else
      repeat with i=129 to 255
         replace numtochar(i) with "\u" & format("%04x", i) in pString
      end repeat
   end if
   
   if sUnicodeInput then
      return pString
   else
      return utf8encode(pString)
   end if
end json.encodeString
I'll be glad if someone can do a second check on the results. Especially, I do not have ready access to check under Windows.

Thanks! Hope this helps...
iMed Editor

imed-edition
Posts: 12
Joined: Fri Apr 21, 2006 1:53 pm

two bugs ?

Post by imed-edition » Wed Sep 09, 2009 1:21 pm

In the two following functions :

Code: Select all

private function jObjectToArray
private function jArrayToArray
It appears that the first of the three following lines should be changed from:

Code: Select all

   repeat while sIndex <= sNumTokens
      add 1 to sIndex
      switch sJson[sIndex]
to:

Code: Select all

   repeat while sIndex < sNumTokens -- and not: <=
otherwise, the sIndex in

Code: Select all

switch sJson[sIndex]
will go out of sNumTokens which is the max index bound value.

If someone is interested I can drop a new version of the stack including the fix from our previous post.

iMed Editor
(look at http://www.imed-edition.net/rev/index.html to display our direct email)

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

Post by Mark Smith » Wed Sep 09, 2009 8:52 pm

Thanks or these - I'll get the lib updated tonight!

Best,

Mark

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

Post by Mark Smith » Wed Sep 09, 2009 9:04 pm

Now updated to 1.0.4b - at

http://futsoft.futilism.com/revolutionstuff.html

Best,

Mark Smith

Post Reply