JSON

Richard Gaskin ambassador at fourthworld.com
Wed Aug 29 09:27:21 CDT 2012


Rod McCall wrote:
> I am currently developing a server application for a car simulator and
> part of it may use JSON. I'd appreciate any pointers to tutorials or
> even links to libraries on how to use JSON in LiveCode, therefore if
> you have any tips in this direction please let me know. . Otherwise I
> may have to revert to CSV or XML. From memory LiveCode did have some
> nice XML features but it's a while since I tried them out.

The libJSON stack Andy suggested should work well for what you need:
<http://revonline2.runrev.com/stack/82/LibJson-1-0b>

FWIW, if by chance you're also using RevServer on the server end you may 
find working with encoded arrays even more convenient, and certainly 
much faster.

Compressed encoded arrays have become my favorite transport vehicle for 
data, running them through base64Encode to make them extra network-safe:

   put base64Encode(compress(arrayEncode(tArray))) into tOutData

On the receiving end it's probably best to handle each of the reversing 
functions in a set of "try" statements to avoid execution errors in the 
event that anything happened to the data in transit:

function PayloadToArray pArray
   try
     put base64Decode(pArray) into pArray
   catch tErr
     throw "Data not base64-encoded"
   end try
   --
   try
     put decompress(pArray) into pArray
   catch tErr
     throw "Data not compressed"
   end try
   --
   try
     put arrayDecode(pArray) into pArray
   catch tErr
     throw "Data not array-encoded"
   end try
   --
   return pArray
end PayloadToArray


Looking at the BSON format used by a growing number of data stores like 
MongoDB, it's a natural compliment to JSON but much more compact and 
with simpler parsing.  But of course if you're writing for a browser 
client or a server over which you have no control over the output, 
converting to and from JSON is about as good as it gets for expressing 
hierarchical structures easily.

But if you're writing for a LiveCode client and use LiveCode on the 
server, LiveCode's encoded arrays are quite similar to BSON in many 
respects, and with the speedy convenience of arrayEncode and arrayDecode 
they're a joy to work with, avoiding the overhead of the more verbose 
and computationally-costly JSON.

With encoded arrays, it's almost like working with native BSON on both 
sides of a client-server system.

--
  Richard Gaskin
  Fourth World
  LiveCode training and consulting: http://www.fourthworld.com
  Webzine for LiveCode developers: http://www.LiveCodeJournal.com
  Follow me on Twitter:  http://twitter.com/FourthWorldSys



More information about the use-livecode mailing list