lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Feb 1, 2008 12:55 PM, Petite Abeille <petite_abeille@mac.com> wrote:
> Perhaps one could encode the sequence '\"' first [1]. That way there
> is no confusion about where a string starts and where it ends.

Hmmm... good point. You also need to distinguish between square
brackets that are inside or outside of strings, so you'd encode
those, too. Then you could do your conversion like this:

LUA = JSON
  :gsub('([^\\])\\"', '%1\3')
  :gsub('"[^"]*"',
        function(s) print(s) return s:gsub('[%[%]]', {['[']='\1',
[']']='\2'}) end)
  :gsub('[%[%]]', {['[']='{', [']']='}'})
  :gsub('("[^"]*")%s*:', '[ %1 ] =')
  :gsub('[\1\2\3]', {['\1']='[', ['\2']=']', ['\3']='\\"'})

Which is to say:
  encode all \" as \3 (while leaving \\" alone)
  within strings only:
     encode [ and ] as \1 and \2
  replace "key":  with [ key ] =
  decode [, ], and \"