JSON

Richard Gaskin ambassador at fourthworld.com
Wed Aug 29 10:49:45 EDT 2012


Absolutely.  If you're working with a setup that can use MongoDB, it's a 
wonderfully flexible store.

Where I stumbled into a fascination with encoded arrays was in CGI 
contexts on shared servers, where we don't usually have the luxury of an 
always-on process like MongoDB.

With modestly-sized collections you can get rather nice performance from 
a CGI handling encoded arrays, and you can enjoy it on any server 
without the need for any installs or mods beyond the simple RevServer setup.

It's a rather specialized use-case, of course, which is why I flagged it 
with "FWIW".  But even if you're working on a shared host there are many 
ways to skin the storage cat (pardon the gruesome metaphor).

--
  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


Andrew Kluthe wrote:
> Working with JSON a whole lot with MongoDB over a REST Interface. Mark
> Smith's (RIP) libJSON is really nice. As easy as arrayToJson(pArray)
> and jsonToArray(pJSON)
>
> On Wed, Aug 29, 2012 at 9:27 AM, Richard Gaskin wrote:
>> 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