lua-users home
lua-l archive

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


I'm working on a JSON parser using lpeg and have it mostly working.

One corner case I've run into is where two value types in JSON (null
and undefined) are being mapped to nil captures.

Some problems however:
1) lpeg.Cc(nil) does not seem to work right, it causes a function to
be put in the chain somewhere.  I worked around it by using  '/
function() return nil end'  Though this seems suboptimal.
2) lpeg.Ct( ... ) with nulls inside do not seem to increase the size
of the array as expected.

Ex: [null, 1,2,3, null, 5] results in an array  w/ # = 4
{nil, 1,2,3, nil, 5} results in an array with #= 6

My workaround was to use a 'function' value to represent nil, and then
apply the function to the result of lpeg.Ct, iterating over the table
cleaning out those nil representations.
Before processing i grab #t and store it in t.n, after removing the
values (in the above case and others) #t will no longer == t.n, so I
leave in t.n for the length.  If #t == t.n, then i remove that
element.

Any ideas on how to fix this up w/o my hacks.

--
Thomas Harning Jr.