lua-users home
lua-l archive

Re: JSON to Lua syntax converter?

[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('([^\\])\\"', '%13円')
 :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 \"

AltStyle によって変換されたページ (->オリジナル) /